function MediaForm::save

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

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->info('@type: added %label.', $context);
        $this->messenger()
            ->addStatus($this->t('@type %label has been created.', $t_args));
    }
    else {
        $logger->info('@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;
}

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