You are here

public function MediaTypeForm::form in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/src/MediaTypeForm.php \Drupal\media\MediaTypeForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/media/src/MediaTypeForm.php, line 85

Class

MediaTypeForm
Form controller for media type forms.

Namespace

Drupal\media

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Source is not set when the entity is initially created.

  /** @var \Drupal\media\MediaSourceInterface $source */
  $source = $this->entity
    ->get('source') ? $this->entity
    ->getSource() : NULL;
  if ($this->operation === 'add') {
    $form['#title'] = $this
      ->t('Add media type');
  }
  $form['label'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $this->entity
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this media type.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $this->entity
      ->id(),
    '#maxlength' => 32,
    '#disabled' => !$this->entity
      ->isNew(),
    '#machine_name' => [
      'exists' => [
        MediaType::class,
        'load',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this media type.'),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $this->entity
      ->getDescription(),
    '#description' => $this
      ->t('Describe this media type. The text will be displayed on the <em>Add new media</em> page.'),
  ];
  $plugins = $this->sourceManager
    ->getDefinitions();
  $options = [];
  foreach ($plugins as $plugin_id => $definition) {
    $options[$plugin_id] = $definition['label'];
  }
  $form['source_dependent'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'source-dependent',
    ],
  ];
  if (!$this->entity
    ->isNew()) {
    $source_description = $this
      ->t('<em>The media source cannot be changed after the media type is created.</em>');
  }
  else {
    $source_description = $this
      ->t('Media source that is responsible for additional logic related to this media type.');
  }
  $form['source_dependent']['source'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Media source'),
    '#default_value' => $source ? $source
      ->getPluginId() : NULL,
    '#options' => $options,
    '#description' => $source_description,
    '#ajax' => [
      'callback' => '::ajaxHandlerData',
    ],
    '#required' => TRUE,
    // Once the media type is created, its source plugin cannot be changed
    // anymore.
    '#disabled' => !$this->entity
      ->isNew(),
  ];
  if ($source) {

    // Media source plugin configuration.
    $form['source_dependent']['source_configuration'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Media source configuration'),
      '#tree' => TRUE,
    ];
    $form['source_dependent']['source_configuration'] = $source
      ->buildConfigurationForm($form['source_dependent']['source_configuration'], $this
      ->getSourceSubFormState($form, $form_state));
  }

  // Field mapping configuration.
  $form['source_dependent']['field_map'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Field mapping'),
    '#tree' => TRUE,
    'description' => [
      '#markup' => '<p>' . $this
        ->t('Media sources can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.') . '</p>',
    ],
  ];
  if (empty($source) || empty($source
    ->getMetadataAttributes())) {
    $form['source_dependent']['field_map']['#access'] = FALSE;
  }
  else {
    $options = [
      MediaSourceInterface::METADATA_FIELD_EMPTY => $this
        ->t('- Skip field -'),
    ];
    foreach ($this->entityFieldManager
      ->getFieldDefinitions('media', $this->entity
      ->id()) as $field_name => $field) {
      if (!$field instanceof BaseFieldDefinition || $field_name === 'name') {
        $options[$field_name] = $field
          ->getLabel();
      }
    }
    $field_map = $this->entity
      ->getFieldMap();
    foreach ($source
      ->getMetadataAttributes() as $metadata_attribute_name => $metadata_attribute_label) {
      $form['source_dependent']['field_map'][$metadata_attribute_name] = [
        '#type' => 'select',
        '#title' => $metadata_attribute_label,
        '#options' => $options,
        '#default_value' => isset($field_map[$metadata_attribute_name]) ? $field_map[$metadata_attribute_name] : MediaSourceInterface::METADATA_FIELD_EMPTY,
      ];
    }
  }
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
    '#attached' => [
      'library' => [
        'media/type_form',
      ],
    ],
  ];
  $form['workflow'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Publishing options'),
    '#group' => 'additional_settings',
  ];
  $form['workflow']['options'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Default options'),
    '#default_value' => $this
      ->getWorkflowOptions(),
    '#options' => [
      'status' => $this
        ->t('Published'),
      'new_revision' => $this
        ->t('Create new revision'),
      'queue_thumbnail_downloads' => $this
        ->t('Queue thumbnail downloads'),
    ],
  ];
  $form['workflow']['options']['status']['#description'] = $this
    ->t('Media will be automatically published when created.');
  $form['workflow']['options']['new_revision']['#description'] = $this
    ->t('Automatically create new revisions. Users with the "Administer media" permission will be able to override this option.');
  $form['workflow']['options']['queue_thumbnail_downloads']['#description'] = $this
    ->t('Download thumbnails via a queue. When using remote media sources, the thumbnail generation could be a slow process. Using a queue allows for this process to be handled in the background.');
  if ($this->moduleHandler
    ->moduleExists('language')) {
    $form['language'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Language settings'),
      '#group' => 'additional_settings',
    ];
    $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('media', $this->entity
      ->id());
    $form['language']['language_configuration'] = [
      '#type' => 'language_configuration',
      '#entity_information' => [
        'entity_type' => 'media',
        'bundle' => $this->entity
          ->id(),
      ],
      '#default_value' => $language_configuration,
    ];
  }
  return $form;
}