You are here

public function MediaBundleForm::form in Media entity 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/MediaBundleForm.php, line 85

Class

MediaBundleForm
Form controller for node type forms.

Namespace

Drupal\media_entity

Code

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

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $form['#entity'] = $bundle = $this->entity;
  $form_state
    ->set('bundle', $bundle
    ->id());
  if ($this->operation == 'add') {
    $form['#title'] = $this
      ->t('Add media bundle');
  }
  elseif ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('Edit %label media bundle', [
      '%label' => $bundle
        ->label(),
    ]);
  }
  $form['label'] = [
    '#title' => $this
      ->t('Label'),
    '#type' => 'textfield',
    '#default_value' => $bundle
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this media bundle.'),
    '#required' => TRUE,
    '#size' => 30,
    '#weight' => -100,
  ];

  // @todo: '#disabled' not always FALSE.
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $bundle
      ->id(),
    '#maxlength' => 32,
    '#disabled' => !$bundle
      ->isNew(),
    '#machine_name' => [
      'exists' => [
        '\\Drupal\\media_entity\\Entity\\MediaBundle',
        'exists',
      ],
      'source' => [
        'label',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this media bundle.'),
    '#weight' => -90,
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $bundle
      ->getDescription(),
    '#description' => $this
      ->t('Describe this media bundle. The text will be displayed on the <em>Add new media</em> page.'),
    '#weight' => -80,
  ];
  $plugins = $this->mediaTypeManager
    ->getDefinitions();
  $options = [];
  foreach ($plugins as $plugin => $definition) {
    $options[$plugin] = $definition['label'];
  }
  $form['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type provider'),
    '#default_value' => $bundle
      ->getType()
      ->getPluginId(),
    '#options' => $options,
    '#description' => $this
      ->t('Media type provider plugin that is responsible for additional logic related to this media.'),
    '#weight' => -70,
    '#ajax' => [
      'callback' => '::ajaxTypeProviderData',
      'progress' => [
        'type' => 'throbber',
        'message' => $this
          ->t('Updating type provider configuration form.'),
      ],
    ],
  ];

  // Media type plugin configuration.
  $form['type_configuration'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Type provider configuration'),
    '#tree' => TRUE,
    '#weight' => -60,
  ];

  /** @var \Drupal\media_entity\MediaTypeInterface $plugin */
  if ($plugin = $bundle
    ->getType()) {
    $plugin_configuration = empty($this->configurableInstances[$plugin
      ->getPluginId()]['plugin_config']) ? $bundle->type_configuration : $this->configurableInstances[$plugin
      ->getPluginId()]['plugin_config'];

    /** @var \Drupal\media_entity\MediaTypeBase $instance */
    $instance = $this->mediaTypeManager
      ->createInstance($plugin
      ->getPluginId(), $plugin_configuration);

    // Store the configuration for validate and submit handlers.
    $this->configurableInstances[$plugin
      ->getPluginId()]['plugin_config'] = $plugin_configuration;
    $form['type_configuration'][$plugin
      ->getPluginId()] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'edit-type-configuration-plugin-wrapper',
      ],
    ];
    $form['type_configuration'][$plugin
      ->getPluginId()] += $instance
      ->buildConfigurationForm([], $form_state);
  }

  // Field mapping configuration.
  $form['field_mapping'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Field mapping'),
    '#tree' => TRUE,
    '#attributes' => [
      'id' => 'field-mapping-wrapper',
    ],
    'description' => [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('Media type plugins can provide metadata fields such as title, caption, size information, credits, ... Media entity 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.'),
    ],
    '#weight' => -50,
  ];
  if (empty($plugin) || empty($plugin
    ->providedFields())) {
    $form['field_mapping']['empty_message'] = [
      '#prefix' => '<em>',
      '#suffix' => '</em>',
      '#markup' => $this
        ->t('No metadata fields available.'),
    ];
  }
  else {
    $skipped_fields = [
      'mid',
      'uuid',
      'vid',
      'bundle',
      'langcode',
      'default_langcode',
      'uid',
      'revision_timestamp',
      'revision_log',
      'revision_uid',
    ];
    $options = [
      '_none' => $this
        ->t('- Skip field -'),
    ];
    foreach ($this->entityFieldManager
      ->getFieldDefinitions('media', $bundle
      ->id()) as $field_name => $field) {
      if (!in_array($field_name, $skipped_fields)) {
        $options[$field_name] = $field
          ->getLabel();
      }
    }
    foreach ($plugin
      ->providedFields() as $field_name => $field_label) {
      $form['field_mapping'][$field_name] = [
        '#type' => 'select',
        '#title' => $field_label,
        '#options' => $options,
        '#default_value' => isset($bundle->field_map[$field_name]) ? $bundle->field_map[$field_name] : '_none',
      ];
    }
  }
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
    '#attached' => [
      'library' => [
        'media_entity/media_bundle_form',
      ],
    ],
    '#weight' => 100,
  ];
  $form['workflow'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Publishing options'),
    '#group' => 'additional_settings',
  ];
  $workflow_options = [
    'status' => $bundle
      ->getStatus(),
    'new_revision' => $bundle
      ->shouldCreateNewRevision(),
    'queue_thumbnail_downloads' => $bundle
      ->getQueueThumbnailDownloads(),
  ];

  // Prepare workflow options to be used for 'checkboxes' form element.
  $keys = array_keys(array_filter($workflow_options));
  $workflow_options = array_combine($keys, $keys);
  $form['workflow']['options'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Default options'),
    '#default_value' => $workflow_options,
    '#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('Entities will be automatically published when they are created.');
  $form['workflow']['options']['new_revision']['#description'] = $this
    ->t('Automatically create a new revision of media entities. 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.');
  if ($this->moduleHandler
    ->moduleExists('language')) {
    $form['language'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Language settings'),
      '#group' => 'additional_settings',
    ];
    $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('media', $bundle
      ->id());
    $form['language']['language_configuration'] = [
      '#type' => 'language_configuration',
      '#entity_information' => [
        'entity_type' => 'media',
        'bundle' => $bundle
          ->id(),
      ],
      '#default_value' => $language_configuration,
    ];
  }
  return $form;
}