2016-12-09 1 views
1

Drupal 8의 node.html.twig에서 사이드 바를 호출 할 수 있습니까? 그런Drupal 8 : node.html.twig의 사이드 바

뭔가 :

<article{{ attributes }}> 

    {{ title_prefix }} 
    {% if not page %} 
    <h2{{ title_attributes }}> 
     <a href="{{ url }}" rel="bookmark">{{ label }}</a> 
    </h2> 
    {% endif %} 
    {{ title_suffix }} 

    {% if display_submitted %} 
    <footer> 
     {{ author_picture }} 
     <div{{ author_attributes }}> 
     {% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %} 
     {{ metadata }} 
     </div> 
    </footer> 
    {% endif %} 

    <div{{ content_attributes }}> 
    {{ content }} 
    </div> 
<div{{ content_attributes }}> 
    {{ sidebar}} 
    </div> 

나는 각 노드에 대해 다른 사이드 바를 표시하고 싶습니다.

답변

2

현재 http://atendesigngroup.com/blog/making-region-content-available-node-templates-drupal-8 발견이 솔루션을 시도 할 수 있습니다 :

/** 
* Implements hook_preprocess_node() for NODE document templates. 
*/ 
function THEME_preprocess_node(&$variables) { 
    // Allowed view modes 
    $view_mode = $variables['view_mode']; // Retrieve view mode 
    $allowed_view_modes = ['full']; // Array of allowed view modes (for performance so as to not execute on unneeded nodes) 

    // If view mode is in allowed view modes list, pass to THEME_add_regions_to_node() 
    if(in_array($view_mode, $allowed_view_modes)) { 
    // Allowed regions (for performance so as to not execute for unneeded region) 
    $allowed_regions = ['primary_content']; 
    THEME_add_regions_to_node($allowed_regions, $variables); 
    } 
} 

/** 
* THEME_add_regions_to_node 
*/ 

function THEME_add_regions_to_node($allowed_regions, &$variables) { 
    // Retrieve active theme 
    $theme = \Drupal::theme()->getActiveTheme()->getName(); 

    // Retrieve theme regions 
    $available_regions = system_region_list($theme, 'REGIONS_ALL'); 

    // Validate allowed regions with available regions 
    $regions = array_intersect(array_keys($available_regions), $allowed_regions); 

    // For each region 
    foreach ($regions as $key => $region) { 

    // Load region blocks 
    $blocks = entity_load_multiple_by_properties('block', array('theme' => $theme, 'region' => $region)); 

    // Sort ‘em 
    uasort($blocks, 'Drupal\block\Entity\Block::sort'); 

    // Capture viewable blocks and their settings to $build 
    $build = array(); 
    foreach ($blocks as $key => $block) { 
     if ($block->access('view')) { 
     $build[$key] = entity_view($block, 'block'); 
     } 
    } 

    // Add build to region 
    $variables[$region] = $build; 
    } 
} 

내가 노드를 재정의이 훅을 사용합니다. node.html.twig에서 {{primary_content}}을 (를) 사용할 수 있습니다.

+0

대단히 감사합니다. 완벽하게 작동합니다. –