function MediaTypeForm::save

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

Overrides EntityForm::save

File

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

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'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.