Drupal 8 从自定义模块渲染自定义主题

Drupal 8 render custom theme from custom module

当 Drupal 8 刚出来的时候,这对我来说一直很有效。但是,这似乎不再起作用并且我收到错误消息。 Drupal 文档一直很糟糕,因此没有解决方案。

custom.module

<?php

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

    return $theme;
}

function custom_menu(){
  $items = array();
  $items['admin/config/system/custom'] = array(
    'title' => 'Custom',
    'description' => 'Configuration Custom',
    'route_name' => 'custom.settings'
  );
  return $items;
}

custom.routing.yml

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

src/Controller/RoutingController.php

<?php

namespace Drupal\custom\Controller;

class RoutingController {
    public function home(){
        return array(
            '#title' => 'Home',
            '#theme' => 'home_page'
        );
    }

}

home_page.html.twig

<main>
   <!-- some markup -->
</main>

你的控制器没有扩展基本控制器class问题一
试试这个

namespace Drupal\custom\Controller;

use Drupal\Core\Controller\ControllerBase;

class RoutingController extends ControllerBase{
    public function home(){
        return array(
            '#title' => 'Home',
            '#theme' => 'home_page'
        );
    }

}

home_page.html.twig

<main>
   <!-- some markup -->
   {{ content }}
</main>

也尝试用路径扩展你的主题挂钩

'path' => drupal_get_path('module', 'custom') . '/templates',

并将模板树枝文件放入 module/templates 文件夹