Views/Flags relationship-argument puzzle - drupal

I'm currently wrestling with a views relationship-argument puzzle.
I have a Flag setup called Favorites, so that users can favorite site content. I also am using Content Profiles and Pathauto. Now I'm adding a views content attachment to the user content profile to display the user's chosen favorites.
Pathauto's default content profile path is: member/[title-raw] - so that a typical alias for a user's profile page is: member/john-smith (please bear in mind that the profile node id isn't the same as the user id)
And here's my views setup:
Relationship: flags:favorites - BY: Any user
Argument: This is where my understanding breaks down. I need to somehow get the username or uid out of the URL of the current profile.
Any ideas of the correct argument to get this thing to work? I've been trying out all the possibilities that occur to me, and so far no luck.
Thanks

In your View setup, click on + in Arguments to add an argument.
In your argument setup, select "User" from the "Groups" drop down.
Check "User: Name"
Click "Add" button
Save your view
As long as this is your only argument, you just have to pass in user name in the URL. In your example, so see the view for john-smith, you would navigate to http://example.com/viewname/john-smith.
That would be for page type view.
If you are creating a block view type, you cannot pass arguments in the URL. For a block type view, follow these steps:
In your View setup, click on + in Arguments to add an argument.
Select "PHP Code" for "Default argument type"
Enter the following for the PHP code,
if (arg(0) == 'viewname' && arg(1) != '') {
return arg(1);
}
Now that block will get arguments from the URL, similar to what occurs for a page view.

if you're willing to use custom php code in order to fetch the argument, http://api.drupal.org/api/function/drupal_get_normal_path/6 is your friend. you can pass it the alias you're visitting with arg(0).'/'.arg(1)

Related

Drupal 7 views Contextual filters with Blocks, default value doesn't work properly , wield

I have a view created that show content based on the url: eg: domain.com/projects/[username]/[projectname], which shows a project of a specific user, that works well. Additionally, I want to show the related projects of this user on the sidebar, so I create a block view, using the Contextual filters and the default value.
because my url is projects/username ,By using the default value -> raw value from URL, so I set the path component as 2. but that doesn't work for me.
eg: suppose my username is "abc".
in the preview, if the url path is "projects/abc", it doesn't show anything although the username is in the 2ed component of url path.
if the url path is "abc/*", it will show the related content, meaning as long as the username is in the first argument of the url path, it works.
I don't what happens, it seems that configuration of path component as 2 doesn't work. I am so puzzled , what happened ?
Yo are missed something. I think each of your users have a url alias. For example 'user/1' have url alias 'abc'. Url components of contextual filter works for direct url, not for alias. For example second component of user url 'abc' will be '1', becauser original url is 'user/1'.
You can easily debug it by printing argument in working views header, try it yourself:
<?php print arg(0) . '<br>' . arg(1); ?>
For my user/1 that have alias 'abc' i received next:
user
1
For path 'abc/*' this is second component, but
for path 'projects/username' it is not 3-rd because this url hasn't aliases with [uid] component, you must use projects/[uid] or other validation for argument, for example you can check autor of a project. Choose a 'User ID from UR'L and check option 'Also look for a node and use the node author' in default value settings of contextual filter.

How do I list all nodes created by user specified in argument using Views in Drupal?

I have been trying to create a page using views that will list down all the nodes authored by a specific user.
The user name will be specified in the path (like - stuff/[user-name] )
Can someone please tell me how to do this using Views.
I am stuck on a dead end
for the views url path specify: stuff/%
in the arguments section add: user -> user: name
Should do it, but I'm not sure if it isn't buggy (see this issue: http://drupal.org/node/744468)
If you use user:id instead of username, you will be fine.

Take data from a field from exsisting node and make that the default value of a field in different content type

I don't know if I'm on the right track but I'm trying to let users of my web site create there own versions of pages on my web site.
Basically I'd like to make our documentation used as a starting point where they just add details and make a new page for themselves in the process.
I have a 'book' content type that I have changed with CCK and a 'client edits' content type that uses a nodereferencefromURL widget to link itself to the book node.
So simple version of what I'm saying is I have a link on my book pages that creates a node using client edits content type. I would like to put some fields on the client edits content type that take the values of some of the fields from the book page it is linked from.
I'm sure I'm missing something as I would have thought someone would have tried this before but I can't even find a hint on how to go about this.
All I really need is a point in the right direction if my current thinking is wrong.
Current thinking is that I use a php script to get the default value for a field on the new node add screen that drags the value for a field from the book I'm linking from.
I'm thinking this is the case because there is an option for default values for the field in cck manage fields that lets you put in a php value to return a default value for your field.
Am I on the right track or is there already a module or process that does what I'm talking about and I'm just too dumb to find it.
This sounds a little strange, are your client edits going to be a diff from the original node or just coppied data?
I would prehaps do it a more simple way, just have book nodes, and have different fields disaply depending on who edits it (enable the content_permissions module). That way you can use the node clone module to create the users copy.
You will need to make a module to contain your custom php code.
I ended up using rules to save information from the user and the cloned node into hidden fields.
One that saved the original node ID into a field when ever you create content of that type unless the url ends with Clone. This means that when you create the clone the original node ID is kept in the field.
That made it easy to use a views argument that took the node ID to make the clone appear along side the original when a user visits the original page.
The second rule trick was to compute a field that saved the "store name" from the profile of the user only when saving clone content.
This meant that there was a hidden field on the clone that stored the info so I could then use another views argument to restrict the view to only people with the same store name in their profile.
I am no good with PHP but I managed to find a snippet (can't remember where) that returns the store name of the current logged in user as the argument.
global $user;
profile_load_profile($user);
return $user->profile_store_name;

Drupal using views with CCK custom fields

I've got a Drupal site which uses a custom field for a certain type of node (person_id) which corresponds to a particular user. I want to create a view so that when logged in, a user can see a list of nodes 'tagged' with their person_id. I've got the view working fine, with a url of my-library/username but replacing username with a different username shows a list of all nodes tagged with that user. What I want to do is stop users changing the URL and seeing other users' tagged nodes. How can I do this? Is there somewhere where I can dictate that the only valid argument for this page is the one that corresponds with the current logged in user's username?
person_id = uid?
In this case, add argument with user:uid, then in Validation options select PHP Code, read comment of this field carefully:
Enter PHP code that returns TRUE or
FALSE. No return is the same as FALSE,
so be SURE to return something if you
do not want to declare the argument
invalid. Do not use . The
argument to validate will be
"$argument" and the view will be
"$view". You may change the argument
by setting "$handler->argument".
Add this code:
global $user;
$account = user_load('name'=>arg(1));
$handler->argument = $user->uid;
return $account->uid == $user->uid;
I'm not sure how you have setup your view, which gives some different options to solve this. A way that should work would be to set the default argument be the logged users id/username and remove the argument from the url.
Alternatively you could create your own filter which requires some work with the views API, but gives more control.

change user_profile_form form fields order

When a user login , the user will be redirect to a user profile page, which has a My account field set.
the field set has 2 fields, "Username: ", "Email address:". those 2 fields are generated by drupal.
those 2 field contained in a form which has a id ("user_profile_form") . I want to change the order of those 2 fields.
I have tried to intercept 'user_profile_form' , inside hook_form_alter.
code as follow:
$form['account']['name']['#weight'] = 1;
but that did not success, drupal did not even rendering the 'name' field, so no username: showed on browser.
What you did is absolutely correct, and probably did work. You can change the weight of the fields with the method described above.
The username field is not always rendered. The reason is that a persmission is required: change own username. If that perm is not set, you wont be allowed to alter you username and the field wont be shown.
Info on debugging.
Your info alone is not quite enough to debug. From what you describe, you are doing the right thing, but other modules could be making things a bit tricky for you. The devel module is quite good when it comes to debugging, ti defines two functions I use a lot when debugging:
dpm() pretty prints the variable to the message area using krumo.
dd() Prints / saves a variable to a log file. Useful when you can't view messages on the screen.
I would suggest that you look at the $form variable before and after you alter it.
Things that could make it go wrong:
Did you remember to pass the $form variable by reference using the & notation?
Is another module altering your form after you?
Are you checking for the correct form id, so you alter the correct form?
These are some pointers, before you bring more info, all I can do is guess to what your problem exactly can be. I did something like this a few days ago so I know what you describe shouldn't be a problem.

Resources