You are here

public function MediaForm::save in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/src/MediaForm.php \Drupal\media\MediaForm::save()
  2. 9 core/modules/media/src/MediaForm.php \Drupal\media\MediaForm::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/MediaForm.php, line 60

Class

MediaForm
Form controller for the media edit forms.

Namespace

Drupal\media

Code

public function save(array $form, FormStateInterface $form_state) {
  $saved = parent::save($form, $form_state);
  $context = [
    '@type' => $this->entity
      ->bundle(),
    '%label' => $this->entity
      ->label(),
    'link' => $this->entity
      ->toLink($this
      ->t('View'))
      ->toString(),
  ];
  $logger = $this
    ->logger('media');
  $t_args = [
    '@type' => $this->entity->bundle->entity
      ->label(),
    '%label' => $this->entity
      ->toLink($this->entity
      ->label())
      ->toString(),
  ];
  if ($saved === SAVED_NEW) {
    $logger
      ->notice('@type: added %label.', $context);
    $this
      ->messenger()
      ->addStatus($this
      ->t('@type %label has been created.', $t_args));
  }
  else {
    $logger
      ->notice('@type: updated %label.', $context);
    $this
      ->messenger()
      ->addStatus($this
      ->t('@type %label has been updated.', $t_args));
  }

  // Redirect the user to the media overview if the user has the 'access media
  // overview' permission. If not, redirect to the canonical URL of the media
  // item.
  if ($this
    ->currentUser()
    ->hasPermission('access media overview')) {
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl('collection'));
  }
  else {
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl());
  }
  return $saved;
}