function BlockForm::form

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

Overrides EntityForm::form

1 call to BlockForm::form()
BlockEntitySettingTrayForm::form in core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
Gets the actual form array to be built.
1 method overrides BlockForm::form()
BlockEntitySettingTrayForm::form in core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
Gets the actual form array to be built.

File

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

Class

BlockForm
Provides form for block instance forms.

Namespace

Drupal\block

Code

public function form(array $form, FormStateInterface $form_state) {
    $entity = $this->entity;
    // Store the gathered contexts in the form state for other objects to use
    // during form building.
    $form_state->setTemporaryValue('gathered_contexts', $this->contextRepository
        ->getAvailableContexts());
    $form['#tree'] = TRUE;
    $form['settings'] = [];
    $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
    $form['settings'] = $this->getPluginForm($entity->getPlugin())
        ->buildConfigurationForm($form['settings'], $subform_state);
    $form['visibility'] = $this->buildVisibilityInterface([], $form_state);
    // If creating a new block, calculate a safe default machine name.
    $form['id'] = [
        '#type' => 'machine_name',
        '#maxlength' => 64,
        '#description' => $this->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
        '#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity),
        '#machine_name' => [
            'exists' => '\\Drupal\\block\\Entity\\Block::load',
            'replace_pattern' => '[^a-z0-9_.]+',
            'source' => [
                'settings',
                'label',
            ],
        ],
        '#required' => TRUE,
        '#disabled' => !$entity->isNew(),
    ];
    // Theme settings.
    if ($theme = $entity->getTheme()) {
        $form['theme'] = [
            '#type' => 'value',
            '#value' => $theme,
        ];
    }
    else {
        $theme = $this->config('system.theme')
            ->get('default');
        $theme_options = [];
        foreach ($this->themeHandler
            ->listInfo() as $theme_name => $theme_info) {
            if (!empty($theme_info->status)) {
                $theme_options[$theme_name] = $theme_info->info['name'];
            }
        }
        $form['theme'] = [
            '#type' => 'select',
            '#options' => $theme_options,
            '#title' => $this->t('Theme'),
            '#default_value' => $theme,
            '#ajax' => [
                'callback' => '::themeSwitch',
                'wrapper' => 'edit-block-region-wrapper',
            ],
        ];
    }
    // Hidden weight setting.
    $weight = $entity->isNew() ? $this->getRequest()->query
        ->get('weight', 0) : $entity->getWeight();
    $form['weight'] = [
        '#type' => 'hidden',
        '#default_value' => $weight,
    ];
    // Region settings.
    $entity_region = $entity->getRegion();
    $region = $entity->isNew() ? $this->getRequest()->query
        ->get('region', $entity_region) : $entity_region;
    $form['region'] = [
        '#type' => 'select',
        '#title' => $this->t('Region'),
        '#description' => $this->t('Select the region where this block should be displayed.'),
        '#default_value' => $region,
        '#required' => TRUE,
        '#options' => system_region_list($form_state->getValue('theme', $theme), REGIONS_VISIBLE),
        '#prefix' => '<div id="edit-block-region-wrapper">',
        '#suffix' => '</div>',
    ];
    $form['#attached']['library'][] = 'block/drupal.block.admin';
    return $form;
}

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