You are here

protected function BlockVisibilityGroupedListBuilder::addGroupColumn in Block Visibility Groups 8

Add Column to show Visibility Group.

Parameters

array $form: The form array.

1 call to BlockVisibilityGroupedListBuilder::addGroupColumn()
BlockVisibilityGroupedListBuilder::buildBlocksForm in src/BlockVisibilityGroupedListBuilder.php
Builds the main "Blocks" portion of the form.

File

src/BlockVisibilityGroupedListBuilder.php, line 339

Class

BlockVisibilityGroupedListBuilder
Extends BlockListBuilder to add our elements only show certain blocks.

Namespace

Drupal\block_visibility_groups

Code

protected function addGroupColumn(array &$form) {
  $entity_ids = [];
  foreach (array_keys($form) as $row_key) {
    if (strpos($row_key, 'region-') !== 0) {
      $entity_ids[] = $row_key;
    }
  }
  $entities = $this->storage
    ->loadMultipleOverrideFree($entity_ids);
  if (!empty($entities)) {
    $labels = $this
      ->getBlockVisibilityLabels($this->group_storage);

    /** @var Block $block */
    foreach ($entities as $block) {
      if (!empty($form[$block
        ->id()])) {

        // Get visibility group label.
        $visibility_group = $this
          ->t('Global');
        $conditions = $block
          ->getVisibilityConditions();
        if ($conditions
          ->has('condition_group')) {
          $condition_config = $conditions
            ->get('condition_group')
            ->getConfiguration();
          if (isset($labels[$condition_config['block_visibility_group']])) {
            $visibility_group = '<strong>' . $labels[$condition_config['block_visibility_group']] . '</strong>';
          }
        }
        $row =& $form[$block
          ->id()];

        // Insert visibility group at correct position.
        foreach (Element::children($row) as $i => $child) {
          $row[$child]['#weight'] = $i;
        }
        $row['block_visibility_group'] = [
          '#markup' => $visibility_group,
          '#weight' => 1.5,
        ];
        $row['#sorted'] = FALSE;
      }
    }

    // Adjust header.
    array_splice($form['#header'], 2, 0, [
      $this
        ->t('Visibility group'),
    ]);

    // Increase colspan.
    foreach (Element::children($form) as $child) {
      foreach (Element::children($form[$child]) as $gchild) {
        if (isset($form[$child][$gchild]['#wrapper_attributes']['colspan'])) {
          $form[$child][$gchild]['#wrapper_attributes']['colspan'] = $form[$child][$gchild]['#wrapper_attributes']['colspan'] + 1;
        }
      }
    }
  }
}