ADVERTISEMENTS

Steps to use hooks in Drupal 8 module or theme

Himanshu BatraNov 19, 2020
Steps to use hooks in Drupal 8 module or theme

Sometimes we got some different requirements from client side to implement with existing core and contributed modules. As per coding best practice, we always avoid to touch core & contributed modules & themes directly. So, fulfilling client's requirement, we use some methods which is called as hooks in drupal.

What is Hook ?

Hook is a simple method in PHP but in drupal we have some predefined structure of methods( hooks ) to interact with drupal core. Suppose, we have to change text of submit button of a form. For this scenario, we have a specific hook i.e. hook_form_alter in drupal 8.

ADVERTISEMENTS

Why should we have to use ?

For reaching goal of requirement with existing modules and themes in drupal, we need only hooks. We should have to use for avoiding complications in code. You can implement hooks either by creating a micro module or in theme file. Suppose you added a micro module and you are facing any issue after couple of months. You can easily uninstall your module and diagnose issue easily. This would works like plug and play feature.

Steps to implement hooks :

Here we are elaborating hooks implementaion steps over a form scenario which requires to add placeholder in every field of form and update submit button text.

Step 1: Initiate Micro Module

This is a initial step which we always prefer to do for any kind of alteration in existing functionality. In this step, we have to create directory with name i.e. etutorialz at path i.e. [DRUPAL_ROOT]/modules/custom/etutorialz. After that create two files in this directory.

  • Info file i.e. etutorialz.info.yml
  • Module file i.e. etutorialz.module

Define about your micro module in info file by adding this code.

name: Etutorialz Micro Module
description: This module if for adding custom hooks in existing drupal functionality
package: Custom
type: module
version: 1.0
core: 8.x

Step 2: Add Hook

After initializing micro module, we have to add code for altering existing form in drupal 8. We'll add this code in module file i.e. etutorialz.module

/**
 * @file
 * Micro Module for alter contact form.
 * hook_form_alter
 */
use Drupal\Core\Form\FormStateInterface;

function etutorialz_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  if($form_id == "contact_us_form") {
    // Adding Placeholder
    $form['name']['#attributes']['placeholder'] = 'Full Name';
	$form['email']['#attributes']['placeholder'] = 'Your Email Address';
	$form['message']['#attributes']['placeholder'] = 'Your message for us';
	// Update Submit Button Text
	$form['actions']['submit']['#value'] = 'Contact Us';
  }
}
ADVERTISEMENTS

Step 3: Install Module

Now, we just have to install this module from admin end i.e. Admin > Extend. You'll get your module under package "Custom" in this listing. You just have to checked that checkbox and click on "Install" button at bottom.

Step 4: Clear Cache

Sometimes, we got some cache issue after adding any module or theme. So, clearing cache after any code level changes would be a best practice in drupal. After clearing cache, we are ready with our custom micro module with implementations of hooks.

Above mentioned steps are quite enough for adding a hook in your drupal site to complete your tasks.

ADVERTISEMENTS