drupal :how to pass current userid as argument - drupal

i need to display the contents added by that user...
i have designed a view to display the result.. but it displays the results of all users.....
i need to display the contents added by that user...
when i pass the argument of userid it works, but is there any dynamic way to do that....

If all you need is a list of nodes by the current user, the easiest way is to use the User:Current filter. Just click the [+] next to 'Filters', choose 'User:Current' from the list, click the 'Add' button and set the filter to 'Yes'.
The argument-method as described by streetparade (using the 'User: Uid' argument) is useful if you want users to see the list of other users as well. Suppose that your view's url is www.example.com/my-content. If you add a user argument, user #3 could see what user #7 has posted on www.example.com/my-content/7. If you want that, use the argument. If you do not want that, use the filter.

Use This
<?php
global $user;
$userId = $user->uid;
?>
You can access all this way
(source: chromaticsites.com)

Related

Drupal user permissions & odd content types

I have a permissions problem in Drupal. I want users to be able to create a certain node type, but there are two different paths I need to give them permissions for to let them do this. The type is content created by a module called isbn2node, and there are two ways to make content through it, each with different paths:
?=node/add/isbn2node-book
?=node/add/isbn2node_book/isbn2node
One has an underscore and the other one has a hyphen. The first path leads to a form that lets users enter information on a book manually; the second path lets them enter an ISBN, searches for it, and populates the form for them based on the results.
I've changed permissions in the People menu so they can add isbn2node-book content manually using the first path, but there isn't an option to let them use the second method. Aliasing the url so it didn't have node/add in the path didn't work either.
Creating a duplicate content type seems like an ugly solution to this; is there a more elegant way to let users access that second path?
A little code in a custom module using hook_node_access should do it.
$node is either a node object or the machine name of the content type on which to perform the access check (if the node is being created then the $node object is not available so it will be a string instead).
So this should do it:
function MY_MODULE_node_access($node, $op, $account) {
if ($op == 'create') {
$type = $node;
if($type == 'book' && $account->uid) return NODE_ACCESS_ALLOW;
}
}
I figured this out, and the issues I was having were specific to this content type. The ISBN2Node module requires users to have the Administer Nodes permission to use its lookup and bulk import features.
There is some extra code for the module's hook_permission and hook_menu sections submitted as a fix in the module's issues thread.

Show a previous page value in Drupal multi-step webform?

In a multi-step Drupal webform, is it possible to show a value entered in a previous-step page?
For example, the first step page captures a username field as "John", is it possible to show a greeting in next step page, showing Hello John, ... ?
p.s. for anonymous user.
I know this is old, but I needed the same thing so I hope this helps others in search.
I needed values from a previous Webform step for my custom Webform component. The _webform_redner_[component](...) hook doesn't provide the form or form_state, also the previous steps' data (more than previous step that was just submitted) isn't in the $_POST.
To solve this, I manually retrieve the form from Drupal's form caching system using the form_build_id which is in the $_POST variable.
/* ... */
$form_state = array();
// Get the form_state to pass on to our build function.
// Webforms doesn't provide it at this point so we'll need to manually get it using the form's build_id.
$form = form_get_cache($_POST['form_build_id'], $form_state);
/* ... */
You now have the entire form and form_state that also includes previous steps' values.
Have a look at multistep form example here: http://drupal.org/node/717750
The general idea is that in the submit function you save all posted values to $form_state['storage'], which you can later access on the next steps.
Edit:
Have a look at the example with both Prev and Next buttons I have just created: http://zgadzaj.com/basic-drupal-multipart-form-example-with-previous-and-next-buttons

Check if user agreed to terms , set cookie

I'm a Drupal nub. I would like to check on every page if user (anonymouse) agreed to somekind of terms. I suppose i should write small custom module ?
Where will this condition be written
if(!$_COOKIE('confirm')){
//jQuery show confirmation form
//Set cookie for 1hour
}
maybee in page.tpl.php ? Please, give me some tips ..
If you don't want to store the info for a long time, you should use $_SESSION variable. Then in preprocess page you could check if the user has accepted and set a variable that you can use in your page.tpl.php.
user_save() accepts an array argument which you can put custom data into. This will then be loaded with your $user object and you can use in any template file.
Check out these modules:
http://drupal.org/project/legal
http://drupal.org/project/terms_of_use
The Legal and TOS modules are good if you need a login. If working with anonymous users, however, you'll need to use the rules module with https://www.drupal.org/project/rules_session_vars.

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.

Views/Flags relationship-argument puzzle

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)

Resources