3 namespace Drupal\media_entity;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\entity\Form\RevisionableContentEntityForm;
9 * Form controller for the media edit forms.
11 class MediaForm extends RevisionableContentEntityForm {
14 * Default settings for this media bundle.
21 * The entity being used by this form.
23 * @var \Drupal\media_entity\Entity\Media
30 protected function prepareEntity() {
31 parent::prepareEntity();
32 $media = $this->entity;
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);
44 public function form(array $form, FormStateInterface $form_state) {
45 $form = parent::form($form, $form_state);
47 // Media author information for administrators.
48 if (isset($form['uid']) || isset($form['created'])) {
51 '#title' => $this->t('Authoring information'),
52 '#group' => 'advanced',
54 'class' => ['node-form-author'],
57 'library' => ['node/drupal.node'],
64 if (isset($form['uid'])) {
65 $form['uid']['#group'] = 'author';
68 if (isset($form['created'])) {
69 $form['created']['#group'] = 'author';
72 $form['#attached']['library'][] = 'node/form';
74 $form['#entity_builders']['update_status'] = [$this, 'updateStatus'];
82 protected function actions(array $form, FormStateInterface $form_state) {
83 $element = parent::actions($form, $form_state);
84 $media = $this->entity;
86 // Add a "Publish" button.
87 $element['publish'] = $element['submit'];
88 // If the "Publish" button is clicked, we want to update the status to
90 $element['publish']['#published_status'] = TRUE;
91 $element['publish']['#dropbutton'] = 'save';
92 if ($media->isNew()) {
93 $element['publish']['#value'] = $this->t('Save and publish');
96 $element['publish']['#value'] = $media->isPublished() ? $this->t('Save and keep published') : $this->t('Save and publish');
98 $element['publish']['#weight'] = 0;
100 // Add a "Unpublish" button.
101 $element['unpublish'] = $element['submit'];
102 // If the "Unpublish" button is clicked, we want to update the status to
104 $element['unpublish']['#published_status'] = FALSE;
105 $element['unpublish']['#dropbutton'] = 'save';
106 if ($media->isNew()) {
107 $element['unpublish']['#value'] = $this->t('Save as unpublished');
110 $element['unpublish']['#value'] = !$media->isPublished() ? $this->t('Save and keep unpublished') : $this->t('Save and unpublish');
112 $element['unpublish']['#weight'] = 10;
114 // If already published, the 'publish' button is primary.
115 if ($media->isPublished()) {
116 unset($element['unpublish']['#button_type']);
118 // Otherwise, the 'unpublish' button is primary and should come first.
120 unset($element['publish']['#button_type']);
121 $element['unpublish']['#weight'] = -10;
124 // Remove the "Save" button.
125 $element['submit']['#access'] = FALSE;
127 $element['delete']['#access'] = $media->access('delete');
128 $element['delete']['#weight'] = 100;
134 * Entity builder updating the media status with the submitted value.
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.
141 * The complete form array.
142 * @param \Drupal\Core\Form\FormStateInterface $form_state
143 * The current state of the form.
145 * @see \Drupal\media\MediaForm::form()
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']);