Drupal 8 - 如何在自定义模块中覆盖内容的模板?

时间:2015-11-26 18:36:53

标签: drupal-8

在查看了一堆模块后,我仍然感到困惑的是如何获取我创建的内容片(服务器),制作我自己的自定义模块(服务器)并做一些逻辑以便我可以通过那是一个服务器模板。

servers.info.yml

name: Servers Module
description: Creates a Servers Module
package: Servers
type: module
core: 8.x

servers.module

<?php
  function servers_theme(){
   $theme['server_page'] = [
      'variables' =>['name' => NULL],
      'template' => 'server',
    ];
    return $theme;
  }

我不确定这是否正确,但我的servers.routing.yml

servers.server:          #Pretty sure this part doesn't matter
  path: '/node/{server}' #I'd like to have /server/{server/ but my content's url is /node/xx
  defaults:
    _title: 'Servers'
    _controller: '\Drupal\servers\Controller\Servers::getServersImplementation'
    entity_type: 'server' #Not sure if this is correct
  requirements:
    _permission: 'access content' #Do I need this?

控制器

#located in servers/src/controller/Servers.php
namespace Drupal\Servers\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Controller for js_example pages.
 *
 * @ingroup js_example
 */
class Servers extends ControllerBase {
  /**
   * Accordion page implementation.
   *
   * We're allowing a twig template to define our content in this case,
   * which isn't normally how things work, but it's easier to demonstrate
   * the JavaScript this way.
   *
   * @return array
   *   A renderable array.
   */
  public function getServersImplementation($name)
  {
    return[
      '#theme'=>'server_page',
      '#name'=>$name,
    ];
  }
}

当我启用模块时,绝对没有任何显示。我知道它已启用,因为我可以将路由路径更改为节点id / node / 93,并且该节点将给我一个页面未找到错误,当没有路由时我可以获取内容。

error.log或最近的日志消息中没有错误。我在D8中对此很困惑,但是一旦我能够理解这个概念,我知道我将能够理解完成模块所需的一切。

提前致谢。

0 个答案:

没有答案