Get the configuration setting in drupal 8

In drupal 8 we can get any of the configuration setting

$config =\Drupal::service(‘config.factory’)->get(‘module.settings’);
$form_mode_settings = $config->get(‘key’);

This will get you an array of your configuration settings

Advertisements

Useful Hg Commands for source code Management

  1. hg st – shows the changed file
  2. hg history – shows the changed history
  3. hg history | head -40 – shows the changed history for last 40 changes
  4. hg branch – shows the current branch
  5. hg branches – shows all the branches in reprository
  6. hg update -C -Reset the changes and reflect same as parent directory
  7. hg pull –update – Pull the latest code and update
  8. hg commit -m “Message” – Does the commit
  9. hg push – Push the code the current branch
  10. hg addremove – Its a good practice to use this as it helps in not conflicting
  11. hg revert –no-backup filename – Reverts the changes which we have done in that file
  12. hg diff filename – shows the difference of file

Steps to do initial push from local to Reprository with hg mercurial

  • first write on hg st on your project directory, it will show the file which has got the changes
  • Then write hg commit -m “Initial Commit”
  • Then write¬† hg pull –update
  • Then write hg push
  • This will push all your changes to reprository.
  • If you want to revert the changes the use hg revert –no backup and then the directory of the filename

 

Age Validation with dob in Drupal 8

I was been given a situation where there was 2 fields one was cateogry and another was date-of-birth.

So the cateogry was dropdown consists of 4 options OBC ,General ,ST and SC.

The age should be between 35 to 45 for General and OBC and 35 to 48 for ST and SC.

So i have written some code in drupal 8 for age validation.

created a .module file and witten the below code which validates

<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function modulename_form_alter(&$form, FormStateInterface $form_state, $form_id) {
 if($form_id == "your-form-id"){
     $form['#validate'][] = 'dob_form_validate';
 }
}

function dob_form_validate($form, FormStateInterface &$form_state) {
 $dob = $form_state->getValue('field_date_of_birth');
 if(isset($dob)){
   $birthDate = $dob[0]['value']->format('Y-m-d');
   $birthDate = new DateTime($birthDate);
   $to = new DateTime('2016-01-31');
   $age = $birthDate->diff($to);
   $years = $age->y;
   $months = $age->m;
   $days = $age->d;
 }
 $cateogry = $form_state->getValue('field_ap_category');
 if(isset($cateogry)){
  $cat = $cateogry[0]['value'];
  if($cat == "BC 1" || $cat == "BC 2" || $cat == "General"){
    if($years == 45 && ($months > 0 || $days>0))
    {
      $form_state->setErrorByName('field_ap_date_of_birth', t('Age must be between 35 and 45 for General and BC'));
    }
    else if($years < 35 || $years > 45 ){
      $form_state->setErrorByName('field_ap_date_of_birth', t('Age must be between 35 and 45 for General and BC'));
    }
  }
  else if($cat == "SC" || $cat=="ST"){
    if($years == 48 && ($months > 0 || $days>0))
    {
      $form_state->setErrorByName('field_ap_date_of_birth', t('Age must be between 35 and 48 for SC and ST'));
    }
    else if($years < 35 || $years > 48){
      $form_state->setErrorByName('field_ap_date_of_birth',t('Age must be between 35 and 48 for SC and ST'));
    }
  }
 }
}

Custom Submit handler in drupal 8

If we want to create our  custom submit handler to submit a form then we can easily use the below code

use Drupal\Core\Form;
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id){
  if($form_id == 'node_newmodule_form'){
    foreach (array_keys($form['actions']) as $action) {
     if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
       $form['actions'][$action]['#submit'][] = 'custom_form_submit';
     }
    }
  }
}

public function custom_form_submit(&$form, FormStateInterface $form_state){
   echo "It will invoke";
}

So we can easily choose which form id to invoke this submit handler.
The custom function custom_form_submit will be envoke when we submit the following form id

How to transfer file from local to remote with sftp

We can transfer file from local to remote with sftp

From Terminal we write

sftp username@ip_address

for example my system username is mahtabalam and

for ip we can get by writing ‘ifconfig’ in terminal.

sftp mahtabalam@192.168.0.206

then it will ask for password. Enter the password

then it will come as

sftp>

To transfer file

sftp> put /var/www/html/index.php /var/www/html

So it will copy the localfile to remote

Drupal 8 Hooks hook_element_info_alter()

There are many hooks in drupal 8

So i will list the most important hooks which i have used so far

hook_element_info_alter()

Alter the element type information returned from modules.

A module may implement this hook in order to alter the element type defaults defined by a module.

example code:

function hook_element_info_alter(&$type) {
  // Decrease the default size of textfields.
  if (isset($type['textfield']['#size'])) {
    $type['textfield']['#size'] = 40;
  }
}

function mymodule_element_info_alter(array &$types) {
  //call the custom function after build.
  foreach ($types as $type => $info) {
    $types[$type]['#after_build'][] = 'my_custom_function';
  }
}

This hook is mainly used to alter the elements so $type contains all the elements.

 

Important Services in drupal 8

$contentTypes = \Drupal::service('entity.manager')->getStorage('node_type')->loadMultiple();

It will list all the content types of your current project.
It is very useful to call service(‘entity.manager’);

$type = $contentTypes->label();

This will get the Name of content type.

$type = $contentTypes->id();

This will get the slug of content type. Means the content type machine name

$form_modes = \Drupal::service('entity_display.repository')->getFormModes('node');

This will get the list of all content type form mode in current project