Drupal 8 hook_menu () для рендеринга hook_theme ()

Я наконец погрузился в Drupal 8 для проекта. Однако в моем модуле я не могу понять, как визуализировать шаблон из моего модуля на основе маршрута.

В Drupal 7 я бы обычно делал это

custom.module

function union_views_menu() {
    $items = array();

    $items['home'] = array(
        'title' => 'Home',
        'description' => 'home apge',
        'page callback' => 'home_page',
        'access arguments' => array( 'access content'),
        'type' => MENU_CALLBACK
    );
    return $items;
}

function home_page() {
    return theme('home_page');
}

function union_views_theme(){
    return array(
        'home_page'     => array(
            'template'  => 'templates/home-page'
        )
  );
}

И тогда у меня был бы шаблон в папке templates

С Drupal 8 я примерно здесь:

custom.routing.yml

custom:
    path: /home
    defaults: 
        _controller: Drupal\custom\Controller\CustomController::custom
    requirements:
        _permission: 'access content'

src / Controller / CustomController.php

namespace Drupal\custom\Controller;

class CustomController {
    public function custom(){
        return array(
            '#title' => 'Custom Theme',
            '#markup' => 'This is a content.'
        );
    }
}

И все работает отлично, чтобы добраться до маршрута. Но я не могу понять, как создать функцию hook_theme для моего hook_menu, чтобы использовать ее в качестве обратного вызова.


person NicholasByDesign    schedule 11.11.2016    source источник


Ответы (1)


Догадаться

Добавить custom.module

function custom_theme() {
    $theme['home_page'] = [
        'variables' => ['name' => NULL],
        'template' => 'home_page'
    ];

    return $theme;
}

в моем контроллере заменил '#markup' на:

'#theme' => 'home_page'
person NicholasByDesign    schedule 11.11.2016