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


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

$form element with #type => ‘submit’ with custom submit and validate function

$form[‘copy_local’] = [
‘#type’ => ‘submit’,
‘#prefix’ => ‘<br>’,
‘#name’ => ‘public’,
‘#value’ => $this->t(‘Copy local public files to S3’),
‘#validate’ => [
[$this, ‘copyLocalValidateForm’],
‘#submit’ => [
[$this, ‘copyLocalSubmitForm’],

public function copyLocalValidateForm(){

//Code Here


When we click on this submit button then the #validate is called first and the #submit.It will check for the custom method in same class and then call that method.






Drupal 8 userful functions in users

$role_names = user_role_names();

It returns array of all the user roles like anonymous,authenticated,admin.

To check whether a username exists or email exists then drupal 8 provide the below functions to check

$username = “Mahtab”

$hasUsername = user_load_by_name($username);

the $hasUsername will return true or false, the same we can check for email by

$hasEmail = user_load_by_mail($email);

To get the current user id

$user = \Drupal::currentUser();
$uid = $user->id();

How to build Drupal 8 theme with bootstrap as base theme

Building theme in drupal 8 is easy and you can easily build the base theme of bootstrap. Just follow the instructions below

First download the bootstrap theme from https://www.drupal.org/project/bootstrap

Then in bootstrap theme you will find one folder named as starterkits

Copy the less folder of starter kits to your theme directory where the bootstrap theme is placed

Rename the less folder as your themename lets suppose we change the name less as mahi

Rename all these files from the less directory

  • THEMENAME.libraries.yml to mahi.libraries.yml
  • THEMENAME.starterkit.yml to mahi.info.yml
  • THEMENAME.theme to mahi.theme
  • config/install/THEMENAME.settings.yml to config/install/mahi.settings.yml
  • config/schema/THEMENAME.schema.yml to config/schema/mahi.schema.yml

Now open mahi.info.yml and change the following

name: ‘Mahi





After this Go to the bootstrap official page and download the bootstrap source

Place it in your theme directory and rename as bootstrap

Next compile the less file which will create the style.css file.
lessc less/style.less > css/style.css from terminal going to your theme directory

Now go to the appearence tab in drupal and install and set as default to your theme

Enjoy it will start working. If any issue occurs comment below so i will solve the problems

Create a custom block programatically in drupal 8

Creating a block in drupal 8 is easy. Just follow the steps and you will be ready with a block

Go to your drupal 8 files and there is a module folder

In module folder create a folder named your module name. I am going to make a new folder named mahi and inside your module name you have to follow php PSR4 convention. So your file structure will be like modulename/src/Plugin/Block/filename.php.

The module file contains 2 important files one is modulename.info.yml and another is modulename.module

So it will look like this


Now open your .php file and add the following lines of code


namespace Drupal\mahi\Plugin\Block;
use Drupal\Core\Block\BlockBase;
* Provides a ‘mahi’ block.
* @Block(
*     id = “mahi_block”,
*     admin_label = @Translation(“Mahi Block”),
*     category = @Translation(“Custom article block example”)
* )
class MahiBlock extends BlockBase {
public function build() {
return array(
‘#type’ => ‘markup’,
‘#markup’ => ‘This block list the article.’,

Now open your .info.yml and write the following code

name: Mahi Block
description: A simple module for Block.
core: 8.x
package: Custom
– node
– block
type: module

Dont add anything in .module file

Now go to drupal-8 site and extend that module

Now go to block configuration and place your block at your choice

Refresh the site and your block will be displayed


Advantages of Drupal 8 over Drupal 7 or earlier version

If you are a Drupal 7 developer and want to make a way to develop your sites on Drupal 8 then there are many benefits of Drupal 8 over Drupal 7 which is listed below

Drupal 8 has brought a lot of changes over Drupal 7. The most important part in using drupal 8 is that it is built on Symfony framework. Many contributed modules has been added to core in drupal 8

Here are the list of Drupal 8 new features


  • Views – In earlier version of drupal views was in contributed module but in drupal 8 it is in core Module.
  • Mobile Friendly – Drupal 8 is mobile friendly. The default themes ( Bartik, Seven and Stark ) uses responsive design. Responsive design means that website can change the layout based on size and capabilities of the device.
  • Object Oriented Programming – Drupal 8 uses Object oriented Programming concept which is based on Symfony framework used in drupal 8. This makes the code easy to maintain, scale and reuse.
  • Template Change – In earlier version of Drupal the template files were using PHP Template as the default templating engine and the extension was .tpl.php but there are a major change in templating part. Drupal 8 uses Twig which is a template engine for PHP and it is also part of the Symfony 2 framework. So all the template file in drupal 8 is saved with .html.twig
  • Multilingual – Drupal 8 also supports auto detection of languages and auto downloading latest interface translations.
  • Inline Editor – If you want to edit the content in the body by clicking on the edit content option . You can easily edit the content of the page without going to the edit page.
  • New Field types added –
  • Email Field
  • Telephone Field