Get all user data with custom field drupal 7 - drupal

I am new to Drupal. How do I get all user data in a single query or function, including extra fields like middle name, phone # from:
Configuration » People » Account settings
I want all the records of all the users, including the extra fields listed above. How can I achieve this?

If the fields are attached to the user : user_load();
Something like :
$user = user_load(1); // 1 = uid of admin user
drupal_set_message($user->field_middle_name); // print a message with your user custom field named middle_name.
If you got many users to load, use user_load_multiple, the perfs will be better.

Related

Is it possible to pass a special query parameter value to a WooCommerce product link?

I am looking to use WooCommerce in a bit of a strange way, and I'm wondering if there is any way to make this possible. Here's my desired workflow:
Step #1: From a different site subdomain, provide a link to a certain virtual product, but with a query parameter with a unique user id number.
Explanation: The user is at othersite.example.com, and they get a link to buy a product in a WooCommerce store set up with a wordpress site at https://example.com/product/virtual-product
However, this product will be a payment to unlock something on the othersite.example.com site which has its own user and authentication system. (Firebase)
Would it be possible to pass a user id from the othersite.example.com by way of a url query parameter and then have that included in the order info?
ie. From the othersite.example.com someone could be given a link to the product like this https://example.com/product/virtual-product?userid=00000000000000000, with 00000000000000000 being their user id at othersite.example.com.
Then if that userid query value could be included in the order, the following steps should be doable.
Step #2: Have a webhook that fires when the product is purchased, telling a server managing the users for othersite.example.com that the user with userid 00000000000000000 has made a successful purchase of that product.
Is there a way to accept custom values like this to the order? Or is this totally out of the scope of WooCommerce's functionality?
Thanks so much.
I believe that it can be done by using a redirection/link directly to the checkout page, like: "http://yourdomain.com/checkout/?add-to-cart=PRODUCTID&000000" (tested)
In the example, "checkout" stands for the name of your checkout page, "PRODUCTID" stands for the id of desired product, "000000" stands for the user id of redirected person in the other website.
Then you can add a hidden input field to the checkout page (into checkout form, so you will see this value in order) and using the URL you can assign the "000000" (user id) to the value of this field.
I hope that works. If you have any problems with implementation, you can ask me.
- Useful link for hidden input field addition: https://stackoverflow.com/a/42614905/11003615
- Useful link for getting value from current URL using JS: https://web-design-weekly.com/snippets/get-url-with-javascript/
- Useful link for getting value from current URL using PHP/Wordpress Core: https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
- Useful link (contains 4 parts) for adding a custom field to checkout & validating this field & saving this field to order fields & displaying the value of this field in order page: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-7

Drupal how to have certain admins only see the users they've created

I use Drupal 7 and would prefer to do so but if I need to use Drupal 6 I will. I have a Drupal 7 site that I allow "advisors" to create authenticated users. I am an admin on the site so I see and can do everything. All that an "advisor" can do is create and edit authenticated users. Is there a way to make a view that displays the users that the particular advisor has created while the advisor is logged in?
If you don't understand what I'm saying let me put it this way. I am the admin of the site so I can do everything. I created a user role called advisor. There's also an authenticated role for users. Advisors can create authenticated users. So I have Advisor 1. Advisor 1 created 10 users. I also have Advisor 2. Advisor 2 created 3 users. I'd like a page (more than likely built with views - and I've used views before on other sites so I'm familiar with them) - I'd like a page that Advisor 1 could go to once logged in and it would display all of the 10 users that they created. Advisor 1 wouldn't see the users that Advisor 2 create.
Is this possible? Any help on this would be greatly appreciated. Thank you in advance.
I have implemented something similar and I can give you some guidelines.
First of all drupal 7 doesn't store the information of the creator of the users.
So, in order to track this you will need to attach a custom field to the user to keep this information.
In such case you ll have to make sure that every creator can insert only himself in this field and not anyone else or you 'll have to find an automated way to fill it in.
I suggest you use field permission module for setting permissions to this field (users probably should not even have view, creators should not be able to change it etc ) and computed field module for automatically populate the field (eg creator_field) upon creation for example with the uid of the creator.
Finally when creating the view you could add contextual filtering by using the logged in user on the creator_field.
You should also have a look at Organic Groups. I haven't use it but might be helpful
Hope it helps.
Updated
Add
global $user;
$entity_field[0]['value'] = $user -> uid;
to the Computed Code (PHP) in the computed field settings and store the value as integer in "Database store settings". Then in your view you should add a view of user with contextual filter of creator_field. In the contextual filter settings you must set "provide default value" -> "User ID from logged in user" in the "WHEN THE FILTER VALUE IS NOT IN THE URL" section.
This could be done relatively easily in Drupal 7 by adding a field to the user profile that points back to the user creator (using the Entity Reference module). Profile2 could offer a shortcut to making this field available on a profile, though if this is the only customization that you need to add to your profiles, it would be cleaner to do this in a custom module.
A view (Views module) could be configured to output a list of users with the current logged in user as the creator.
You'll also need to add similar logic for user_access to allow/restrict profile editing (if the current user is the user referenced in the profile). A permissions hook also could be useful if you plan to have different admin levels.
The Tokens module should work to insert this value for the new user, by inserting the current user into the field. Or you can do this before the user is saved. It would go a little something like this:
function mymodule_user_presave(&$edit, $account, $category) {
if ($account->is_new) {
global $user;
$created_by = $user->uid;
$edit['created_by'] = $created_by;
}
}
Good luck.

