Custom module development in Drupal 8

(@mbovan)

MiloŇ° Bovan

  • Drupal.org (@mbovan)
  • Drupal 8 Developer

Overview

  • Create a simple module
  • Skeleton
  • Routes and controllers
  • Menu items, local tasks, local actions
  • Custom permissions
  • Services
  • Plugin API
  • Blocks

How to create a module?

  • demo.info.yml
name: Drupal 8 Demo
description: 'Drupal 8 demo module'
type: module
core: 8.x
name: Drupal 8 Demo
description: 'Drupal 8 demo module'
type: module
core: 8.x

dependencies:
  - node
test_dependencies:
  - test_module

configure: configure.route
  • composer.json
{
    "name": "drupal/demo",
    "description": "This is an example composer.json for demo module.",
    "type": "drupal-module",
    "license": "GPL-2.0+"
}

How to create a module?

Directory structure

Routing

  • Symfony2's HttpKernel, HttpFoundation, and related libraries
  • \Drupal::request()

Routing

Drupal 8 Docs

  • demo.routing.yml

Routing

demo.route:
  path: '/demo'
  defaults:
    _controller: '\Drupal\demo\Controller\DemoController::demo'
    _title: 'Demo'
  requirements:
    _permission: 'access content'
demo.route:
  path: '/demo'
  defaults:
    _controller: '\Drupal\demo\Controller\DemoController::demo'
    _title: 'Demo'
  requirements:
    _permission: 'access content'

demo.param:
  path: '/admin/content/demo/{node}'
  defaults:
    _controller: '\Drupal\demo\Controller\DemoController::param'
    _title: 'Demo'
  requirements:
    _permission: 'access content'
  • src/Controller/DemoController.php

Routing

<?php

namespace Drupal\demo\Controller;

class DemoController {

  public function demo() {
    return ['#markup' => t('Demo')];
  }

}
<?php

namespace Drupal\demo\Controller;
use Drupal\node\NodeInterface;

class DemoController {

  public function demo() {
    return ['#markup' => t('Demo')];
  }

  public function param(NodeInterface $node) {
    return ['#markup' => $node->label()];
  }

}

Menu links

  • demo.links.menu.yml
demo.route:
  title: 'Demo menu item'
  parent: system.admin_structure
  description: 'Provides a link to the demo route.'
  route_name: demo.route

Local tasks

  • demo.links.task.yml
demo.task:
  title: Demo task
  route_name: demo.route
  base_route: system.admin_content

Local actions

  • demo.links.action.yml
demo.action:
  route_name: demo.route
  title: Add demo item
  appears_on:
    - system.admin_content

Custom permissions

  • demo.permissions.yml
administer demo:
  title: Administer Demo

Services

  • demo.services.yml
services:
  demo.service:
    class: Drupal\demo\DemoService
services:
  demo.service:
    class: Drupal\demo\DemoService
    arguments: ['@entity_type.manager']

Custom blocks

  • Rely on Plugin API
  • src/Plugin/Block/DemoBlock.php
<?php

namespace Drupal\demo\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Demo block.
 *
 * @Block(
 *   id = "demo_block",
 *   admin_label = @Translation("Demo block"),
 * )
 */
class DemoBlock extends BlockBase {
  
  public function build() {
    return ['#markup' => $this->t('Demo block')];
  }
}

Summary

  • Built a simple module
  • Routes handling
  • Code samples (items, permissions, blocks...)
  • bit.ly/d8-demo (Demo module Github repository)

  • bit.ly/d8-demo-slides (Slides)

  • drupal.org/docs/8

  • api.drupal.org/api/drupal/8

RESOURCES

Thank you!

Questions

& ANSWERS

@mbovan

Custom module development in Drupal 8

By mbovan

Custom module development in Drupal 8

  • 61
Loading comments...

More from mbovan