You are here

public function ContextGroupsBlockPageVariant::build in Context groups 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/DisplayVariant/ContextGroupsBlockPageVariant.php \Drupal\context_groups\Plugin\DisplayVariant\ContextGroupsBlockPageVariant::build()

File

src/Plugin/DisplayVariant/ContextGroupsBlockPageVariant.php, line 55

Class

ContextGroupsBlockPageVariant
Provides a page display variant that decorates the main content with blocks.

Namespace

Drupal\context_groups\Plugin\DisplayVariant

Code

public function build() {
  $build = parent::build();
  $rendered_context_groups = $this
    ->getNonEmptyContextGroups($build);

  // Get current context.
  $contexts = $this->contextManager
    ->getActiveContexts();
  foreach ($contexts as $context) {
    $groups = $context
      ->getThirdPartySettings('context_groups');

    // Add context groups to build array.
    foreach ($groups as $key => $data) {

      // If group has no content, don't display it.
      if (!in_array($key, $rendered_context_groups)) {
        continue;
      }
      if (empty($data['parent'])) {

        // If group has no parents.
        $old_data = isset($build[$data['region']][$key]) ? $build[$data['region']][$key] : [];
        $new_data = [
          '#theme' => 'context_groups',
          '#attributes' => [
            'class' => [
              'context-groups context-groups-' . $key . ' ' . $data['class'],
            ],
          ],
          '#weight' => $data['weight'],
          '#context_id' => $context
            ->getName(),
          '#context_group' => $key,
        ];
        $group_content = array_merge_recursive($old_data, $new_data);
        $build[$data['region']][$key] = $group_content;
      }
      else {

        // If group has parents.
        $build_parent =& $build[$data['region']];
        foreach ($data['all_parents'] as $value) {
          $build_parent =& $build_parent[$value];
        }
        $old_data = isset($build_parent[$key]) ? $build_parent[$key] : [];
        $new_data = [
          '#theme' => 'context_groups',
          '#attributes' => [
            'class' => [
              'context-groups context-groups-' . $key . ' ' . $data['class'],
            ],
          ],
          '#weight' => $data['weight'],
          '#context_id' => $context
            ->getName(),
          '#context_group' => $key,
        ];
        $merge = array_merge_recursive($old_data, $new_data);
        $build_parent[$key] = $merge;
      }
    }
  }

  // Move blocks inside context groups.
  foreach (Element::children($build) as $region_name) {
    foreach ($build[$region_name] as $key => $block) {

      // If block is not a context group, edit his position if needed.
      if (isset($block['#type']) && $block['#type'] == 'container') {
        continue;
      }

      // If block has a parent, move it.
      if (!empty($block['#configuration']['all_parents'])) {
        $build_parent =& $build[$region_name];
        foreach ($block['#configuration']['all_parents'] as $value) {
          $build_parent =& $build_parent[$value];
        }

        // Place block in correct context group.
        $build_parent[$key] = $block;

        // Unset original block.
        unset($build[$region_name][$key]);
      }
    }
  }
  return $build;
}