You are here

public function FieldBlock::blockForm in Field as Block 8.2

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/FieldBlock.php, line 206

Class

FieldBlock
Provides a fieldblock.

Namespace

Drupal\fieldblock\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {

  // This method receives a sub form state instead of the full form state.
  // There is an ongoing discussion around this which could result in the
  // passed form state going back to a full form state. In order to prevent
  // future breakage because of a core update we'll just check which type of
  // FormStateInterface we've been passed and act accordingly.
  // @See https://www.drupal.org/node/2798261
  if ($form_state instanceof SubformStateInterface) {
    $form_state = $form_state
      ->getCompleteFormState();
  }
  $form['label_from_field'] = [
    '#title' => $this
      ->t('Use field label as block title'),
    '#type' => 'checkbox',
    '#default_value' => $this->configuration['label_from_field'],
  ];
  $form['field_name'] = [
    '#title' => $this
      ->t('Field'),
    '#type' => 'select',
    '#options' => $this
      ->getFieldOptions(),
    '#default_value' => $this->configuration['field_name'],
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'blockFormChangeFieldOrFormatterAjax',
      ],
      'wrapper' => 'edit-block-formatter-wrapper',
    ],
  ];
  $form['formatter'] = [
    '#type' => 'container',
    '#id' => 'edit-block-formatter-wrapper',
  ];
  $field_name = $form_state
    ->getValue([
    'settings',
    'field_name',
  ], $this->configuration['field_name']);
  $field_definition = NULL;
  $formatter_id = $form_state
    ->getValue([
    'settings',
    'formatter',
    'id',
  ], $this->configuration['formatter_id']);
  if ($field_name) {
    $field_definition = $this
      ->getFieldDefinition($field_name);
    $formatter_options = $this
      ->getFormatterOptions($field_definition);
    if (empty($formatter_options)) {
      $formatter_id = '';
    }
    else {
      if (empty($formatter_id)) {
        $formatter_id = key($formatter_options);
      }
      $form['formatter']['id'] = [
        '#title' => $this
          ->t('Formatter'),
        '#type' => 'select',
        '#options' => $formatter_options,
        '#default_value' => $this->configuration['formatter_id'],
        '#required' => TRUE,
        '#ajax' => [
          'callback' => [
            $this,
            'blockFormChangeFieldOrFormatterAjax',
          ],
          'wrapper' => 'edit-block-formatter-wrapper',
        ],
      ];
    }
  }
  $form['formatter']['change'] = [
    '#type' => 'submit',
    '#name' => 'fieldblock_change_field',
    '#value' => $this
      ->t('Change field'),
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'settings',
      ],
    ],
    '#submit' => [
      [
        get_class($this),
        'blockFormChangeFieldOrFormatter',
      ],
    ],
  ];
  if ($formatter_id) {
    $formatter_settings = $this->configuration['formatter_settings'] + $this->formatterPluginManager
      ->getDefaultSettings($formatter_id);
    $formatter_options = [
      'field_definition' => $field_definition,
      'view_mode' => '_custom',
      'configuration' => [
        'type' => $formatter_id,
        'settings' => $formatter_settings,
        'label' => '',
        'weight' => 0,
      ],
    ];
    if ($formatter_plugin = $this->formatterPluginManager
      ->getInstance($formatter_options)) {
      $formatter_settings_form = $formatter_plugin
        ->settingsForm($form, $form_state);

      // Convert field UI selector states to work in the block configuration
      // form.
      FormHelper::rewriteStatesSelector($formatter_settings_form, "fields[{$field_name}][settings_edit_form]", 'settings[formatter][settings]');
    }
    if (!empty($formatter_settings_form)) {
      $form['formatter']['settings'] = $formatter_settings_form;
      $form['formatter']['settings']['#type'] = 'fieldset';
      $form['formatter']['settings']['#title'] = $this
        ->t('Formatter settings');
    }
  }
  return $form;
}