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.

PHP: How i enable PHP version 7.4 and disable PHP version 7.3

 In this PHP Tutorial, I will let you know how to install PHP version from 7.3 to 7.4 using command on your linux machine.

There are some cases in which you have to upgrade your PHP version but before upgrading to any version from previous one, you should be always aware from its incompatibilities.

There is very simple step that you need to follow to update your PHP version to newest.

First, You need to add the PPA ondrej/php which will provide the PHP 7.4 package and other required PHP extensions







After successfully adding the PPA, you can install any version of PHP 7.

sudo apt install php7.4


Now you can run following command to install commanly used php-extensions:

sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y


If you are using PostgreSQL for database connection then you need to install below extension :

sudo apt-get install php7.4-pgsql

If you have already installed any other version of PHP then you should disable that version first and then enable the current version of PHP that you want to use for your application by running following command:


sudo a2dismod php7.3

sudo a2enmod php7.4


Now restart your apache server by running below command:


sudo service apache2 restart

You are done with the steps and now you can check the PHP version by running following command:

php -v





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)   {   $...