BlockForm::form

public BlockForm::form(array $form, FormStateInterface $form_state)

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 123

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'] = [];
  $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'] = 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',
      ),
    );
  }

  // Hidden weight setting.
  $weight = $entity->isNew() ? $this->getRequest()->query->get('weight', 0) : $entity->getWeight();
  $form['weight'] = array(
    '#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'] = 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;
}
doc_Drupal
2016-10-29 08:47:32
Comments
Leave a Comment

Please login to continue.