user_load by a user profile field?

I'm trying to login users by detecting it's facebook user id.
"profile_fbuid" is a (hidden) profile field that I created to login users with the corresponding facebook user id.
When a user tries to login with Facebook I detect his/her facebook user id, but when I try to match with corresponding Drupal user this line doesn't work:
$user_exists = user_load(array('profile_fbuid'=>$fbuserid));
I get this error: user warning: Unknown column 'profile_fbuid'
I know what the error means, but I don't know how can I search a user using a user profile field.
Thanks for your help!
You created a custom field that is attached to the user profile called profile_fbuid. What you want to do in the database is look for this table:
field_data_field_profile_fbuid.
The relevant columns are entity_id, which will be the (Drupal) user id, and field_profile_fbuid_value, which will contain the value (the Facebook UID).
There is no field in the users table called profile_fbuid, so I assume you generated this field by using the user profile module.
I think it's better to create a query which gets the uid of this column and then passes it to the user_load() function.
$query = db_query("select `uid` from {table_name} where `profile_fbuid` = %d ",$fbuserid );
while($result = db_fetch_object($query)){
$uid = $result->uid ;
$user_exists = user_load(array('uid'=>$uid));
}

Drupal 7: dynamic elements/tokens (logged in user's name) in basic page

I'm looking for a simple and clean way to create a basic page that shows something like this:
Hello $user, your ID: $userid
$user and $userid should show respective values. I want to achieve this with Drupal 7 without having to:
enable PHP filter
hack Drupal core files (like http://drupal.org/node/1073886 would do)
use unstable modules (if possible)
create new modules/code (if possible)
Thanks for your help!
You could do this by creating a view using Views module:
set the logged in user as argument.
Then you can add the user name and user id as fields but select not to display them.
Add a global text where you can insert the user id and user name values in the text using placeholders.

Drupal: help with content type creating & linking nodes

I have a situation where someone can create a job content type. I've added a CCK field that is related to the 'organisation' content type. So when someone creates a new job posting they can choose which organisation the job is for in that field (auto completed). The problem is I can create a new job and select any organisation, even if I didn't create that organisation myself.
Is there a way I can restrict what someone types in that field to 1. an organisation name that exists, and 2. an organisation that belong to that user?
I'm using drupal 6.
Any help most appreciated!
Jonesy
You can use a php rule for the allowed values array of your node reference field, which I think in your case would need to run a quick query for all the nodes made by the current user.
I have not tested this code, but it should be reasonably close to what you're after if I have understood the question!
global $user; //make sure we have access to the user
//find all the job organisation nodes made by this user
//(replace organisation with the actual name of your content type)
$query = db_query("select nid from {node} where type = 'organisation' and uid = %d", $user->uid);
//put the results in the values array
$values = array()
while($result = db_fetch_result($query)){
$values[] = $result;
}
I'm not in a place where I can check this easily, but you could also try using a select on the edit page, and display the choices with a view. Then, create a view over the content type and check that the node user is the logged in user.
You could use a Nodereference field.
It's Autocomplete, and on the bottom of the settings of the field, you can select a View to filter the Nodes that can be referenced ( it's called "Advanced - Nodes that can be referenced (View) ")
Just make a View limiting the nodes to nodes that the current logged in user created, and use that on the Nodereference settings page.
Using that option, drops the option 'Content types that can be referenced' which is located above the Advanced settings.
So make sure to add a filter on node type.
I assume you already use Nodereference, and you might have looked past that option

Resources