2016-09-23 68 views
0

我想我的自定義Drupal 8模塊爲與某個URL匹配的頁面強制使用不同的模板。這裏是我的模塊結構:如何從Drupal 8中的模塊更改節點模板?

/sass_edit 
    /css 
    /js 
    /template 
     builder_template.html.twig // this is the template file 
    sass_edit.module 
    ... 
    sass_edit.*.yml // various yml files 

這些都是builder_template.html.twig內容:

<h1>test</h1> 

這裏是代碼中的相關行我.module文件:

function sass_edit_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { 

    $current_path = \Drupal::service('path.current')->getPath(); 

    if(\Drupal::currentUser()->isAuthenticated() && strpos($current_path, "/builder/") >= 0) { 
    $suggestions[] = 'builder_template'; 
    } 

    // kint($suggestions); 
    // die(); 
} 

當我參觀頁面的URL包含/builder/,代碼將運行並將字符串builder_template添加到$suggestions陣列。但是,當頁面呈現時,新模板將被忽略。我曾嘗試沖洗緩存,但沒有結果。

我不確定如何繼續。我發現的所有文檔都是從定製主題添加主題建議,而不是自定義模塊。

任何想法我做錯了什麼?非常感謝你的幫助!

+0

您是否啓用了Twig Debug?嘗試$ suggestions [] = $ hook。 '__'。 'builder_template'; 枝條引擎有時會出現掛鉤問題。 –

回答

1

也許您在粘貼的代碼中存在拼寫錯誤,但您的模板覆蓋文件夾應爲templates而不是您編寫的template。不要忘記在更改目錄名稱後刷新緩存;)