Adding custom fields to Drupal 7 user profile depending on role - drupal

I have the user-signup disabled on my Drupal 7 website. Only the administrator can create accounts.
I want to add custom fields for a user depending on the role the user has been assigned by the administrator.Now I want to add some fields to the registration system that depend to the role that the admin chosen.
eg: If the admin has created a user account with role A and another account with role B.
When the users log-in, user A should be asked to enter phone number whereas the second user should be asked to enter home address.
I tried profile2 module but I could not achieve what I wanted.

Use the User_Role_Field module that does exactly what you want. If you need better configuration per role access use the Field_Permissions module.
Notice that after user registration the fields are displayed under user profile and not in registration form. Your question is a bit confusing...

There's the conditional_fields module, which sounds like it'll get you most of the way there.
Failing that, I'd say your best bet is to use hook_form_alter() in a custom module.
Something like:
function yourmodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register') {
// Define the phone field.
$form['phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone'),
'#size' => 30,
'#maxlength' => 30,
'settings' => array(
'#states' => array(
// Hide this field when role A isn't checked.
'invisible' => array(
':input[role="A"]' => array('checked' => FALSE),
),
),
),
);
// Define home address field
// [copy the snippet above & tweak to suit...]
}
}
This is unlikely to work out of the box (I've no drupal environment to hand), but with some adaptation it should get you most of the way there.
The "':input[role="A"]' => array('checked' => TRUE)," line will need the most work, i.e. "role" will need to match the name attribute of the "Roles" checkboxes, and the "A" will need to correspond to A's actual value when checked.
If you've no other form_alter usage in your module, you might also consider replacing the function name with "yourmodule_user_register_form_alter".

Related

Changing Type from Content To Global or VDC Drupal

I'm using Drupal 7 with the view_database_connector module. I'm currently working with a view that consists of a table displaying database information. My goal is to have a field with action buttons corresponding to each row, such as delete.
I am not allowed to use global php as a field.
I've attempted to make a custom module following this. I can currently use this module on content tables, however, when I try to use it on my view_database_connector table, I'm unable to add it as a field, since it's not apart of the same group.
Here's where I set up the information for making the action:
function mymodule_views_data_alter(&$data) {
// debug($data['node']);
$data['node']['actions'] = array(
'title' => t('Actions'),
'help' => t('Clickable links to actions a user may perform on a Node.'),
'field' => array(
'handler' => 'mymodule_views_handler_field_actions',
'group' => 'Content',
'click sortable' => FALSE,
),
);
}
I've tried deleting Content, changing it to global, and changing it to the VDC type, but none of that changes it into Global or VDC.
Alternatively, if there's an easier way just to hook into a field that has a button to run my code which will download a file, that could circumvent this issue.
You don't have to create this feature on your own - it already exists in views. There is a field type with actions as delete.
Other option is to use option called "rewrite value of this field" (or similar). With it you can make your field from views interface, and totally rewrite it's output. You can use token and some of them for sure contains node id, so you can use it to generate link to node deleting/editing/viewing pages.

Woocommerce Admin Order Page Required Fields

I use the Admin Order Page of WooCommerce to book certain products in a box office.
I want to make certain customer data required fields. (Like first_name,last_name,email etc.)
That the order can only be processed (saved) with this data entered in the billing information field.
I haven't found any solution on the internet that makes the fields in the admin panel required. Only found solutions for the frontend.
Any ideas?
this will prevent "Save order" button from firing if first_name is empty in the billing section.
add_filter('woocommerce_admin_billing_fields', 'woocommerce_require_admin_billing_fields');
function woocommerce_require_admin_billing_fields( $fields ){
$fields['first_name']['custom_attributes'] = array( 'required' => 'required' );
// $fields['last_name']['custom_attributes'] = array( 'required' => 'required' ); // for last_name
return $fields;
}
but keep in mind that this prevention is only on client side and will not work on older browsers...
I can't find a way to stop post from saving on the server side.

Change cache settings programmatically in Drupal 7

I have a block which displays list of RSS feed from an external site. I want to keep caching other blocks except the mentioned block. Howto do that?
For example, I have blockA, blockB and blockC. I only want to change the blockB's cache settings permamently to DRUPAL_NO_CACHE and leave other blocks as they are and I want to do that programmatically.
You can change the caching roles in the specific module that creates youre block.
In the block info like beneath:
function pref_block_info() {
return array(
'pref_main' => array(
'info' => t('Display flash game for auth. users'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_winner' => array(
'info' => t('Show the winner of the last week.'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_leader' => array(
'info' => t('Show the leader of the current week.'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_top' => array(
'info' => t('Show the top 10 of the current week.'),
'cache' => DRUPAL_NO_CACHE,
),
);
}
The answer given by Jurgo is perfectly right, if you are defining the block within your own module.
In case if you want to change the caching behavior of a block written by some other module then you can use the function mymodule_block_list_alter
function mymodule_block_list_alter(&$blocks, $theme, $code_blocks) {
// Remove the caching on rss feeds block.
// Here rss-feeds is the unique key for the block
$blocks['rss-feeds']['cache'] = DRUPAL_NO_CACHE;
}
Where do the blocks come from? That's important. As Jurgo said, you can specify it in hook_block_info if it's a custom module. If they are views blocks, there is a caching setting per display within views that handles this. If they are blocks provided by some other module, you'd need to directly query the database to change the block's caching setting.
As a general note, to display RSS feeds, just use Feeds and Views. Then you don't write custom code at all for any of this.
This will reduce the work by going to performance settings page (admin/settings/performance) & clicking "cleared cached data" by scrolling down.
But make sure that this page is only accessed by administrator.
For Drupal 7 is same as Drupal 6:
<?php
drupal_flush_all_caches();
drupal_set_message('cache flushed.');
?>

Menu path with wildcard

You can't use wildcards in menu paths? A quick summary of my problem (which I've made sure makes sense, so you're not wasting your time): I have a menu which i'm showing on node pages of a certain content-type. My path to a node page would be like...
events/instal2010
...where instal2010 would be the name of an event (event is the content-type).
I'm using the Context and Menu Block modules to place a menu in the sidebar on that page...
Event (the default active item)
Programme
Visitor info
Book tickets
... where the path for Programme would be
events/instal2010/programme
So for this to work for many different events, those menu items need a wildcard in their path, e.g.
events/*/programme
Perhaps it's time to ditch menus and just use a block with php to determine what page we're on from the URL.
Any advice from experienced hands would be awsome, thanks.
You cannot create menu items with wildcards from the administrative interface of Drupal, but you can create menu items with wildcards in a module. I would recommend creating a custom module that uses hook_menu() to create the menu items. An example implementation would look something like:
function YOURMODULE_menu() {
$items = array();
$items['events/%/programme'] = array(
'title' => 'Programme',
'description' => 'Loads a program page',
'page callback' => 'YOUR CUSTOM FUNCTION NAME', // Custom function used to perform any actions, display the page, etc
'page arguments' => array(1), // Passes wildcard (%) to your page callback function
'access callback' => TRUE, // Change if you want to control access
'type' => MENU_NORMAL_ITEM, // Creates a link in the menu
'menu_name' => 'primary-links' // Adds the link to your primary links menu, change if needed
);
return $items;
}
In $items['events/%/programme'] = array(, the % is the wildcard and it will be passed to your page callback function. It may be helpful to read more about hook_menu() and the Anatomy of hook_menu may also help as well.

Drupal pass argument to page

I have a custom Drupal module displaying some data in a table. Each row has a link which if clicked will delete the relevant row. Specifically, when the link is clicked it will take the user to a confirmation page. This page is really just a drupal form which says 'are you sure' with two buttons: 'Yes', 'No'. I figure I will need to pass the rowID to the confirmation page.
My question: What is the typically way to pass data to a new page in Drupal 7? I guess I could just add the rowID to the URL and use the $_GET[] from the confirmation page... I don't think this is very safe and was wondering if there was a better 'Drupal' way.
Thanks!
You'd use something like the following
<?php
function yourmod_menu() {
// for examlple
$items['yourmod/foo/%/delete'] = array(
'title' => 'Delete a foo',
'page callback' => 'drupal_get_form',
'page arguments' => array('youmode_foo_delete_confirm', 2), // 2 is the position of foo_id
'access arguments' => array('delete foo rows'),
'type' => MENU_CALLBACK,
);
return $items;
}
function yourmod_foo_delete_confirm($form, &$form_state, $foo_id) {
// load the row
$foo = yourmod_get_foo($foo_id);
// build your form, if you need to add anything to the confirm form
// ....
// Then use drupal's confirm form
return confirm_form($form,
t('Are you sure you want to delete the foo %title?',
array('%title' => $foo->title)),
'path/to/redirect',
t('Some description.'),
t('Delete'),
t('Cancel'));
}
?>
You can look here for examples of how core modules do it (have look at node_delete_confirm)
The simplest solution would be to use an existing module created for this purpose:
http://drupal.org/project/entityreference_prepopulate (for entity references)
http://drupal.org/project/nodereference_url (for node references)
http://drupal.org/project/prepopulate (for other form values)
You can configure which form values can be set from the URL, then rewrite the fields displayed in your table to generate the necessary links.
If the data are nodes, you can make the link node/%/delete where % is the nid. Drupal knows how to handle the delete page, as its a core path. Then, the delete confirmation follows the rest of the system and is very 'Drupal'.
I am not sure if this changed at all in Drupal 7, but this is what I did for countless modules.

Resources