Drupal 7 : How to add meta tags programmatically to custom pages in Drupal

 Using hook_html_head_alter() or using hook_preprocess_html() we could add Meta tags and title.

Sample code for adding meta tag description are below , put code in MYTHEME/template.php. file


function MYTHEME_html_head_alter(&$head_elements) {
  $head_elements['metatag_description'] = array(
      '#theme' => 'metatag',
      '#tag' => 'meta',
      '#id' => 'metatag_description',
      '#name' => 'description',
      '#value' => 'Description for meta-tags',
      '#type' => 'html_tag',
    );
}

Or you can use the following code

function MYTHEME_preprocess_html(&$vars) {
  $metatag_description = array(
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'description',
        'content' => 'Description for meta_tags',
      )
    );
    drupal_add_html_head($metatag_description, 'description');
}

Drupal 7 : Removing create new account , Log in and Request new password tab from login , password reset and user registration page

If you want to remove the Log in , Registration and password reset tab from user, registration and password reset page, and you want to above these url is accessible , write below code in your custom module.









function custom_code_menu_alter(&$item) {
  // Hide the "Create new account" tab
  $item['user/register']['type'] = MENU_CALLBACK;
  $item['user']['type'] = MENU_CALLBACK;
  $item['user/password']['type'] = MENU_CALLBACK;
}





Drupal 7 : How to add role selection field to the registration form

 If any module doesn't fit your requirement then you can use the below code You can create any custom module and put code in .module file.

function custom_code_form_alter(&$form, &$form_state, $form_id) {
	 
	
	if($form_id == 'user_register_form') {
		
		$form['account']['roles']['#title'] = t('User type');
		$form['account']['roles']['#access'] = TRUE;
		$form['account']['roles']['#required'] = TRUE;
		$form['account']['roles']['#type'] = 'select';
		foreach ($form['account']['roles']['#options'] as $key => $value) {
			if($value == 'administrator') {
				unset($form['account']['roles']['#options'][$key]);
			}
		}
		
		
	}
		
		
		
}
function custom_code_user_presave(&$edit, $account, $category) {
  if (isset($edit['roles'])) {
    is_array($edit['roles']) ? '' : $edit['roles'] = array ( $edit['roles'] => $edit['roles'], 2 => '1' );
    $edit['roles'] = array_filter($edit['roles']);
  }
}



 

Drupal 7 : How to use HTML5 datalists for autocomplete

 I show you how to use HTML 5 datalist in drupal 7 form step by step


Step 1

           Create a custom module , in .module file use Hook form alter, in my case module name is "custom_code". So My custom_code.module file are below.



function custom_code_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'inventory_node_form') {
                  global $user;
		  $curtuid = $user->uid;
                
               $quer = db_select('node', 'ne');
		$quer->fields('ne', array('nid'));
		$quer->condition('ne.uid', $curtuid);
		$quer->condition('ne.status', 1);
		$quer_re = $quer->execute();
		foreach ($quer_re as $value_res) {

		$data2[] = $value_res->nid;
		}

                if(!empty($data2)) {
		$quer1 = db_select('field_data_field_room', 'nr');
		$quer1->fields('nr', array('field_room_value'));
		$quer1->condition('nr.entity_id', $data2, 'IN');
		$quer12 = $quer1->execute();
		foreach ($quer12 as $value_res1) {

		$data21[] = $value_res1->field_room_value;
		}
                $form['room_val_hid'] = array(
		'#type' => 'hidden',
		'#attributes' => array(
		                       'id' => 'roomhiddenval',
		                      ),
		'#value' => $data21,
		 );
  }
} 


Add Hidden form field with hook form alter and assign  autocomplete value to hidden form.

after this we create a .js file 


Step 2

jQuery('#inventory-node-form #edit-field-room-und-0-value').attr("list","hidennroomid");
jQuery('#inventory-node-form #edit-field-room-und-0-value').after('<datalist id="hidennroomid"></datalist>');
	 var hinndenroomval = jQuery("#roomhiddenval").val();
	 var res = hinndenroomval.split(" ");
	 var length = res.length;
	 //console.log(res);
	 //console.log(length);
	 for (i = 0; i <=length-1; i++) {
		 
		 jQuery("#hidennroomid").append("<option value='" + 
                res[i]+ "'></option>");
		 
		// console.log([res[i]]);
	 }


with Javascript first assign list attribute to field , in my case field id is "inventory-node-form #edit-field-room-und-0-value". the add datalist after filed.

Kindly see above code and its work for me.

Thanks







Drupal 7 : How to add custom validation to an existing Drupal node form

 Form validation is an essential part of any web system. You need to ensure that the user has added valid data and if not, show them a meaningful error message. Validation functions are normally implemented in the module where the form is defined. However, thanks to Drupal’s hook system, you can add a validation function to any Drupal form, even if you did not create the form.


Alter the form

 

To add the new validation function, you need to alter the form. You can do this by implementing hook_form_alter().

function custom_code_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'registration_form_node_form') {
    $form['#validate'][] = 'starting_registraion_email_validate';
  }
} 


In the above code, we are first checking that the form id is registration_form_node_form.

$form['#validate’] is a form element which lists all of the validation functions for a given form.

Create the validation function

You need to add your validation function to that array by adding the following: $form['#validate'][] = ‘starting_registraion_email_validate’. $form[‘validate’] is an array and you can add to that with $form['#validate'][].


function starting_registraion_email_validate(&$form, &$form_state, $form_id) {
        $valss = $form_state['input']['field_email_mem_info']['und'][0]['email'];
	$query11 = db_select('field_data_field_email_mem_info', 'emaild');
	$query11->fields('emaild', array('bundle'));
	$query11->condition('emaild.field_email_mem_info_email', $valss);
	$result11 = $query11->execute();
	$num_of_results = $result11->rowCount();
	if($num_of_results > 0) {
		form_set_error('Email', t('Email id already register'));
	}
} 


You will see there are now two validation handlers, the core node_form_validate and the one that you added, starting_registraion_email_validate.

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...