src/EntityEmbedDisplay/EntityEmbedDisplayBase.php | 2 +- src/Plugin/Derivative/FieldFormatterDeriver.php | 5 ++-- .../EntityEmbedDisplay/ImageFieldFormatter.php | 11 ++------ .../EntityEmbedDisplay/MediaImageDecorator.php | 3 +- .../FunctionalJavascript/EntityEmbedTestBase.php | 6 ++-- tests/src/FunctionalJavascript/MediaImageTest.php | 32 +++++++++------------- 6 files changed, 24 insertions(+), 35 deletions(-) diff --git a/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php b/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php index df0e3aa..3cb86cd 100644 --- a/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php +++ b/src/EntityEmbedDisplay/EntityEmbedDisplayBase.php @@ -324,7 +324,7 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac } /** - * Check if an attribute is set. + * Checks if an attribute is set. * * @param string $name * The name of the attribute. diff --git a/src/Plugin/Derivative/FieldFormatterDeriver.php b/src/Plugin/Derivative/FieldFormatterDeriver.php index 775dfbe..d420acc 100644 --- a/src/Plugin/Derivative/FieldFormatterDeriver.php +++ b/src/Plugin/Derivative/FieldFormatterDeriver.php @@ -75,8 +75,9 @@ class FieldFormatterDeriver extends DeriverBase implements ContainerDeriverInter $this->derivatives[$formatter] = $base_plugin_definition; $this->derivatives[$formatter]['label'] = $label; - // Set plugins we know don't need alt and title to not use - // MediaImageDecorator. + // The base entity embed display plugin annotation has opted into + // `supports_image_alt_and_title`. For some derivatives we know that they + // do not support this, so opt them back out. if (in_array($formatter, $no_media_image_decorator, TRUE)) { $this->derivatives[$formatter]['supports_image_alt_and_title'] = FALSE; } diff --git a/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php b/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php index 48c7351..c175ded 100644 --- a/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php +++ b/src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php @@ -29,13 +29,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class ImageFieldFormatter extends FileFieldFormatter { - /** - * A string that signifies not the render the alt text. - * - * @const string - */ - const EMPTY_STRING = '""'; - /** * The image factory. * @@ -185,7 +178,7 @@ class ImageFieldFormatter extends FileFieldFormatter { // double quotes in place of empty alt text only if that was filled // intentionally by the user. if (!empty($entity_element) && $entity_element['data-entity-embed-display'] == 'image:image') { - $alt = static::EMPTY_STRING; + $alt = MediaImageDecorator::EMPTY_STRING; } } @@ -218,7 +211,7 @@ class ImageFieldFormatter extends FileFieldFormatter { public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { // When the alt attribute is set to two double quotes, transform it to the // empty string: two double quotes signify "empty alt attribute". See above. - if (trim($form_state->getValue(['attributes', 'alt'])) === static::EMPTY_STRING) { + if (trim($form_state->getValue(['attributes', 'alt'])) === MediaImageDecorator::EMPTY_STRING) { $form_state->setValue(['attributes', 'alt'], ''); } } diff --git a/src/Plugin/entity_embed/EntityEmbedDisplay/MediaImageDecorator.php b/src/Plugin/entity_embed/EntityEmbedDisplay/MediaImageDecorator.php index bdffa70..9c9e43f 100644 --- a/src/Plugin/entity_embed/EntityEmbedDisplay/MediaImageDecorator.php +++ b/src/Plugin/entity_embed/EntityEmbedDisplay/MediaImageDecorator.php @@ -14,10 +14,11 @@ use Drupal\media\MediaInterface; * Decorator on all EntityEmbedDisplays that adds alt and title overriding. */ class MediaImageDecorator implements EntityEmbedDisplayInterface { + use StringTranslationTrait; /** - * A string that signifies not the render the alt text. + * A string that signifies not to render the alt text. * * @const string */ diff --git a/tests/src/FunctionalJavascript/EntityEmbedTestBase.php b/tests/src/FunctionalJavascript/EntityEmbedTestBase.php index bba01d9..113e7a6 100644 --- a/tests/src/FunctionalJavascript/EntityEmbedTestBase.php +++ b/tests/src/FunctionalJavascript/EntityEmbedTestBase.php @@ -59,15 +59,15 @@ JS; } /** - * Get an embed code with given attributes. + * Creates an embed code with given attributes. * * @param array $attributes * The attributes to add. * * @return string - * A string containing a drupal-entity dom element. + * A string containing a element with the given attributes. */ - protected function getEmbedCode(array $attributes) { + protected function createEmbedCode(array $attributes) { $dom = Html::load('This placeholder should not be rendered.'); $xpath = new \DOMXPath($dom); $drupal_entity = $xpath->query('//drupal-entity')[0]; diff --git a/tests/src/FunctionalJavascript/MediaImageTest.php b/tests/src/FunctionalJavascript/MediaImageTest.php index ffdef5b..f6df168 100644 --- a/tests/src/FunctionalJavascript/MediaImageTest.php +++ b/tests/src/FunctionalJavascript/MediaImageTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\entity_embed\FunctionalJavascript; +use Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay\MediaImageDecorator; use Drupal\field\Entity\FieldConfig; use Drupal\file\Entity\File; use Drupal\media\Entity\Media; @@ -13,13 +14,6 @@ use Drupal\media\Entity\Media; */ class MediaImageTest extends EntityEmbedTestBase { - /** - * A string that signifies not the render the alt text. - * - * @const string - */ - const EMPTY_STRING = '""'; - /** * The user to use during testing. * @@ -46,10 +40,9 @@ class MediaImageTest extends EntityEmbedTestBase { } /** - * Tests the host entity's langcode is available in EntityEmbedDialog. + * Tests alt and title overriding for embedded images. */ public function testAltAndTitle() { - \Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg'); /** @var \Drupal\file\FileInterface $file */ $file = File::create([ @@ -403,8 +396,8 @@ class MediaImageTest extends EntityEmbedTestBase { // Test that setting value to double quote will allow setting the alt // and title to empty. - $alt->setValue(static::EMPTY_STRING); - $title->setValue(static::EMPTY_STRING); + $alt->setValue(MediaImageDecorator::EMPTY_STRING); + $title->setValue(MediaImageDecorator::EMPTY_STRING); $this->submitDialog(); @@ -413,7 +406,7 @@ class MediaImageTest extends EntityEmbedTestBase { $this->assertEmpty($img->getAttribute('title')); // Test the same embed with different alt and title text. - $input = $this->getEmbedCode([ + $input = $this->createEmbedCode([ 'alt' => 'alt 1', 'title' => 'title 1', 'data-embed-button' => 'test_media_entity_embed', @@ -423,7 +416,7 @@ class MediaImageTest extends EntityEmbedTestBase { 'data-entity-uuid' => $media->uuid(), 'data-langcode' => 'en', ]); - $input .= $this->getEmbedCode([ + $input .= $this->createEmbedCode([ 'alt' => 'alt 2', 'title' => 'title 2', 'data-embed-button' => 'test_media_entity_embed', @@ -433,7 +426,7 @@ class MediaImageTest extends EntityEmbedTestBase { 'data-entity-uuid' => $media->uuid(), 'data-langcode' => 'en', ]); - $input .= $this->getEmbedCode([ + $input .= $this->createEmbedCode([ 'alt' => 'alt 3', 'title' => 'title 3', 'data-embed-button' => 'test_media_entity_embed', @@ -490,7 +483,6 @@ class MediaImageTest extends EntityEmbedTestBase { $img = $this->assertSession()->elementExists('xpath', "//img[contains(@alt, 'alt 3')]"); $this->assertEquals('alt 3', $img->getAttribute('alt')); $this->assertEquals('title 3', $img->getAttribute('title')); - } /** @@ -498,10 +490,12 @@ class MediaImageTest extends EntityEmbedTestBase { */ protected function reopenDialog() { $this->getSession()->switchToIFrame(); - $select_and_edit_embed = "var editor = CKEDITOR.instances['edit-body-0-value']; - var entityEmbed = editor.widgets.getByElement(editor.editable().findOne('div')); - entityEmbed.focus(); - editor.execCommand('editdrupalentity');"; + $select_and_edit_embed = <<getSession()->executeScript($select_and_edit_embed); $this->assertSession()->assertWaitOnAjaxRequest(); $this->assertSession()->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');