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

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
  • 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 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 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



Underscore vs. hyphen in URL. Which one to use?

The question in SEO(Search Engine Optimazation) URLs is whether to use a hyphen, or dash, (these-are-hyphens) or an underscore (these_are_underscores) to separate your words.

Does Google recognize both of them as word separators?

The short answer is that you should use a hyphen for your SEO URLs. Google treats a hyphen as a word separator, but does not treat an underscore that way.

Google treats and underscore as a word joiner — so mahtab_alam is the same as mahtabalam to Google. This has been confirmed directly by Google themselves, including the fact that using dashes over underscores will have a (minor) ranking benefit.

Again, SEO URLs should use hyphens to separate words. Do not use underscores, do not try to use spaces, and do not smash all the words together intoonebigword. As of 2012, dashes are still the best way to optimize your SEO URLs.

For example
instead of
when you create a new project or website

Subscribe for daily new posts