Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Created January 14, 2019 18:01
Show Gist options
  • Select an option

  • Save ahebrank/f582e2998ce18643e9cfb33d3d0c8b42 to your computer and use it in GitHub Desktop.

Select an option

Save ahebrank/f582e2998ce18643e9cfb33d3d0c8b42 to your computer and use it in GitHub Desktop.
diff --git a/config/schema/embederator.schema.yml b/config/schema/embederator.schema.yml
index bd55b90..78a9222 100644
--- a/config/schema/embederator.schema.yml
+++ b/config/schema/embederator.schema.yml
@@ -30,3 +30,11 @@ embederator.embederator_type.*:
wrapper_class:
type: string
label: 'Wrapper class for template'
+
+field.formatter.settings.embederator_default:
+ type: mapping
+ label: 'Embederator append unique DOM ID'
+ mapping:
+ append_unique_id:
+ type: boolean
+ label: 'Append unique DOM ID'
diff --git a/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php b/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php
index 305e71b..9872b63 100644
--- a/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php
@@ -5,6 +5,7 @@ namespace Drupal\embederator\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Component\Utility\Html;
/**
* Plugin implementation of the default embederator (token replacing) formatter.
@@ -41,6 +42,7 @@ class EmbederatorFormatter extends FormatterBase {
try {
$response = $client->request('GET', $url);
$markup = (string) $response->getBody();
+ $markup = $this->uniquify($markup);
}
catch (Exception $e) {
$markup = '<p>Unable to load ' . $url . '</p>';
@@ -58,6 +60,7 @@ class EmbederatorFormatter extends FormatterBase {
$elements = [];
foreach ($items as $delta => $item) {
$markup = $token->replace($embed_pattern_field['value'], ['embederator' => $entity]);
+ $markup = $this->uniquify($markup);
$elements[$delta] = [
'#type' => 'processed_text',
'#text' => $markup,
@@ -80,4 +83,66 @@ class EmbederatorFormatter extends FormatterBase {
&& ($field_definition->getName() == 'embed_id'));
}
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsSummary() {
+ $summary = [];
+ if ($this->getSetting('append_unique_id')) {
+ $summary[] = $this->t('Append unique identifier to DOM IDs.');
+ }
+ return $summary;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultSettings() {
+ return [
+ 'append_unique_id' => FALSE,
+ ] + parent::defaultSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ $element['append_unique_id'] = [
+ '#title' => $this->t('Append unique DOM ID'),
+ '#type' => 'checkbox',
+ '#default_value' => $this->getSetting('append_unique_id'),
+ ];
+
+ return $element;
+ }
+
+ /**
+ * Add a random suffix to ID attributes in DOM markup.
+ *
+ * See e.g., https://api.drupal.org/api/drupal/core%21modules%21filter%21src%21Plugin%21Filter%21FilterHtml.php/function/FilterHtml%3A%3AfilterAttributes/8.2.x.
+ */
+ protected function uniquify($html) {
+ if ($this->getSetting('append_unique_id')) {
+ $suffix = uniqid();
+
+ // Collect all the IDs and make their replacements.
+ $html_dom = Html::load($html);
+ $xpath = new \DOMXPath($html_dom);
+ foreach ($xpath->query('//*[@id]') as $element) {
+ $orig_id = $element->getAttribute('id');
+ $new_id = $orig_id . "-" . $suffix;
+ foreach ($xpath->query('//*[@for="' . $orig_id . '"]') as $for_element) {
+ $for_element->setAttribute('for', $new_id);
+ }
+ $element->setAttribute('id', $new_id);
+ }
+
+ $text = Html::serialize($html_dom);
+ $html = trim($text);
+ }
+
+ // Run the replacements on the markup.
+ return $html;
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment