diff --git a/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php b/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php index eeac1ce..88f0691 100644 --- a/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php +++ b/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php @@ -63,14 +63,16 @@ public function testPageAccess() { // Access the node form before moderation is enabled, the publication state // should now be visible. $this->drupalGet('node/add/moderated_content'); - $this->assertSession()->fieldExists('Published'); + $status_field = $this->assertSession()->fieldExists('Published'); + $this->assertFalse($status_field->hasAttribute('disabled')); // Now enable the workflow. $this->enableModerationThroughUi('moderated_content', 'editorial'); - // Access that the status field is no longer visible. + // Assert that the status field is no longer enabled. $this->drupalGet('node/add/moderated_content'); - $this->assertSession()->fieldNotExists('Published'); + $this->assertSession()->elementAttributeExists('css', '[data-drupal-selector="edit-status-value"]', 'disabled'); + $this->assertSession()->elementAttributeContains('css', '[data-drupal-selector="edit-status-value"]', 'disabled', 'disabled'); // Create a node to test with. $this->drupalPostForm(NULL, [ diff --git a/core/modules/media/config/install/core.entity_form_display.media.file.default.yml b/core/modules/media/config/install/core.entity_form_display.media.file.default.yml index 15de2ac..50d57bc 100644 --- a/core/modules/media/config/install/core.entity_form_display.media.file.default.yml +++ b/core/modules/media/config/install/core.entity_form_display.media.file.default.yml @@ -32,6 +32,13 @@ content: size: 60 placeholder: '' third_party_settings: { } + status: + type: boolean_checkbox + settings: + display_label: true + weight: 100 + region: content + third_party_settings: { } uid: type: entity_reference_autocomplete weight: 5 diff --git a/core/modules/media/config/install/core.entity_form_display.media.image.default.yml b/core/modules/media/config/install/core.entity_form_display.media.image.default.yml index 7806770..6f2323b 100644 --- a/core/modules/media/config/install/core.entity_form_display.media.image.default.yml +++ b/core/modules/media/config/install/core.entity_form_display.media.image.default.yml @@ -34,6 +34,13 @@ content: size: 60 placeholder: '' third_party_settings: { } + status: + type: boolean_checkbox + settings: + display_label: true + weight: 100 + region: content + third_party_settings: { } uid: type: entity_reference_autocomplete weight: 5 diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index 8584041..5c68552 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -423,6 +423,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ]) ->setDisplayConfigurable('view', TRUE); + $fields['status'] + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'settings' => [ + 'display_label' => TRUE, + ], + 'weight' => 120, + ]) + ->setDisplayConfigurable('form', TRUE); + $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Authored on')) ->setDescription(t('The time the media item was created.')) diff --git a/core/modules/media/src/MediaForm.php b/core/modules/media/src/MediaForm.php index 1e91e9f..d20211d 100644 --- a/core/modules/media/src/MediaForm.php +++ b/core/modules/media/src/MediaForm.php @@ -49,92 +49,12 @@ public function form(array $form, FormStateInterface $form_state) { $form['#attached']['library'][] = 'media/media_form'; - $form['#entity_builders']['update_status'] = [$this, 'updateStatus']; - return $form; } /** * {@inheritdoc} */ - protected function actions(array $form, FormStateInterface $form_state) { - $element = parent::actions($form, $form_state); - $media = $this->entity; - - // Add a "Publish" button. - $element['publish'] = $element['submit']; - // If the "Publish" button is clicked, we want to update the status to - // "published". - $element['publish']['#published_status'] = TRUE; - $element['publish']['#dropbutton'] = 'save'; - if ($media->isNew()) { - $element['publish']['#value'] = $this->t('Save and publish'); - } - else { - $element['publish']['#value'] = $media->isPublished() ? $this->t('Save and keep published') : $this->t('Save and publish'); - } - $element['publish']['#weight'] = 0; - - // Add a "Unpublish" button. - $element['unpublish'] = $element['submit']; - // If the "Unpublish" button is clicked, we want to update the status to - // "unpublished". - $element['unpublish']['#published_status'] = FALSE; - $element['unpublish']['#dropbutton'] = 'save'; - if ($media->isNew()) { - $element['unpublish']['#value'] = $this->t('Save as unpublished'); - } - else { - $element['unpublish']['#value'] = !$media->isPublished() ? $this->t('Save and keep unpublished') : $this->t('Save and unpublish'); - } - $element['unpublish']['#weight'] = 10; - - // If already published, the 'publish' button is primary. - if ($media->isPublished()) { - $element['publish']['#button_type'] = 'primary'; - } - // Otherwise, the 'unpublish' button is primary and should come first. - else { - $element['unpublish']['#button_type'] = 'primary'; - $element['unpublish']['#weight'] = -10; - } - - // Remove the "Save" button. - $element['submit']['#access'] = FALSE; - - $element['delete']['#access'] = $media->access('delete'); - $element['delete']['#weight'] = 100; - - return $element; - } - - /** - * Entity builder updating the media status with the submitted value. - * - * @param string $entity_type_id - * The entity type identifier. - * @param \Drupal\media\MediaInterface $media - * The media updated with the submitted values. - * @param array $form - * The complete form array. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @see \Drupal\media\MediaForm::form() - */ - public function updateStatus($entity_type_id, MediaInterface $media, array $form, FormStateInterface $form_state) { - $element = $form_state->getTriggeringElement(); - if (!empty($element['#published_status'])) { - $media->setPublished(); - } - else { - $media->setUnpublished(); - } - } - - /** - * {@inheritdoc} - */ public function save(array $form, FormStateInterface $form_state) { $saved = parent::save($form, $form_state); $context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label()]; diff --git a/core/modules/media/tests/src/Functional/MediaRevisionTest.php b/core/modules/media/tests/src/Functional/MediaRevisionTest.php index ce1897f..6e409b3 100644 --- a/core/modules/media/tests/src/Functional/MediaRevisionTest.php +++ b/core/modules/media/tests/src/Functional/MediaRevisionTest.php @@ -26,7 +26,7 @@ public function testFileMediaRevision() { $page = $this->getSession()->getPage(); $page->fillField('Name', 'Foobar'); $page->attachFileToField('File', $this->container->get('file_system')->realpath($uri)); - $page->pressButton('Save and publish'); + $page->pressButton('Save'); $assert->addressMatches('/^\/media\/[0-9]+$/'); // The media item was just created, so it should only have one revision. @@ -41,7 +41,7 @@ public function testFileMediaRevision() { $assert->checkboxChecked('Create new revision'); $page = $this->getSession()->getPage(); $page->fillField('Name', 'Foobaz'); - $page->pressButton('Save and keep published'); + $page->pressButton('Save'); $this->assertRevisionCount($media, 2); } @@ -66,7 +66,7 @@ public function testImageMediaRevision() { $page = $this->getSession()->getPage(); $page->fillField('Name', 'Foobar'); $page->attachFileToField('Image', \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg'); - $page->pressButton('Save and publish'); + $page->pressButton('Save'); $assert->addressMatches('/^\/media\/[0-9]+$/'); // The media item was just created, so it should only have one revision. @@ -81,7 +81,7 @@ public function testImageMediaRevision() { $assert->checkboxChecked('Create new revision'); $page = $this->getSession()->getPage(); $page->fillField('Name', 'Foobaz'); - $page->pressButton('Save and keep published'); + $page->pressButton('Save'); $this->assertRevisionCount($media, 2); } diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php index 8f3f675..4caab94 100644 --- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php +++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php @@ -59,7 +59,7 @@ public function testMediaWithOnlyOneMediaType() { $page->fillField('name[0][value]', $media_name); $revision_log_message = $this->randomString(); $page->fillField('revision_log_message[0][value]', $revision_log_message); - $page->pressButton('Save and publish'); + $page->pressButton('Save'); $media_id = $this->container->get('entity.query')->get('media')->execute(); $media_id = reset($media_id); /** @var \Drupal\media\MediaInterface $media */ @@ -78,7 +78,7 @@ public function testMediaWithOnlyOneMediaType() { $assert_session->checkboxNotChecked('edit-revision'); $media_name = $this->randomMachineName(); $page->fillField('name[0][value]', $media_name2); - $page->pressButton('Save and keep published'); + $page->pressButton('Save'); /** @var \Drupal\media\MediaInterface $media */ $media = $this->container->get('entity_type.manager') ->getStorage('media') @@ -109,7 +109,7 @@ public function testMediaWithOnlyOneMediaType() { $assert_session->checkboxChecked('edit-revision'); $page->fillField('name[0][value]', $media_name); $page->fillField('revision_log_message[0][value]', $revision_log_message); - $page->pressButton('Save and keep published'); + $page->pressButton('Save'); $assert_session->titleEquals($media_name . ' | Drupal'); /** @var \Drupal\media\MediaInterface $media */ $media = $this->container->get('entity_type.manager') @@ -118,6 +118,16 @@ public function testMediaWithOnlyOneMediaType() { $this->assertEquals($media->getRevisionLogMessage(), $revision_log_message); $this->assertNotEquals($previous_revision_id, $media->getRevisionId()); + // Test the status checkbox. + $this->drupalGet('media/' . $media_id . '/edit'); + $page->uncheckField('status[value]'); + $page->pressButton('Save'); + /** @var \Drupal\media\MediaInterface $media */ + $media = $this->container->get('entity_type.manager') + ->getStorage('media') + ->loadUnchanged($media_id); + $this->assertFalse($media->isPublished()); + // Tests media delete form. $this->drupalGet('media/' . $media_id . '/edit'); $page->clickLink('Delete'); diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php index 8c80b83..9cf467b 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php @@ -47,7 +47,7 @@ public function testMediaFileSource() { $this->drupalGet("media/add/{$media_type_id}"); $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath)); $assert_session->assertWaitOnAjaxRequest(); - $page->pressButton('Save and publish'); + $page->pressButton('Save'); $assert_session->addressEquals('media/1'); @@ -64,7 +64,7 @@ public function testMediaFileSource() { $this->drupalGet("media/add/{$media_type_id}"); $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath)); $assert_session->assertWaitOnAjaxRequest(); - $page->pressButton('Save and publish'); + $page->pressButton('Save'); $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'text--plain.png'); } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php index 0505d3b..1aa3fe0 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php @@ -62,7 +62,7 @@ public function testMediaImageSource() { $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg'); $assert_session->assertWaitOnAjaxRequest(); $page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1'); - $page->pressButton('Save and publish'); + $page->pressButton('Save'); $assert_session->addressEquals('media/1'); diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php index 007b7c6..b0b39b6 100644 --- a/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -111,6 +111,13 @@ public function form(array $form, FormStateInterface $form_state) { ]; $form['status']['#group'] = 'footer'; + // Show the status checkbox but make it disabled if user has no access. + if (!$form['status']['#access']) { + $form['status']['#access'] = TRUE; + $form['status']['#disabled'] = TRUE; + $form['status']['widget']['value']['#description'] = t('You can not change the published status.'); + } + // Node author information for administrators. $form['author'] = [ '#type' => 'details', diff --git a/core/themes/seven/css/components/media.css b/core/themes/seven/css/components/media.css new file mode 100644 index 0000000..34b1599 --- /dev/null +++ b/core/themes/seven/css/components/media.css @@ -0,0 +1,3 @@ +.media-form .field--name-status { + margin-top: 1.5em; +} diff --git a/core/themes/seven/seven.libraries.yml b/core/themes/seven/seven.libraries.yml index 424e7f5..8f09764 100644 --- a/core/themes/seven/seven.libraries.yml +++ b/core/themes/seven/seven.libraries.yml @@ -119,3 +119,11 @@ tour-styling: css: theme: css/components/tour.theme.css: {} + +media-form: + version: VERSION + css: + layout: + css/components/media.css: {} + dependencies: + - media/media_form diff --git a/core/themes/seven/seven.theme b/core/themes/seven/seven.theme index 91c03e6..bea2465 100644 --- a/core/themes/seven/seven.theme +++ b/core/themes/seven/seven.theme @@ -186,3 +186,10 @@ function seven_form_node_form_alter(&$form, FormStateInterface $form_state) { $form['revision_information']['#type'] = 'container'; $form['revision_information']['#group'] = 'meta'; } + +/** + * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\media\MediaForm. + */ +function seven_form_media_form_alter(&$form, FormStateInterface $form_state) { + $form['#attached']['library'][] = 'seven/media-form'; +}