function BlockForm::buildVisibilityInterface

Same name in other branches
  1. 9 core/modules/block/src/BlockForm.php \Drupal\block\BlockForm::buildVisibilityInterface()
  2. 8.9.x core/modules/block/src/BlockForm.php \Drupal\block\BlockForm::buildVisibilityInterface()
  3. 10 core/modules/block/src/BlockForm.php \Drupal\block\BlockForm::buildVisibilityInterface()

Helper function for building the visibility UI form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form array with the visibility UI added in.

1 call to BlockForm::buildVisibilityInterface()
BlockForm::form in core/modules/block/src/BlockForm.php
Gets the actual form array to be built.
1 method overrides BlockForm::buildVisibilityInterface()
BlockEntitySettingTrayForm::buildVisibilityInterface in core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
Helper function for building the visibility UI form.

File

core/modules/block/src/BlockForm.php, line 218

Class

BlockForm
Provides form for block instance forms.

Namespace

Drupal\block

Code

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();
    $definitions = $this->manager
        ->getFilteredDefinitions('block_ui', $form_state->getTemporaryValue('gathered_contexts'), [
        'block' => $this->entity,
    ]);
    foreach ($definitions 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, $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;
    }
    // Disable negation for specific conditions.
    $disable_negation = [
        'entity_bundle:node',
        'language',
        'response_status',
        'user_role',
    ];
    foreach ($disable_negation as $condition) {
        if (isset($form[$condition])) {
            $form[$condition]['negate']['#type'] = 'value';
            $form[$condition]['negate']['#value'] = $form[$condition]['negate']['#default_value'];
        }
    }
    if (isset($form['user_role'])) {
        $form['user_role']['#title'] = $this->t('Roles');
        unset($form['user_role']['roles']['#description']);
    }
    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'),
        ];
    }
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.