Created
November 25, 2020 10:40
-
-
Save podarok/47b9e38df6570a31d1ea87fea250d6c2 to your computer and use it in GitHub Desktop.
Drupal entity_embed 1.1 backward compatibility patch for https://www.drupal.org/project/entity_embed/issues/2511404#comment-12129724
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/entity_embed.module b/entity_embed.module | |
index a531922..e913005 100644 | |
--- a/entity_embed.module | |
+++ b/entity_embed.module | |
@@ -10,6 +10,10 @@ use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Field\FieldItemListInterface; | |
use Drupal\Core\Entity\ContentEntityInterface; | |
+use Drupal\Component\Utility\UrlHelper; | |
+ | |
+ | |
+ | |
/** | |
* Implements hook_help(). | |
*/ | |
@@ -55,6 +59,11 @@ function template_preprocess_entity_embed_container(array &$variables) { | |
$variables['element'] += ['#attributes' => []]; | |
$variables['attributes'] = $variables['element']['#attributes']; | |
$variables['children'] = $variables['element']['#children']; | |
+ if(isset($variables['element']['#context']['data-entity-embed-display-settings']['link_url'])) { | |
+ $variables['url'] = UrlHelper::filterBadProtocol($variables['element']['#context']['data-entity-embed-display-settings']['link_url']); | |
+ } else { | |
+ $variables['url'] = ''; | |
+ } | |
} | |
/** | |
diff --git a/src/Form/EntityEmbedDialog.php b/src/Form/EntityEmbedDialog.php | |
index cc9daaf..697cdb2 100644 | |
--- a/src/Form/EntityEmbedDialog.php | |
+++ b/src/Form/EntityEmbedDialog.php | |
@@ -191,6 +191,7 @@ class EntityEmbedDialog extends FormBase { | |
'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', | |
'data-entity-embed-display-settings' => isset($form_state->get('entity_element')['data-entity-embed-settings']) ? $form_state->get('entity_element')['data-entity-embed-settings'] : [], | |
]; | |
+ | |
$form_state->set('entity_element', $entity_element); | |
$entity = $this->loadEntityByAttributes($entity_element); | |
$form_state->set('entity', $entity ?: NULL); | |
@@ -500,6 +501,24 @@ class EntityEmbedDialog extends FormBase { | |
$form['attributes']['data-entity-embed-display-settings'] += $display->buildConfigurationForm($form, $form_state); | |
} | |
+ if (isset($form['attributes']['data-entity-embed-display-settings'])) { | |
+ if (isset($entity_element['data-entity-embed-display-settings']) && !is_array($entity_element['data-entity-embed-display-settings'])) { | |
+ $entity_element['data-entity-embed-display-settings'] = Json::decode($entity_element['data-entity-embed-display-settings']); | |
+ } | |
+ // Supress Drupal's "Link image to" dropdown when embedding an image, | |
+ // since the 'Link to' option provides this functionality. | |
+ if (isset($form['attributes']['data-entity-embed-display-settings']['image_link'])) { | |
+ $form['attributes']['data-entity-embed-display-settings']['image_link']['#type'] = 'hidden'; | |
+ $form['attributes']['data-entity-embed-display-settings']['image_link']['#value'] = ''; | |
+ } | |
+ $form['attributes']['data-entity-embed-display-settings']['link_url'] = [ | |
+ '#title' => t('Link to'), | |
+ '#description' => $this->t('DEPRECATED! Keep it empty. Use WYSIWYG Link button.'), | |
+ '#type' => 'textfield', | |
+ '#default_value' => isset($entity_element['data-entity-embed-display-settings']['link_url']) ? Html::decodeEntities($entity_element['data-entity-embed-display-settings']['link_url']) : '', | |
+ ]; | |
+ } | |
+ | |
// When Drupal core's filter_align is being used, the text editor may | |
// offer the ability to change the alignment. | |
if ($editor->getFilterFormat()->filters('filter_align')->status) { | |
@@ -645,6 +664,9 @@ class EntityEmbedDialog extends FormBase { | |
$entity = $form_state->get('entity'); | |
$plugin_id = $entity_element['data-entity-embed-display']; | |
$plugin_settings = !empty($entity_element['data-entity-embed-display-settings']) ? $entity_element['data-entity-embed-display-settings'] : []; | |
+ if (isset($entity_element['data-entity-embed-display-settings']['link_url']) && $entity_element['data-entity-embed-display-settings']['link_url'] != ''){ | |
+ $form_state->setError($entity_element, $this->t('Link to is deprecated. Remove its value and use WYSIWYG Link button functionality over embedded entity.')); | |
+ } | |
$display = $this->entityEmbedDisplayManager->createInstance($plugin_id, $plugin_settings); | |
$display->setContextValue('entity', $entity); | |
$display->setAttributes($entity_element); | |
diff --git a/templates/entity-embed-container.html.twig b/templates/entity-embed-container.html.twig | |
index e3af878..f4d234f 100644 | |
--- a/templates/entity-embed-container.html.twig | |
+++ b/templates/entity-embed-container.html.twig | |
@@ -12,4 +12,4 @@ | |
* @ingroup themeable | |
*/ | |
#} | |
-<div{{ attributes }}>{{ children }}</div> | |
+<article{{ attributes }}>{% if url %}<a href='{{ url }}' {{ url_attributes|default('') }}>{{ children }}</a>{% else %}{{ children }}{% endif %}</article> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment