You are here

public function Image::buildConfigurationForm in Media entity image 8

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 MediaTypeBase::buildConfigurationForm

File

src/Plugin/MediaEntity/Type/Image.php, line 160

Class

Image
Provides media type plugin for Image.

Namespace

Drupal\media_entity_image\Plugin\MediaEntity\Type

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $bundle = $form_state
    ->getFormObject()
    ->getEntity();
  $options = [];
  $allowed_field_types = [
    'file',
    'image',
  ];
  foreach ($this->entityFieldManager
    ->getFieldDefinitions('media', $bundle
    ->id()) as $field_name => $field) {
    if (in_array($field
      ->getType(), $allowed_field_types) && !$field
      ->getFieldStorageDefinition()
      ->isBaseField()) {
      $options[$field_name] = $field
        ->getLabel();
    }
  }
  $form['source_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field with source information'),
    '#description' => $this
      ->t('Field on media entity that stores Image file. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
    '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
    '#options' => $options,
  ];
  $form['gather_exif'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Whether to gather exif data.'),
    '#description' => $this
      ->t('Gather exif data using exif_read_data().'),
    '#default_value' => empty($this->configuration['gather_exif']) || !function_exists('exif_read_data') ? 0 : $this->configuration['gather_exif'],
    '#options' => [
      0 => $this
        ->t('No'),
      1 => $this
        ->t('Yes'),
    ],
    '#ajax' => [
      'callback' => '::ajaxTypeProviderData',
    ],
    '#disabled' => function_exists('exif_read_data') ? FALSE : TRUE,
  ];
  return $form;
}