/**
  * Presents a list of conditions to add to the block_visibility_group entity.
  *
  * @param \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup $block_visibility_group
  *   The block_visibility_group entity.
  *
  * @return array
  *   The condition selection page.
  */
 public function selectCondition(BlockVisibilityGroup $block_visibility_group, $redirect) {
   $build = [
     '#theme' => 'links',
     '#links' => [],
   ];
   $available_plugins = $this->conditionManager->getDefinitions();
   // @todo Should nesting Conditions be allowed
   unset($available_plugins['condition_group']);
   foreach ($available_plugins as $condition_id => $condition) {
     $build['#links'][$condition_id] = [
       'title' => $condition['label'],
       'url' => Url::fromRoute('block_visibility_groups.condition_add', [
         'block_visibility_group' => $block_visibility_group->id(),
         'condition_id' => $condition_id,
         'redirect' => $redirect,
       ]),
       'attributes' => [
         'class' => ['use-ajax'],
         'data-dialog-type' => 'modal',
         'data-dialog-options' => Json::encode([
           'width' => 'auto',
         ]),
       ],
     ];
   }
   return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     $definitions = parent::getDefinitions();
     // Make sure that all definitions have a category to avoid PHP notices in
     // CategorizingPluginManagerTrait.
     // @todo fix this in core in CategorizingPluginManagerTrait.
     foreach ($definitions as &$definition) {
         if (!isset($definition['category'])) {
             $definition['category'] = $this->t('Other');
         }
     }
     return $definitions;
 }
Example #3
0
 /**
  * Helper function for building the visibility UI form.
  *
  * @param array $form
  *   An associative array containing the structure of the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  *
  * @return array
  *   The form array with the visibility UI added in.
  */
 protected function buildVisibilityInterface(array $form, FormStateInterface $form_state)
 {
     $form['visibility_tabs'] = ['#type' => 'vertical_tabs', '#title' => $this->t('Visibility'), '#parents' => ['visibility_tabs'], '#attached' => ['library' => ['block/drupal.block']]];
     // @todo Allow list of conditions to be configured in
     //   https://www.drupal.org/node/2284687.
     $visibility = $this->entity->getVisibility();
     foreach ($this->manager->getDefinitions() as $condition_id => $definition) {
         // Don't display the current theme condition.
         if ($condition_id == 'current_theme') {
             continue;
         }
         // Don't display the language condition until we have multiple languages.
         if ($condition_id == 'language' && !$this->language->isMultilingual()) {
             continue;
         }
         /** @var \Drupal\Core\Condition\ConditionInterface $condition */
         $condition = $this->manager->createInstance($condition_id, isset($visibility[$condition_id]) ? $visibility[$condition_id] : []);
         $form_state->set(['conditions', $condition_id], $condition);
         $condition_form = $condition->buildConfigurationForm([], $form_state);
         $condition_form['#type'] = 'details';
         $condition_form['#title'] = $condition->getPluginDefinition()['label'];
         $condition_form['#group'] = 'visibility_tabs';
         $form[$condition_id] = $condition_form;
     }
     if (isset($form['node_type'])) {
         $form['node_type']['#title'] = $this->t('Content types');
         $form['node_type']['bundles']['#title'] = $this->t('Content types');
         $form['node_type']['negate']['#type'] = 'value';
         $form['node_type']['negate']['#title_display'] = 'invisible';
         $form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value'];
     }
     if (isset($form['user_role'])) {
         $form['user_role']['#title'] = $this->t('Roles');
         unset($form['user_role']['roles']['#description']);
         $form['user_role']['negate']['#type'] = 'value';
         $form['user_role']['negate']['#value'] = $form['user_role']['negate']['#default_value'];
     }
     if (isset($form['request_path'])) {
         $form['request_path']['#title'] = $this->t('Pages');
         $form['request_path']['negate']['#type'] = 'radios';
         $form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
         $form['request_path']['negate']['#title_display'] = 'invisible';
         $form['request_path']['negate']['#options'] = [$this->t('Show for the listed pages'), $this->t('Hide for the listed pages')];
     }
     if (isset($form['language'])) {
         $form['language']['negate']['#type'] = 'value';
         $form['language']['negate']['#value'] = $form['language']['negate']['#default_value'];
     }
     return $form;
 }