2016-11-11 121 views
1

我終於潛入Drupal 8的一個項目。在我的模塊中,雖然我似乎無法確定如何根據路線呈現模塊。Drupal 8 hook_menu()渲染hook_theme()

在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' 
     ) 
); 
} 

然後我會在模板的模板文件夾

使用Drupal 8我to about here:

custom.routing.yml

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

的src /控制器/ CustomController.php

namespace Drupal\custom\Controller; 

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

和所有偉大的前往路線的作品。但我似乎無法弄清楚爲我的hook_menu創建一個hook_theme函數來用作回調函數。

回答

1

想通了

添加custom.module

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

    return $theme; 
} 
在我的控制器

更換'#markup'有:

'#theme' => 'home_page'