Monday, May 22, 2017

Create custom module in Drupal 8 via Drupal console

Generate module 
 drupal generate:module - It will ask the module name and basic information.

Give the module a name of module (welcome) and Enter module description, path and basic information. Then drupal will create the below files under the your custom module.

composer.json
welcome.info.yml
welcome.module

Install module

The module now exists but it is not enabled. You don’t need to go to the module page to enable it, you can install and enable the welcome module using the module:install command:

drupal module:install welcome

This will install the module and automatically rebuild the cache.

Monday, April 11, 2016

List out CCK field allowed values in Drupal

List out CCK field allowed values in Drupal

$field = field_info_field('field_machine_name');
$allowed_values = list_allowed_values($field);

This will return the allowed values list as array.

Usage:
field_info_field - Load the field instance by using field machine name
list_allowed_values - List out the allowed values

Tuesday, March 1, 2016

Remove the password hint details in drupal register page

You can use the below code remove the password hint details in drupal register page.

/**
 * Implements hook_element_info_alter().
 */
function HOOK_element_info_alter(&$types) {
  if (isset($types['password_confirm']['#process']) && (($position = array_search('user_form_process_password_confirm', $types['password_confirm']['#process'])) !== FALSE)) {
    unset($types['password_confirm']['#process'][$position]);
  }
}

Note: Please clear form cache after adding

Remove the user tab link in user login page and user register page

We can use the below code to remove the user tab link in user login page and user register page.

/**
 * Implementation of hook_menu_alter().
*/
function HOOK_menu_alter(&$items) {
  $items['user/login']['type'] = MENU_CALLBACK;
  $items['user/register']['type'] = MENU_CALLBACK;
  $items['user/password']['type'] = MENU_CALLBACK; 
}

Note: Please clear the menu cache after adding this function

Monday, July 6, 2015

Programmatically create view mode for product in Drupal Commerce

1. First create the view mode by using the following hook or Entity View Mode module. 

2. Then create the template file commerce-product--product--{view-mode}.tpl.php
3. Inside the template file, you can render what ever you want.
4. Then clear the cache.