Созонов Алексей

Веб-разработчик

Создать модуль drupal 8

09.08.2015

1) Модули теперь хранятся в папке modules

2) Вместо .info теперь используется .info.yml.

Пример:

name: Foobar
type: module
description: 'This is a demo for creating Drupal 8 modules.'
package: Custom
version: 8.x-1.0
core: 8.x
dependencies:
  - node
  - block
hidden: false // Позволяет скрыть модуль в списке всех модулей

Более подробно о .info.yml

3) MVC - архитектура

Создаем файл modules/foobar/lib/Drupal/foobar/Controller/FoobarController.php

<?php

namespace Drupal\foobar\Controller;

use Drupal\Core\Controller\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class FoobarController implements ControllerInterface {

  public static function create(ContainerInterface $container) {
    return new static($container->get('module_handler'));
  }

  /**
   * This will return the output of the foobar page.
   */
  public function foobarPage() {
    return array(
      '#markup' => t('This is the demo foobar page.'),
    );
  }

}

4) Маршрутизация

Создаем файл modules/foobar/foobar.routing.yml

foobar:
  pattern: 'admin/foobar'
  defaults:
    _content: '\Drupal\foobar\Controller\FoobarController::foobarPage'
  requirements:
    _permission: 'access administration pages'

Более подробно о маршрутизации (New Symfony-based routing system)

5) Добавляем menu item в foobar.module

<?php

/**
 * Implements hook_menu().
 */
function foobar_menu() {
  // The paths given here need to match the ones in foobar.routing.yml exactly.
  $items['admin/foobar'] = array(
    'title' => 'Foobar',
    'description' => 'This is the demo foobar page.',
    // The name of the route from foobar.routing.yml
    'route_name' => 'foobar',
  );
  return $items;
}

6) После того, как вы включите модуль, у вас появится элемент меню Foobar, который открывает страницу с текстом “This is the demo foobar page.”

#Drupal 8