Commit b45a1015 authored by Gábor Hojtsy's avatar Gábor Hojtsy

Issue #2863431 by Manuel Garcia, seanB, naveenvalecha, Gábor Hojtsy, Berdir,...

Issue #2863431 by Manuel Garcia, seanB, naveenvalecha, Gábor Hojtsy, Berdir, tstoeckler, timmillwood, tacituseu, marcoscano: Change "Save and keep un-/published" buttons for media module
parent 3ecc833e
......@@ -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
......
......@@ -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
......
......@@ -424,6 +424,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
])
->setDisplayConfigurable('view', TRUE);
$fields['status']
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 100,
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time the media item was created.'))
......
......@@ -49,89 +49,9 @@ public function form(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = '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}
*/
......
......@@ -85,7 +85,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.
......@@ -100,7 +100,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);
// Confirm the correct revision title appears on "view revisions" page.
......@@ -132,7 +132,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.
......@@ -147,7 +147,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);
// Confirm the correct revision title appears on "view revisions" page.
......
......@@ -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');
......
......@@ -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');
}
......
......@@ -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');
......
.media-form .field--name-status {
margin-top: 1.5em;
}
......@@ -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/form
......@@ -186,3 +186,14 @@ 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) {
// @todo Revisit after https://www.drupal.org/node/2892304 is in. It
// introduces a footer region to these forms which will allow for us to
// display a top border over the published checkbox by defining a
// media-edit-form.html.twig template the same way node does.
$form['#attached']['library'][] = 'seven/media-form';
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment