How to create custom extension/module in Drupal 8

How to create custom extension/module in Drupal 8

Let create a content gallery module then here are the steps to create that module.
Create a folder for your module

Let the machine name of our module is "mncplanet_gallery" then creating a folder
within your Drupal installation at the path: /modules/custom/mncplanet_gallery or /sites/all/modules/mncplanet_gallery.
You may omit/custom/subfolder and place your module to /modules/mncplanet_gallery folder, but often it's good idea
to have a dedicated place for your own modules, so you'll not have to search for them among other ones,
downloaded from

| mncplanet_gallery.routing.yml
| |-src
| | |-Controller
| | | MncplanetGalleryController.php
| | |-Plugin
| | | |-Block
| | | | MncGalleryBlock.php

Define the module information in .info.yml
A .info.yml file is an essential part of a Drupal 8 module that store metadata about the project.

name: ' Content gallery'
type: module
description: 'create dynamic gallery for particular content type'
package: custom
core: 8.x

Add a routing file
Let define the controller and route in this file.

  path: /mncplanet/gallery
    _controller: Drupal\mncplanet_gallery\Controller\MncplanetGalleryController::gallery
    _permission: 'access content'

Adding a controller
The gallery() function in the MncplanetGalleryController class will be returning the markup text,
when the routing system invokes the page.


namespace Drupal\mncplanet_gallery\Controller;
use Drupal\Core\Controller\ControllerBase;

Class MncplanetGalleryController {

  public function gallery()
  return array(
       '#title' => 'Mncplanet Gallery',
        '#markup' => 'This is some content'


The MncGalleryBlock.php file is used to create a gallery that show as block in drupal


namespace Drupal\mncplanet_gallery\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

class MncGalleryBlock extends BlockBase implements BlockPluginInterface {

   * {@inheritdoc}
  public function build() {
    // static or dynamic code to create gallery
    $slide ='';
    $slide .= '';

    return array(
      '#markup' => $this->t($slide),