3 namespace Drupal\media;
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Form\FormStateInterface;
9 * Form controller for the media edit forms.
13 class MediaForm extends ContentEntityForm {
18 public function form(array $form, FormStateInterface $form_state) {
19 $form = parent::form($form, $form_state);
20 /** @var \Drupal\media\MediaTypeInterface $media_type */
21 $media_type = $this->entity->bundle->entity;
23 if ($this->operation === 'edit') {
24 $form['#title'] = $this->t('Edit %type_label @label', [
25 '%type_label' => $media_type->label(),
26 '@label' => $this->entity->label(),
30 // Media author information for administrators.
31 if (isset($form['uid']) || isset($form['created'])) {
34 '#title' => $this->t('Authoring information'),
35 '#group' => 'advanced',
37 'class' => ['media-form-author'],
44 if (isset($form['uid'])) {
45 $form['uid']['#group'] = 'author';
48 if (isset($form['created'])) {
49 $form['created']['#group'] = 'author';
52 $form['#attached']['library'][] = 'media/form';
60 public function save(array $form, FormStateInterface $form_state) {
61 $saved = parent::save($form, $form_state);
62 $context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label(), 'link' => $this->entity->toLink($this->t('View'))->toString()];
63 $logger = $this->logger('media');
64 $t_args = ['@type' => $this->entity->bundle->entity->label(), '%label' => $this->entity->toLink($this->entity->label())->toString()];
66 if ($saved === SAVED_NEW) {
67 $logger->notice('@type: added %label.', $context);
68 $this->messenger()->addStatus($this->t('@type %label has been created.', $t_args));
71 $logger->notice('@type: updated %label.', $context);
72 $this->messenger()->addStatus($this->t('@type %label has been updated.', $t_args));
75 // Redirect the user to the media overview if the user has the 'access media
76 // overview' permission. If not, redirect to the canonical URL of the media
78 if ($this->currentUser()->hasPermission('access media overview')) {
79 $form_state->setRedirectUrl($this->entity->toUrl('collection'));
82 $form_state->setRedirectUrl($this->entity->toUrl());