Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / media_entity / src / MediaForm.php
1 <?php
2
3 namespace Drupal\media_entity;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\entity\Form\RevisionableContentEntityForm;
7
8 /**
9  * Form controller for the media edit forms.
10  */
11 class MediaForm extends RevisionableContentEntityForm {
12
13   /**
14    * Default settings for this media bundle.
15    *
16    * @var array
17    */
18   protected $settings;
19
20   /**
21    * The entity being used by this form.
22    *
23    * @var \Drupal\media_entity\Entity\Media
24    */
25   protected $entity;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function prepareEntity() {
31     parent::prepareEntity();
32     $media = $this->entity;
33
34     // If this is a new media, fill in the default values.
35     if ($media->isNew()) {
36       $media->setPublisherId($this->currentUser()->id());
37       $media->setCreatedTime(REQUEST_TIME);
38     }
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function form(array $form, FormStateInterface $form_state) {
45     $form = parent::form($form, $form_state);
46
47     // Media author information for administrators.
48     if (isset($form['uid']) || isset($form['created'])) {
49       $form['author'] = [
50         '#type' => 'details',
51         '#title' => $this->t('Authoring information'),
52         '#group' => 'advanced',
53         '#attributes' => [
54           'class' => ['node-form-author'],
55         ],
56         '#attached' => [
57           'library' => ['node/drupal.node'],
58         ],
59         '#weight' => 90,
60         '#optional' => TRUE,
61       ];
62     }
63
64     if (isset($form['uid'])) {
65       $form['uid']['#group'] = 'author';
66     }
67
68     if (isset($form['created'])) {
69       $form['created']['#group'] = 'author';
70     }
71
72     $form['#attached']['library'][] = 'node/form';
73
74     $form['#entity_builders']['update_status'] = [$this, 'updateStatus'];
75
76     return $form;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   protected function actions(array $form, FormStateInterface $form_state) {
83     $element = parent::actions($form, $form_state);
84     $media = $this->entity;
85
86     // Add a "Publish" button.
87     $element['publish'] = $element['submit'];
88     // If the "Publish" button is clicked, we want to update the status to
89     // "published".
90     $element['publish']['#published_status'] = TRUE;
91     $element['publish']['#dropbutton'] = 'save';
92     if ($media->isNew()) {
93       $element['publish']['#value'] = $this->t('Save and publish');
94     }
95     else {
96       $element['publish']['#value'] = $media->isPublished() ? $this->t('Save and keep published') : $this->t('Save and publish');
97     }
98     $element['publish']['#weight'] = 0;
99
100     // Add a "Unpublish" button.
101     $element['unpublish'] = $element['submit'];
102     // If the "Unpublish" button is clicked, we want to update the status to
103     // "unpublished".
104     $element['unpublish']['#published_status'] = FALSE;
105     $element['unpublish']['#dropbutton'] = 'save';
106     if ($media->isNew()) {
107       $element['unpublish']['#value'] = $this->t('Save as unpublished');
108     }
109     else {
110       $element['unpublish']['#value'] = !$media->isPublished() ? $this->t('Save and keep unpublished') : $this->t('Save and unpublish');
111     }
112     $element['unpublish']['#weight'] = 10;
113
114     // If already published, the 'publish' button is primary.
115     if ($media->isPublished()) {
116       unset($element['unpublish']['#button_type']);
117     }
118     // Otherwise, the 'unpublish' button is primary and should come first.
119     else {
120       unset($element['publish']['#button_type']);
121       $element['unpublish']['#weight'] = -10;
122     }
123
124     // Remove the "Save" button.
125     $element['submit']['#access'] = FALSE;
126
127     $element['delete']['#access'] = $media->access('delete');
128     $element['delete']['#weight'] = 100;
129
130     return $element;
131   }
132
133   /**
134    * Entity builder updating the media status with the submitted value.
135    *
136    * @param string $entity_type_id
137    *   The entity type identifier.
138    * @param \Drupal\media_entity\MediaInterface $media
139    *   The media updated with the submitted values.
140    * @param array $form
141    *   The complete form array.
142    * @param \Drupal\Core\Form\FormStateInterface $form_state
143    *   The current state of the form.
144    *
145    * @see \Drupal\media\MediaForm::form()
146    */
147   public function updateStatus($entity_type_id, MediaInterface $media, array $form, FormStateInterface $form_state) {
148     $element = $form_state->getTriggeringElement();
149     if (isset($element['#published_status'])) {
150       $media->setPublished($element['#published_status']);
151     }
152   }
153
154 }