Monday, June 30, 2014

Open a menu link in new window

In Drupal clicking on the menu links will open in the same window. if we want to open the menu links in new window or new tabs, use 'hook_menu_link_alter',

eg:
  1. function mymodule_menu_link_alter(&$item) {
  2.     if ($item['link_path'] == 'node/10') {
  3.       $item['options']['attributes']['target'] = '_blank';
  4.     }
  5. }

Monday, April 7, 2014

Send a notification mail to custom mail id on user registration

When a user registered a notification mail sent to that users mail id when it is set to admin approval is required. If you want to send a notification mail to the admin mail or a custom mail do the following,


Go to /admin/config/system/actions. In the section "Create an advanced action" choose "send an email" and click on "create". It takes you to a page where you need to enter a recipients email address (Enter the Admin email or your custom email address here), subject and Message, then click save.


Now go to /admin/structure/trigger/user (if you not enabled Trigger module please enable it). In the Section "Trigger: After creating a new user account" Choose "Send e-mail" in the select box. Click Assign.

Saturday, March 15, 2014

Alter username in drupal when using email registration

The username field in drupal registration form in not needed when we add our custom fields to capture user name, like First name, Last name,. . So to remove the username field from registration form i used the email_registration module. This module not only remove the username field from registration form but also gives us the feature to login using either username or email address. The problem when using email registration is, a username is generated and assigned based on the username part of the email address.
i.e., if my email id is chris@example.com, my username is set to chris. It is ok when the name is reasonable one. If the email is like iam123@example.com, the username is set to iam123 which is so annoying.

So to change the username displayed for user drupal gives us function hook_username_alter.

  1. function your_module_username_alter(&$name, $account) {
  2.     $user = user_load($account->uid);
  3.     if (!empty($user->field_first_name['und'][0]['value']) && !empty($user->
  4. field_last_name['und'][0]['value'])) {
  5.         $name = $user->field_first_name['und'][0]['value'] ." " . $user->
  6. field_last_name['und'][0]['value'];
  7.     }
  8.     else {
  9.         $name = $user->mail;
  10.     }
  11. }

Wednesday, January 29, 2014

Rename '- None -' option from select list in a webform

When we creating a webform with select options, it gives the list with our options and '-None-' as the default empty value. If i choose it as a mandatory field it changes to '-Select-'. Recently a client of mine wants me to rename the '-None-' as '-Please Select-'. There is no way you can do that with the webform module. There comes an idea of implementing hook_form_alter.

  1. function yourmodule_form_alter(&$form, &$form_state, $form_id) {
  2.   if ($form_id == 'webform_client_form_your_webform_id') {
  3.       $form['submitted']['component_name']['#default_value'] = '';
  4.       $form['submitted']['component_name']['#empty_value'] = '';
  5.       $form['submitted']['component_name']['#empty_option'] = '- Please Select -';
  6. }

Thats it!!