You are here

public function MediaTypeForm::save in Drupal 8

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

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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

Class

MediaTypeForm
Form controller for media type forms.

Namespace

Drupal\media

Code

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

  /** @var \Drupal\media\MediaTypeInterface $media_type */
  $media_type = $this->entity;

  // If the media source is using a source field, ensure it's
  // properly created.
  $source = $media_type
    ->getSource();
  $source_field = $source
    ->getSourceFieldDefinition($media_type);
  if (!$source_field) {
    $source_field = $source
      ->createSourceField($media_type);

    /** @var \Drupal\field\FieldStorageConfigInterface $storage */
    $storage = $source_field
      ->getFieldStorageDefinition();
    if ($storage
      ->isNew()) {
      $storage
        ->save();
    }
    $source_field
      ->save();

    // Add the new field to the default form and view displays for this
    // media type.
    if ($source_field
      ->isDisplayConfigurable('form')) {
      $display = $this->entityDisplayRepository
        ->getFormDisplay('media', $media_type
        ->id());
      $source
        ->prepareFormDisplay($media_type, $display);
      $display
        ->save();
    }
    if ($source_field
      ->isDisplayConfigurable('view')) {
      $display = $this->entityDisplayRepository
        ->getViewDisplay('media', $media_type
        ->id());

      // Remove all default components.
      foreach (array_keys($display
        ->getComponents()) as $name) {
        $display
          ->removeComponent($name);
      }
      $source
        ->prepareViewDisplay($media_type, $display);
      $display
        ->save();
    }
  }
  $t_args = [
    '%name' => $media_type
      ->label(),
  ];
  if ($status === SAVED_UPDATED) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The media type %name has been updated.', $t_args));
  }
  elseif ($status === SAVED_NEW) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The media type %name has been added.', $t_args));
    $this
      ->logger('media')
      ->notice('Added media type %name.', $t_args);
  }

  // Override the "status" base field default value, for this media type.
  $fields = $this->entityFieldManager
    ->getFieldDefinitions('media', $media_type
    ->id());

  /** @var \Drupal\media\MediaInterface $media */
  $media = $this->entityTypeManager
    ->getStorage('media')
    ->create([
    'bundle' => $media_type
      ->id(),
  ]);
  $value = (bool) $form_state
    ->getValue([
    'options',
    'status',
  ]);
  if ($media->status->value != $value) {
    $fields['status']
      ->getConfig($media_type
      ->id())
      ->setDefaultValue($value)
      ->save();
  }
  $form_state
    ->setRedirectUrl($media_type
    ->toUrl('collection'));
}