You are here

public function BlockFormBase::buildForm in Context 8

Same name and namespace in other branches
  1. 8.4 src/Reaction/Blocks/Form/BlockFormBase.php \Drupal\context\Reaction\Blocks\Form\BlockFormBase::buildForm()
  2. 8.0 src/Reaction/Blocks/Form/BlockFormBase.php \Drupal\context\Reaction\Blocks\Form\BlockFormBase::buildForm()

Form constructor.

Parameters

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

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

ContextInterface $context: The context the reaction belongs to.

string|null $reaction_id: The ID of the blocks reaction the block should be added to.

string|null $block_id: The ID of the block to show a configuration form for.

Return value

array

Overrides FormInterface::buildForm

File

src/Reaction/Blocks/Form/BlockFormBase.php, line 175

Class

BlockFormBase

Namespace

Drupal\context\Reaction\Blocks\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, ContextInterface $context = NULL, $reaction_id = NULL, $block_id = NULL) {
  $this->context = $context;
  $this->reaction = $this->context
    ->getReaction($reaction_id);
  $this->block = $this
    ->prepareBlock($block_id);

  // If a theme was defined in the query use this theme for the block
  // otherwise use the default theme.
  $theme = $this
    ->getRequest()->query
    ->get('theme', $this->themeHandler
    ->getDefault());

  // Some blocks require the theme name in the form state like Site Branding
  $form_state
    ->set('block_theme', $theme);

  // Some blocks require contexts, set a temporary value with gathered
  // contextual values.
  $form_state
    ->setTemporaryValue('gathered_contexts', $this->contextRepository
    ->getAvailableContexts());
  $configuration = $this->block
    ->getConfiguration();
  $form['#tree'] = TRUE;
  $form['settings'] = $this->block
    ->buildConfigurationForm([], $form_state);
  $form['settings']['id'] = [
    '#type' => 'value',
    '#value' => $this->block
      ->getPluginId(),
  ];
  $form['custom_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' => isset($configuration['custom_id']) ? $configuration['custom_id'] : preg_replace("/\\W+/", "_", $this->block
      ->getPluginId()),
    '#machine_name' => [
      'source' => [
        'settings',
        'label',
      ],
    ],
    '#required' => TRUE,
  ];
  $form['region'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Region'),
    '#description' => $this
      ->t('Select the region where this block should be displayed.'),
    '#options' => $this
      ->getThemeRegionOptions($theme),
    '#default_value' => isset($configuration['region']) ? $configuration['region'] : '',
  ];
  $form['unique'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Unique'),
    '#description' => $this
      ->t('Check if the block should be uniquely placed, this means that the block can not be overridden by other blocks of the same type in the selected region.'),
    '#default_value' => isset($configuration['unique']) ? $configuration['unique'] : FALSE,
  ];
  $form['theme'] = [
    '#type' => 'value',
    '#value' => $theme,
  ];
  $form['css_class'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Block Class'),
    '#default_value' => isset($configuration['css_class']) ? $configuration['css_class'] : '',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->getSubmitValue(),
    '#button_type' => 'primary',
    '#ajax' => [
      'callback' => '::submitFormAjax',
    ],
  ];

  // Remove ajax from submit, if this is not ajax request.
  if (!$this->request
    ->isXmlHttpRequest()) {
    unset($form['actions']['submit']['#ajax']);
  }

  // Disable cache on form to prevent ajax forms from failing.
  $form_state
    ->disableCache();
  return $form;
}