You are here

public function BlockForm::form in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/src/BlockForm.php \Drupal\block\BlockForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/block/src/BlockForm.php, line 114
Contains \Drupal\block\BlockForm.

Class

BlockForm
Provides form for block instance forms.

Namespace

Drupal\block

Code

public function form(array $form, FormStateInterface $form_state) {
  $entity = $this->entity;

  // Store theme settings in $form_state for use below.
  if (!($theme = $entity
    ->getTheme())) {
    $theme = $this
      ->config('system.theme')
      ->get('default');
  }
  $form_state
    ->set('block_theme', $theme);

  // 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'] = $entity
    ->getPlugin()
    ->buildConfigurationForm(array(), $form_state);
  $form['visibility'] = $this
    ->buildVisibilityInterface([], $form_state);

  // If creating a new block, calculate a safe default machine name.
  $form['id'] = array(
    '#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' => array(
      'exists' => '\\Drupal\\block\\Entity\\Block::load',
      'replace_pattern' => '[^a-z0-9_.]+',
      'source' => array(
        'settings',
        'label',
      ),
    ),
    '#required' => TRUE,
    '#disabled' => !$entity
      ->isNew(),
  );

  // Theme settings.
  if ($entity
    ->getTheme()) {
    $form['theme'] = array(
      '#type' => 'value',
      '#value' => $theme,
    );
  }
  else {
    $theme_options = array();
    foreach ($this->themeHandler
      ->listInfo() as $theme_name => $theme_info) {
      if (!empty($theme_info->status)) {
        $theme_options[$theme_name] = $theme_info->info['name'];
      }
    }
    $form['theme'] = array(
      '#type' => 'select',
      '#options' => $theme_options,
      '#title' => t('Theme'),
      '#default_value' => $theme,
      '#ajax' => array(
        'callback' => '::themeSwitch',
        'wrapper' => 'edit-block-region-wrapper',
      ),
    );
  }

  // Region settings.
  $entity_region = $entity
    ->getRegion();
  $region = $entity
    ->isNew() ? $this
    ->getRequest()->query
    ->get('region', $entity_region) : $entity_region;
  $form['region'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Region'),
    '#description' => $this
      ->t('Select the region where this block should be displayed.'),
    '#default_value' => $region,
    '#empty_value' => BlockInterface::BLOCK_REGION_NONE,
    '#options' => system_region_list($theme, REGIONS_VISIBLE),
    '#prefix' => '<div id="edit-block-region-wrapper">',
    '#suffix' => '</div>',
  );
  $form['#attached']['library'][] = 'block/drupal.block.admin';
  return $form;
}