README.md | 7 ++--- entity_embed.module | 30 ++++++++++------------ .../ImageFieldFormatterTest.php | 14 +++++----- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 821b34e..26cc20c 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,10 @@ different Entity Embed Display plugins out of the box: formatter. This will only work if the entity is a file entity type. - image:_formatter_id_: Renders the entity using a specific Image field formatter. This will only work if the entity is a file entity type, - and the file is an image. For the alt and title text to save, the "alt" - and "title" attributes must be included within the drupal-entity tag within - the allowed_html within the editor. + and the file is an image. For the alt and title text to save, the `alt` + and `title` attributes must be allowed on the `` HTML tag + in the "Allowed HTML tags" for text formats that have the "Limit allowed + HTML tags and correct faulty HTML" filter enabled. Configuration for the Entity Embed Display plugin can be provided by using a `data-entity-embed-display-settings` attribute, which contains a diff --git a/entity_embed.module b/entity_embed.module index fcf48f7..e76aa78 100644 --- a/entity_embed.module +++ b/entity_embed.module @@ -101,7 +101,7 @@ function entity_embed_form_filter_format_edit_form_alter(array &$form, FormState } /** - * Validate callback to ensure order of filters does not break anything. + * Validate callback to ensure filter order and allowed_html are compatible. */ function entity_embed_filter_format_edit_form_validate($form, FormStateInterface $form_state) { // This validate handler is not applicable when using the 'Configure' button. @@ -109,29 +109,28 @@ function entity_embed_filter_format_edit_form_validate($form, FormStateInterface return; } - $allowedHtmlPath = [ + $allowed_html_path = [ 'filters', 'filter_html', 'settings', 'allowed_html', ]; - $buttonGroupPath = [ + $button_group_path = [ 'editor', 'settings', 'toolbar', 'button_groups', ]; - $filterHtmlSettingsPath = [ + $filter_html_settings_path = [ 'filters', 'filter_html', 'settings', ]; - if ($allowed_html = $form_state->getValue($allowedHtmlPath)) { - - if ($button_groups = $form_state->getValue($buttonGroupPath)) { + if ($allowed_html = $form_state->getValue($allowed_html_path)) { + if ($button_groups = $form_state->getValue($button_group_path)) { $buttons = []; $button_groups = json_decode($button_groups, TRUE); if (!empty($button_groups[0])) { @@ -145,9 +144,9 @@ function entity_embed_filter_format_edit_form_validate($form, FormStateInterface /** @var \Drupal\filter\Entity\FilterFormat $filter_format */ $filter_format = $form_state->getFormObject()->getEntity(); - $filterHtml = $filter_format->filters()->get('filter_html'); - $filterHtml->setConfiguration(['settings' => $form_state->getValue($filterHtmlSettingsPath)]); - $restrictions = $filterHtml->getHTMLRestrictions(); + $filter_html = $filter_format->filters()->get('filter_html'); + $filter_html->setConfiguration(['settings' => $form_state->getValue($filter_html_settings_path)]); + $restrictions = $filter_html->getHTMLRestrictions(); $allowed = $restrictions['allowed']; $embeds = \Drupal::entityTypeManager() @@ -159,16 +158,15 @@ function entity_embed_filter_format_edit_form_validate($form, FormStateInterface if ($embed->getTypeId() != 'entity') { continue; } - // Require drupal-entity element. + // Require `` HTML tag. if (empty($allowed['drupal-entity'])) { - $form_state->setError($form['filters']['settings']['filter_html']['allowed_html'], t('The %embed button requires "drupal-entity" among the allowed html tags.', [ + $form_state->setError($form['filters']['settings']['filter_html']['allowed_html'], t('The %embed button requires <drupal-entity> among the allowed HTML tags.', [ '%embed' => $embed->label(), ])); break; } else { - - $attributes = [ + $required_attributes = [ 'data-entity-type', 'data-entity-uuid', 'data-entity-embed-display', @@ -180,8 +178,8 @@ function entity_embed_filter_format_edit_form_validate($form, FormStateInterface 'title', ]; - if ($missing_attributes = array_diff($attributes, array_keys($allowed['drupal-entity']))) { - $form_state->setError($form['filters']['settings']['filter_html']['allowed_html'], t('The "drupal-entity" tag within the allowed html tags is missing the following attributes: %list.', [ + if ($missing_attributes = array_diff($required_attributes, array_keys($allowed['drupal-entity']))) { + $form_state->setError($form['filters']['settings']['filter_html']['allowed_html'], t('The <drupal-entity> tag in the allowed HTML tags is missing the following attributes: %list.', [ '%list' => implode(', ', $missing_attributes), ])); break; diff --git a/tests/src/FunctionalJavascript/ImageFieldFormatterTest.php b/tests/src/FunctionalJavascript/ImageFieldFormatterTest.php index 763b6b8..0ff57ea 100644 --- a/tests/src/FunctionalJavascript/ImageFieldFormatterTest.php +++ b/tests/src/FunctionalJavascript/ImageFieldFormatterTest.php @@ -161,8 +161,6 @@ class ImageFieldFormatterTest extends WebDriverTestBase { $page->checkField('filters[entity_embed][status]'); $page->checkField('filters[filter_html][status]'); $this->assertSession()->buttonExists('Show row weights')->press(); - // Currently there is a bug where you must enable filter_align and caption - // and have them in a certain order. $page->selectFieldOption('filters[entity_embed][weight]', '0'); $this->assertSession()->buttonExists('Save configuration')->press(); $this->assertSession()->responseContains('The text format Embed format has been updated.'); @@ -226,17 +224,19 @@ class ImageFieldFormatterTest extends WebDriverTestBase { $drupal_entity_no_entity_type = ''; $drupal_entity_with_alt_title = ''; - // Verify error message when drupal-entity missing but entity embed button included. + // Verify error message when `` absent but `image_embed` + // button in the active toolbar. $allowed_html_field->setValue($base_tags); $this->assertSession()->buttonExists('Save configuration')->press(); - $this->assertSession()->responseContains('The Image Embed button requires "drupal-entity" among the allowed html tags.'); + $this->assertSession()->responseContains('The Image Embed button requires <drupal-entity> among the allowed HTML tags.'); - // Verify error message when drupal-entity present alt and title missing but image entity embed button included. + // Verify error message when `` present, `alt` and `title` + // absent, but `image_embed` button in the active toolbar. $allowed_html_field->setValue($base_tags . ' ' . $drupal_entity_no_entity_type); $this->assertSession()->buttonExists('Save configuration')->press(); - $this->assertSession()->responseContains('The "drupal-entity" tag within the allowed html tags is missing the following attributes: data-entity-type.'); + $this->assertSession()->responseContains('The <drupal-entity> tag in the allowed HTML tags is missing the following attributes: data-entity-type.'); - // Verify if issues fixed, form submits properly. + // Verify if validation errors fixed, form is submitted successfully. $allowed_html_field->setValue($base_tags . ' ' . $drupal_entity_with_alt_title); $this->assertSession()->buttonExists('Save configuration')->press(); $this->assertSession()->responseContains('The text format Embed format has been updated.');