Created
January 15, 2019 15:53
-
-
Save ahebrank/69ab041d47f150e184b26273d1dbfb55 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..ee28ede 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>'; | |
| @@ -58,6 +61,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, | |
| @@ -66,6 +70,9 @@ class EmbederatorFormatter extends FormatterBase { | |
| } | |
| } | |
| + if ($this->getSetting('nullify_cache')) { | |
| + $elements['#cache']['max-age'] = 0; | |
| + } | |
| return $elements; | |
| } | |
| @@ -80,4 +87,80 @@ 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 hash to form 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 hash to form input DOM IDs'), | |
| + '#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) { | |
| + // Only form inputs. | |
| + if (!$element->hasAttribute('name')) { | |
| + continue; | |
| + } | |
| + $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