You are here

public function MediaSourceBase::buildConfigurationForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/src/MediaSourceBase.php \Drupal\media\MediaSourceBase::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

2 calls to MediaSourceBase::buildConfigurationForm()
OEmbed::buildConfigurationForm in core/modules/media/src/Plugin/media/Source/OEmbed.php
Form constructor.
Test::buildConfigurationForm in core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php
Form constructor.
2 methods override MediaSourceBase::buildConfigurationForm()
OEmbed::buildConfigurationForm in core/modules/media/src/Plugin/media/Source/OEmbed.php
Form constructor.
Test::buildConfigurationForm in core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php
Form constructor.

File

core/modules/media/src/MediaSourceBase.php, line 174

Class

MediaSourceBase
Base implementation of media source plugin.

Namespace

Drupal\media

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $options = $this
    ->getSourceFieldOptions();
  $form['source_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field with source information'),
    '#default_value' => $this->configuration['source_field'],
    '#empty_option' => $this
      ->t('- Create -'),
    '#options' => $options,
    '#description' => $this
      ->t('Select the field that will store essential information about the media item. If "Create" is selected a new field will be automatically created.'),
  ];
  if (!$options && $form_state
    ->get('operation') === 'add') {
    $form['source_field']['#access'] = FALSE;
    $field_definition = $this->fieldTypeManager
      ->getDefinition(reset($this->pluginDefinition['allowed_field_types']));
    $form['source_field_message'] = [
      '#markup' => $this
        ->t('%field_type field will be automatically created on this type to store the essential information about the media item.', [
        '%field_type' => $field_definition['label'],
      ]),
    ];
  }
  elseif ($form_state
    ->get('operation') === 'edit') {
    $form['source_field']['#access'] = FALSE;
    $fields = $this->entityFieldManager
      ->getFieldDefinitions('media', $form_state
      ->get('type')
      ->id());
    $form['source_field_message'] = [
      '#markup' => $this
        ->t('%field_name field is used to store the essential information about the media item.', [
        '%field_name' => $fields[$this->configuration['source_field']]
          ->getLabel(),
      ]),
    ];
  }
  return $form;
}