You are here

public function BlockFieldConfigForm::buildForm in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/BlockFieldConfigForm.php \Drupal\ds\Form\BlockFieldConfigForm::buildForm()
  2. 8.2 src/Form/BlockFieldConfigForm.php \Drupal\ds\Form\BlockFieldConfigForm::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.

Return value

array The form structure.

Overrides FieldFormBase::buildForm

File

src/Form/BlockFieldConfigForm.php, line 54

Class

BlockFieldConfigForm
Configure block fields.

Namespace

Drupal\ds\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $field_key = '') {

  // Fetch field.
  $field = $this
    ->config('ds.field.' . $field_key)
    ->get();

  // Inject default theme in form state (Site branding needs it for instance).
  $default_theme = $this
    ->config('system.theme')
    ->get('default');
  $form_state
    ->set('block_theme', $default_theme);

  // Save the field for future reuse.
  $this->field = $field;

  // Create an instance of the block.

  /* @var $block \Drupal\Core\Block\BlockPluginInterface */
  $manager = \Drupal::service('plugin.manager.block');
  $block_id = $field['properties']['block'];
  $block = $manager
    ->createInstance($block_id);

  // Set block config form default values.
  if (isset($field['properties']['config'])) {
    $block
      ->setConfiguration($field['properties']['config']);
  }

  // Get block config form.
  $form = $block
    ->blockForm($form, $form_state);

  // If the block is context aware, attach the mapping widget.
  if ($block instanceof ContextAwarePluginInterface) {
    $form['context_mapping'] = $this
      ->addContextAssignmentElement($block, $this->contextRepository
      ->getAvailableContexts());
  }
  if (!$form) {
    return [
      '#markup' => $this
        ->t("This block has no configuration options."),
    ];
  }

  // Some form items require this (core block manager also sets this).
  $form['#tree'] = TRUE;
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#weight' => 100,
  ];
  return $form;
}