List -> l_user I have a List node that has a user-reference field (l_user).
Story -> s-user I then have Story nodes that also have a user-reference field (s_user).
There is no direct connection between List and Story.
I want to add a view to List that lists all the stories that reference the same user that list references. Basically something like SELECT stories WHERE story.s_user = this list.l_user and l_user is the user referenced in the list this view is on.
So far the view is being filtered by node:type story but I'm not sure what to use (arguments/filter, etc) to link the story s_user to the list l_user.
Is this doable with Views?
You can do this with a Views argument. What you're trying to do is filter that list by user, and you get the user to filter by from the node you're currently on (or more specifically, the node's user reference field). So what you'll need to do is supply Views with an argument that is equal to the node's user reference field.
To do that, set up your view as normal and as if you were showing every user's node. So you might have a view that's like:
Page 1 (by User 1)
Page 2 (by User 1)
Page 3 (by User 2)
Page 4 (by User 2)
Page 5 (by User 1)
Where the user is a user reference field on each page called Story user reference.
Now, under Arguments add an argument for Content: Story user reference. Now, the view will only show nodes that are posted by the user specified in the argument. The problem is, in a block, there is no argument to specify: you need to provide a default argument.
Still on the argument configuration pane for Content: Story user reference, select Provide default argument under Action to take if argument is not present. You'll get a variety of options, but none of them are what you're looking for: the current node's user reference field.
So you'll need to use the PHP code action and use the following code:
$node = node_load(arg(1));
return $node->field_list_user[0]['uid'];
This loads a node based on the node ID retrieved from the current page's path and returns the node's user reference field (change field_list_user to the name of the user reference field on the list nodes).
So if I'm on node 17 whose user reference field states user 4, the argument that'll be passed to the view is 4. The view will then only show nodes in the view who have user references that are also 4.
Save your view, create a block display, and place it wherever you want. When you visit a node page with a user reference field, the block will populate with the referenced user's nodes.
I'd suggest getting all of the user references from the list node and passing those into a views argument. So your code would look something like this (untested):
$user_ids = array();
foreach ($list_node->l_user as $user_reference) {
$user_ids[] = $user_reference['uid'];
}
$view = views_get_view('list_view');
return $view->preview('block_1', array(implode(',', $user_ids));
That assumes you have a view named 'list_view' with a display named 'block_1' (you can see the machine name when you hover over the display). That display needs to be a node view with a filter of node type 'story' and an argument of content user_l set to take multiple values. There's almost certainly a bug in that code, but I know the general concept works, as I've done it several times before.
To solve this you need to download NodeReferrer. http://drupal.org/project/nodereferrer
It provides a counter part to CCK's node reference field.
Then you can call the field "Node referrer" in your View.
Related
I am trying to build a project in which links to different pages(php) are given on a page. I have a variable which gets the unique id from the link and accordingly displays content based on the id. The user is allowed to delete/edit the content.
When a user clicks two(or more) links one after the other, the second's link id is stored in the variable and if the user presses the delete button on the first link's page the second one gets deleted as the variable has the id from the second(last pressed) link.
I don't want the variable to get modified and each link should be treated as a separate entity. Please tell me how to solve this problem.
A unique id is sent to the class through the link and based on the id content is displayed to the user.There is an option to edit/delete the content and if two links are clicked the id is getting overwritten and the wrong content is deleted. In order to avoid that i want an object created every time so that each object will have its own variables and id is not overwritten
You could pass something in from the query string or some other request context variable.
I have a foreach loop which generates links with different IDs. I have an ajax function to process this ID and the username (from session) to add a record to the table. (It's like enrolling for some events).
I want to make an additional statement which will check whether the user has already enrolled for the event with some ID and if so, the link will be deactivated or change color. I've tried creating another variable (which is passed to the view)
$data['myvariable'] = $this->mymodel->myfunction();
This function in model checks all the records from the database where user's username appears and insert to array all event IDs. I've tried adding an additional if statement before links in foreach loop which checked whether the ID from link is in array but there appeared some problems with the controller. (i think that I couldnt assign the array to the variable $data['myvariable'] ).
I know that accessing to model from view is not "proper"... Anyone know how to solve this problem?
Do you have a field in the DB to store whether or not the user has enrolled? Is so, run a select query on that table checking for that value then use a conditional statement to effect the link. Kinda like this
$enrolled = $this->your_model->your_method($param);
if($enrolled){
process links here;
}
I'd say it depends on the MVC your using also. You may have to load the model in the view to use it from within the view, not ideally the right approach as you could load it via your controller and set it as a variable to be passed into the view if the variable to be passed contains multiple outputs such as a query from a DB or something you can pass it to the view as an array object and then in the view do a while, for, foreach, whatever on that array.
Is it possible to have views show a query based on the current node (not 1 set node but any node)
my situation:
A user submits a node with information
another user can donate to that node from another content type using 2 fields: node reference (to pick the node) and integer (to enter donation amount)
I need a block view to show all the donated amounts for the current displayed node
(would be even better if someone told me how to just all the donated amounts up to show the total amount!)
thanks in advance
If you need dynamic filtering, views arguments is your friend.
After you add fields and filters, add an argument Content: node reference (the name would be whatever you chose to name it, but make sure it's node reference).
In the argument setting, for "Action to take if argument is not present" click on provide default argument and choose Node ID from URL, do validation.
Add a block view and enable it on the block settings page. It's only going to show on the appropriate nodes.
Check this tutorial for more detail.
To do calculations, take a look at Views Calc module.
I have a link in my system as displayed above; 'viewauthorbooks.php?authorid=4' which works fine and generates a page displaying the books only associated with the particular author. However I am implementing another feature where the user can sort the columns (return date, book name etc) and I am using the ORDER BY SQL clause. I have this also working as required for other pages, which do not already have another query in the URI. But for this particular page there is already a paramter returned in the URL, and I am having difficulty in extending it.
When the user clicks on the a table column title I'm getting an error, and the original author ID is being lost!!
This is the URI link I am trying to use:
<th>Return Date</th>
This is so that the data can be sorted in order of Return Date. When I run this; the author ID gets lost for some reason, also I want to know if I am using correct layout to have 2 parameters run in the address? Thanks.
Yes, the layout is correct. Of course it's possible to have even more than 2 parameters in the query string
you have to check resulting query string (just take look into your address bar after click), there must be something like viewauthorbooks.php?authorid=22&orderby=returndate. If number is lost, you have to check it's source - $row['authorid'] variable.
you have to use & and sanitize your output, but apart from that it's correct ;)
Using the profile module I've created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?
I used the dsm($user) function to output the user variables and it contained everything except the data for the profile_real_name
I also ran dsm($vars) on the phptemplate_preprocess_user_profile and I could see it contained an object called account which did contain the info I needed but this object isn't available in the $user variable.
Many thanks
If you want to access the author's profile information in node.tpl.php, then you want to work with a phptemplate_preprocess_node function rather than the user_profile one. The node preprocess function doesn't have an $account object by default though, so you'll have to load it in:
This goes in the phptemplate_preprocess_node function in your template.php file:
if ($vars['uid']) {
$vars['account'] = user_load(array('uid' => $vars['uid']));
}
Then you would be able to access the author's profile values in your node.tpl.php. The value you asked about specifically would be:
print($account->profile_real_name);
However, it sounds like you might want the node author's name to appear as the profile_real_name value rather than their account name?
If so, a MUCH more efficient way would be to override the theme_username function.
That's not directly what you asked about so I won't go into it here, but this post on the drupal.org forums would be an excellent place to start for Drupal 5 or 6:
http://drupal.org/node/122303#comment-204277
$account is what you usually call a user that isn't the global user to avoid accidently overwriting the global user which would result in the user be get logged in as that user.
I just did a bit of checking and the easiest way to solve your problem is to use $account in the template instead of $user.
Using $user in the template or doing like WmasterJ suggests is faulty. You will post the wrong data. You will post the data of the logged in user not the data of the user who's profile is being watched. This bug will happen when you view all other users' profile than your own.
Preprocess functions is not hard to make, in your template.php file in your theme you just replace phptemplate with your theme's name defined the code. In this case you wont need to alter the preprocess function, since you already have what you need.
If you want to do this within for instance the user-profile.tpl.php all the information you need exists within the $account array.
Otherwise you can access user data by loading a user object based on it's id (of the currently logged in person that is, or if you can query the DB and get uid that way).
First get the uid of the current user:
$uid = $user->uid;
Then load the a user object:
// Create user objets based on uid ()
$user_obj = user_load($user->uid);
Then load that users profile variables:
// Load profile
profile_load_profile($user_obj);
Now the $user_obj variable (which is passed by reference to profile_load_profile) has an object with the the profile information that can be accessed like this:
$user_obj->profile_real_name
Hope it helps!