You are here

public function FieldBlock::blockForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/Plugin/Block/FieldBlock.php \Drupal\layout_builder\Plugin\Block\FieldBlock::blockForm()

Overrides BlockPluginTrait::blockForm

File

core/modules/layout_builder/src/Plugin/Block/FieldBlock.php, line 232

Class

FieldBlock
Provides a block that renders a field from an entity.

Namespace

Drupal\layout_builder\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $config = $this
    ->getConfiguration();
  $form['formatter'] = [
    '#tree' => TRUE,
    '#process' => [
      [
        $this,
        'formatterSettingsProcessCallback',
      ],
    ],
  ];
  $form['formatter']['label'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Label'),
    // @todo This is directly copied from
    //   \Drupal\field_ui\Form\EntityViewDisplayEditForm::getFieldLabelOptions(),
    //   resolve this in https://www.drupal.org/project/drupal/issues/2933924.
    '#options' => [
      'above' => $this
        ->t('Above'),
      'inline' => $this
        ->t('Inline'),
      'hidden' => '- ' . $this
        ->t('Hidden') . ' -',
      'visually_hidden' => '- ' . $this
        ->t('Visually Hidden') . ' -',
    ],
    '#default_value' => $config['formatter']['label'],
  ];
  $form['formatter']['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Formatter'),
    '#options' => $this
      ->getApplicablePluginOptions($this
      ->getFieldDefinition()),
    '#required' => TRUE,
    '#default_value' => $config['formatter']['type'],
    '#ajax' => [
      'callback' => [
        static::class,
        'formatterSettingsAjaxCallback',
      ],
      'wrapper' => 'formatter-settings-wrapper',
    ],
  ];

  // Add the formatter settings to the form via AJAX.
  $form['formatter']['settings_wrapper'] = [
    '#prefix' => '<div id="formatter-settings-wrapper">',
    '#suffix' => '</div>',
  ];
  return $form;
}