Created
January 15, 2019 12:56
-
-
Save ahebrank/e9230ecb14f74fbf95ee85d3e7a96577 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/config/schema/embederator.schema.yml b/config/schema/embederator.schema.yml | |
| index bd55b90..9b50b6d 100644 | |
| --- a/config/schema/embederator.schema.yml | |
| +++ b/config/schema/embederator.schema.yml | |
| @@ -30,3 +30,14 @@ embederator.embederator_type.*: | |
| wrapper_class: | |
| type: string | |
| label: 'Wrapper class for template' | |
| + | |
| +field.formatter.settings.embederator_default: | |
| + type: mapping | |
| + label: 'Embederator formatter settings' | |
| + mapping: | |
| + append_unique_id: | |
| + type: boolean | |
| + label: 'Append unique DOM ID' | |
| + nullify_cache: | |
| + type: boolean | |
| + label: 'Force zero-length cache' | |
| diff --git a/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php b/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php | |
| index 305e71b..c3b813d 100644 | |
| --- a/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php | |
| +++ b/src/Plugin/Field/FieldFormatter/EmbederatorFormatter.php | |
| @@ -5,6 +5,8 @@ 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; | |
| +use Drupal\Core\Form\FormStateInterface; | |
| /** | |
| * Plugin implementation of the default embederator (token replacing) formatter. | |
| @@ -41,6 +43,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>'; | |
| @@ -50,6 +53,9 @@ class EmbederatorFormatter extends FormatterBase { | |
| '#text' => $markup, | |
| '#format' => 'full_html', | |
| ]; | |
| + if ($this->getSetting('nullify_cache')) { | |
| + $elements[$delta]['#cache']['max-age'] = 0; | |
| + } | |
| } | |
| } | |
| else { | |
| @@ -58,11 +64,15 @@ 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, | |
| '#format' => $embed_pattern_field['format'], | |
| ]; | |
| + if ($this->getSetting('nullify_cache')) { | |
| + $elements[$delta]['#cache']['max-age'] = 0; | |
| + } | |
| } | |
| } | |
| @@ -80,4 +90,76 @@ 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.'); | |
| + } | |
| + if ($this->getSetting('nullify_cache')) { | |
| + $summary[] = $this->t('Zero cache.'); | |
| + } | |
| + return $summary; | |
| + } | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + public static function defaultSettings() { | |
| + return [ | |
| + 'append_unique_id' => FALSE, | |
| + 'nullify_cache' => 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'), | |
| + ]; | |
| + | |
| + $element['nullify_cache'] = [ | |
| + '#title' => $this->t('Force 0 max-age cache'), | |
| + '#type' => 'checkbox', | |
| + '#default_value' => $this->getSetting('nullify_cache'), | |
| + ]; | |
| + | |
| + 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