Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / media / src / MediaForm.php
1 <?php
2
3 namespace Drupal\media;
4
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form controller for the media edit forms.
10  *
11  * @internal
12  */
13 class MediaForm extends ContentEntityForm {
14
15   /**
16    * {@inheritdoc}
17    */
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;
22
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(),
27       ]);
28     }
29
30     // Media author information for administrators.
31     if (isset($form['uid']) || isset($form['created'])) {
32       $form['author'] = [
33         '#type' => 'details',
34         '#title' => $this->t('Authoring information'),
35         '#group' => 'advanced',
36         '#attributes' => [
37           'class' => ['media-form-author'],
38         ],
39         '#weight' => 90,
40         '#optional' => TRUE,
41       ];
42     }
43
44     if (isset($form['uid'])) {
45       $form['uid']['#group'] = 'author';
46     }
47
48     if (isset($form['created'])) {
49       $form['created']['#group'] = 'author';
50     }
51
52     $form['#attached']['library'][] = 'media/form';
53
54     return $form;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
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()];
65
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));
69     }
70     else {
71       $logger->notice('@type: updated %label.', $context);
72       $this->messenger()->addStatus($this->t('@type %label has been updated.', $t_args));
73     }
74
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
77     // item.
78     if ($this->currentUser()->hasPermission('access media overview')) {
79       $form_state->setRedirectUrl($this->entity->toUrl('collection'));
80     }
81     else {
82       $form_state->setRedirectUrl($this->entity->toUrl());
83     }
84
85     return $saved;
86   }
87
88 }