You are here

class BlockVisibilityGroupedListBuilder in Block Visibility Groups 8

Extends BlockListBuilder to add our elements only show certain blocks.

Hierarchy

Expanded class hierarchy of BlockVisibilityGroupedListBuilder

File

src/BlockVisibilityGroupedListBuilder.php, line 21

Namespace

Drupal\block_visibility_groups
View source
class BlockVisibilityGroupedListBuilder extends BlockListBuilder {
  use BlockVisibilityLister;
  use ConditionsSetFormTrait;

  /**
   * Used in query string to denote blocks that don't have a group set.
   */
  const UNSET_GROUP = 'UNSET-GROUP';

  /**
   * Used in Query string to denote showing all blocks.
   */
  const ALL_GROUP = 'ALL-GROUP';

  /**
   * The entity storage class for Block Visibility Groups.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $group_storage;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a new BlockVisibilityGroupedListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   The theme manager.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   * @param \Drupal\Core\Entity\EntityStorageInterface $block_visibility_group_storage
   *   The group entity storage.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state system service.
   * @param \Drupal\Core\Messenger\MessengerInterface|null $messenger
   *   The messenger service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ThemeManagerInterface $theme_manager, FormBuilderInterface $form_builder, EntityStorageInterface $block_visibility_group_storage, StateInterface $state, MessengerInterface $messenger) {
    parent::__construct($entity_type, $storage, $theme_manager, $form_builder, $messenger);
    $this->group_storage = $block_visibility_group_storage;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('theme.manager'), $container
      ->get('form_builder'), $container
      ->get('entity_type.manager')
      ->getStorage('block_visibility_group'), $container
      ->get('state'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $group_options = $this
      ->getBlockVisibilityGroupOptions();
    $default_value = $this
      ->getCurrentBlockVisibilityGroup();
    $current_block_visibility_group = NULL;
    if (!in_array($default_value, [
      BlockVisibilityGroupedListBuilder::ALL_GROUP,
      BlockVisibilityGroupedListBuilder::UNSET_GROUP,
    ])) {
      $current_block_visibility_group = $default_value;
    }
    $options = [];
    foreach ($group_options as $key => $group_option) {
      if ($default_value == $key) {
        $default_value = $group_option['path'];
      }
      $options[$group_option['path']] = $group_option['label'];
    }
    $form['block_visibility_group'] = [
      '#weight' => -100,
    ];
    $form['block_visibility_group']['select'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Block Visibility Group'),
      '#options' => $options,
      '#default_value' => $default_value,
      // @todo Is there a better way to do this?
      '#attributes' => [
        'onchange' => 'this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value)',
      ],
    ];
    $description = $this
      ->t('Block Visibility Groups allow you to control the visibility of multiple blocks in one place.');
    if (!$this
      ->groupsExist()) {
      $description .= ' ' . $this
        ->t('No Groups have been created yet.');
      $form['block_visibility_group']['create'] = [
        '#type' => 'link',
        '#title' => t('Create a Group'),
        '#url' => Url::fromRoute('entity.block_visibility_group.add_form'),
      ];
    }
    else {
      if ($current_block_visibility_group) {
        $form['block_visibility_group']['block_visibility_group_show_global'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Show Global Blocks'),
          '#default_value' => $this
            ->getShowGlobalWithGroup(),
          '#description' => $this
            ->t('Show global blocks when viewing a visibility group.'),
          '#attributes' => [
            'onchange' => 'this.form.submit()',
          ],
        ];

        /** @var \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup $group */
        $group = $this->group_storage
          ->load($current_block_visibility_group);
        $form['block_visibility_group']['help'] = $this
          ->createHelp($group);
        $conditions_element = $this
          ->createConditionsSet($form, $group, 'layout');
        $conditions_element['#type'] = 'details';
        if ($this->request->query
          ->get('show_conditions')) {
          $conditions_element['#open'] = TRUE;
        }
        else {
          $conditions_element['#open'] = FALSE;
        }
        $form['block_visibility_group']['conditions_section'] = $conditions_element;
      }
    }
    $form['block_visibility_group']['select']['#description'] = $description;
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $show_global = $form_state
      ->getValue('block_visibility_group_show_global', 1);
    $this->state
      ->set('block_visibility_group_show_global', $show_global);

    // Prevent sending an empty value, which would unset all blocks.
    if (!empty($form_state
      ->getValue('blocks'))) {
      parent::submitForm($form, $form_state);
    }
  }

  /**
   * Get the group from the query string.
   *
   * @return mixed|string
   */
  protected function getCurrentBlockVisibilityGroup() {
    $request_id = $this->request->query
      ->get('block_visibility_group');
    if (!$request_id) {
      $request_id = BlockVisibilityGroupedListBuilder::ALL_GROUP;
    }
    return $request_id;
  }

  /**
   * Get Group options info to group select dropdown.
   *
   * @return array
   *   Keys = Group keys
   *   Values array with keys:
   *     label
   *     path - URL to redirect to Group page.
   */
  protected function getBlockVisibilityGroupOptions() {
    $route_options = [
      BlockVisibilityGroupedListBuilder::UNSET_GROUP => [
        'label' => $this
          ->t('- Global blocks -'),
      ],
      BlockVisibilityGroupedListBuilder::ALL_GROUP => [
        'label' => $this
          ->t('- All Blocks -'),
      ],
    ];
    $block_visibility_group_labels = $this
      ->getBlockVisibilityLabels($this->group_storage);
    foreach ($block_visibility_group_labels as $id => $label) {
      $route_options[$id] = [
        'label' => $label,
      ];
    }
    foreach ($route_options as $key => &$route_option) {
      $url = Url::fromRoute('block.admin_display_theme', [
        'theme' => $this->theme,
      ], [
        'query' => [
          'block_visibility_group' => $key,
        ],
      ]);
      $route_option['path'] = $url
        ->toString();
    }
    return $route_options;
  }

  /**
   * {@inheritdoc}
   */
  protected function buildBlocksForm() {
    $form = parent::buildBlocksForm();
    $show_global_in_group = $this
      ->getShowGlobalWithGroup();
    if ($block_visibility_group = $this
      ->getBlockVisibilityGroup(TRUE)) {
      foreach ($form as &$row_info) {
        if (isset($row_info['title']['#url'])) {

          /** @var \Drupal\Core\Url $url */
          $url = $row_info['title']['#url'];
          $query = $url
            ->getOption('query');
          $url = Url::fromRoute('block_visibility_groups.admin_library', [
            'theme' => $this
              ->getThemeName(),
            'block_visibility_group' => $block_visibility_group,
          ], [
            'query' => $query,
          ]);
          $row_info['title']['#url'] = $url;

          // $query['block_visibility_group'] = $this->getBlockVisibilityGroup();
          // $url->setOption('query', $query);.
        }
        if (isset($row_info['operations']['#links']) && $row_info['operations']['#links']) {
          foreach ($row_info['operations']['#links'] as &$op_info) {
            $url = $op_info['url'];
            $query = $url
              ->getOption('query');
            $query['block_visibility_group'] = $block_visibility_group;
            $url
              ->setOption('query', $query);
          }
        }
      }
    }

    // If viewing all blocks, add a column indicating the visibility group.
    if ($this
      ->getBlockVisibilityGroup() == static::ALL_GROUP || $block_visibility_group && $show_global_in_group) {
      $this
        ->addGroupColumn($form);
    }
    return $form;
  }

  /**
   * Get the Block Visibility Group for this page request.
   *
   * @param bool|false $groups_only
   *   Should this function return only group key
   *   or also a constant value if no group.
   *
   * @return string|null
   */
  protected function getBlockVisibilityGroup($groups_only = FALSE) {
    $group = $this->request->query
      ->get('block_visibility_group');
    if ($groups_only && in_array($group, [
      $this::ALL_GROUP,
      $this::UNSET_GROUP,
    ])) {
      return NULL;
    }
    return $group;
  }

  /**
   * {@inheritdoc}
   *
   * Unset blocks that should not be shown with current group.
   */
  protected function getEntityIds() {
    $entity_ids = parent::getEntityIds();
    $current_block_visibility_group = $this
      ->getCurrentBlockVisibilityGroup();
    $show_global_in_group = $this
      ->getShowGlobalWithGroup();
    if (!empty($current_block_visibility_group) && $current_block_visibility_group != $this::ALL_GROUP) {
      $entities = $this->storage
        ->loadMultipleOverrideFree($entity_ids);

      /** @var Block $block */
      foreach ($entities as $block) {
        $config_block_visibility_group = $this
          ->getGroupForBlock($block);
        if (static::UNSET_GROUP == $current_block_visibility_group) {
          if (!empty($config_block_visibility_group)) {
            unset($entity_ids[$block
              ->id()]);
          }
        }
        elseif ($config_block_visibility_group != $current_block_visibility_group && !(empty($config_block_visibility_group) && $show_global_in_group)) {
          unset($entity_ids[$block
            ->id()]);
        }
      }
    }
    return $entity_ids;
  }

  /**
   * Determine if any groups exist.
   *
   * @return bool
   *   TRUE if any groups exist.
   */
  protected function groupsExist() {
    return !empty($this->group_storage
      ->loadMultiple());
  }

  /**
   * Add Column to show Visibility Group.
   *
   * @param array $form
   *   The form array.
   */
  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;
          }
        }
      }
    }
  }

  /**
   * Determine if global(unset) blocks should be shown when viewing a group.
   *
   * @return mixed
   */
  protected function getShowGlobalWithGroup() {
    return $this->state
      ->get('block_visibility_group_show_global', 1);
  }

  /**
   * Create a help description.
   *
   * @param array $form
   *   The form array.
   * @param $group
   *   The block visibility group instance.
   *
   * @return array
   *   A renderable array.
   */
  protected function createHelp(BlockVisibilityGroup $group) {
    $help = '<strong>' . $this
      ->t('Currently viewing') . ': <em>' . $group
      ->label() . '</em></strong>';
    if ($group
      ->getLogic() == 'and') {
      $help .= '<p>' . $this
        ->t('All conditions must pass.') . '</p>';
    }
    else {
      $help .= '<p>' . $this
        ->t('Only one condition must pass.') . '</p>';
    }
    if ($group
      ->isAllowOtherConditions()) {
      $help .= '<p>' . $this
        ->t('Blocks in this group may have other conditions.') . '</p>';
    }
    else {
      $help .= '<p>' . $this
        ->t('Blocks in this group may <strong>not</strong> have other conditions.') . '</p>';
    }
    $help_group = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this
        ->t('Group Settings'),
      'text' => [
        '#type' => 'markup',
        '#markup' => $help,
      ],
      'edit' => [
        '#type' => 'link',
        '#title' => t('Edit Group Settings'),
        '#url' => $group
          ->toUrl('edit-form'),
      ],
    ];
    return $help_group;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockListBuilder::$formBuilder protected property The form builder.
BlockListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. Overrides EntityListBuilder::$limit
BlockListBuilder::$messenger protected property The messenger. Overrides MessengerTrait::$messenger
BlockListBuilder::$request protected property The current request.
BlockListBuilder::$theme protected property The theme containing the blocks.
BlockListBuilder::$themeManager protected property The theme manager.
BlockListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides ConfigEntityListBuilder::getDefaultOperations
BlockListBuilder::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BlockListBuilder::getThemeName protected function Gets the name of the theme used for this block listing.
BlockListBuilder::render public function Overrides EntityListBuilder::render
BlockListBuilder::systemRegionList protected function Wraps system_region_list().
BlockListBuilder::validateForm public function Form validation handler. Overrides FormInterface::validateForm
BlockVisibilityGroupedListBuilder::$group_storage protected property The entity storage class for Block Visibility Groups.
BlockVisibilityGroupedListBuilder::$state protected property The state service.
BlockVisibilityGroupedListBuilder::addGroupColumn protected function Add Column to show Visibility Group.
BlockVisibilityGroupedListBuilder::ALL_GROUP constant Used in Query string to denote showing all blocks.
BlockVisibilityGroupedListBuilder::buildBlocksForm protected function Builds the main "Blocks" portion of the form. Overrides BlockListBuilder::buildBlocksForm
BlockVisibilityGroupedListBuilder::buildForm public function Form constructor. Overrides BlockListBuilder::buildForm
BlockVisibilityGroupedListBuilder::createHelp protected function Create a help description.
BlockVisibilityGroupedListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides BlockListBuilder::createInstance
BlockVisibilityGroupedListBuilder::getBlockVisibilityGroup protected function Get the Block Visibility Group for this page request.
BlockVisibilityGroupedListBuilder::getBlockVisibilityGroupOptions protected function Get Group options info to group select dropdown.
BlockVisibilityGroupedListBuilder::getCurrentBlockVisibilityGroup protected function Get the group from the query string.
BlockVisibilityGroupedListBuilder::getEntityIds protected function Unset blocks that should not be shown with current group. Overrides BlockListBuilder::getEntityIds
BlockVisibilityGroupedListBuilder::getShowGlobalWithGroup protected function Determine if global(unset) blocks should be shown when viewing a group.
BlockVisibilityGroupedListBuilder::groupsExist protected function Determine if any groups exist.
BlockVisibilityGroupedListBuilder::submitForm public function Form submission handler. Overrides BlockListBuilder::submitForm
BlockVisibilityGroupedListBuilder::UNSET_GROUP constant Used in query string to denote blocks that don't have a group set.
BlockVisibilityGroupedListBuilder::__construct public function Constructs a new BlockVisibilityGroupedListBuilder object. Overrides BlockListBuilder::__construct
BlockVisibilityLister::getBlockVisibilityLabels protected function Get labels for groups.
BlockVisibilityLister::getGroupForBlock protected function Get the visibility group for a block.
ConditionsSetFormTrait::createConditionsSet protected function Create a conditions set form.
ConfigEntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load 7
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildHeader public function Builds the header row for the entity listing. 26
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::buildRow public function Builds a row for an entity in the entity listing. 26
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.