Commit e16ce1c4 authored by Samuel Mortenson's avatar Samuel Mortenson Committed by Sam Mortenson
Browse files

Issue #3309555 by mrweiner, samuel.mortenson: sfc_unique_id may be inherited...

Issue #3309555 by mrweiner, samuel.mortenson: sfc_unique_id may be inherited from an outer SFC to nested SFC
parent 38403067
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ class SimpleComponent extends ComponentBase {
   * {@inheritdoc}
   */
  public function prepareContext(array &$context) {
    $context['sfc_unique_id'] = isset($context['sfc_unique_id']) ? $context['sfc_unique_id'] : uniqid($this->getId());
    $context['sfc_unique_id'] = self::determineUniqueId($context);
    $attributes = new Attribute();
    $attributes->setAttribute('data-sfc-id', $this->getId());
    $attributes->setAttribute('data-sfc-unique-id', $context['sfc_unique_id']);
@@ -320,4 +320,23 @@ class SimpleComponent extends ComponentBase {
    }
  }

  /**
   * Determines the unique ID for a component.
   *
   * @param array $context
   *   The context being passed to the Twig template.
   *
   * @return string
   *   The unique ID.
   */
  protected function determineUniqueId(array $context) {
    $unique_ids = &drupal_static(__FUNCTION__, []);

    // Parent components may unintentionally pass their unique IDs to children.
    $id = !isset($context['sfc_unique_id']) || in_array($context['sfc_unique_id'], $unique_ids, TRUE) ? uniqid($this->getId()) : $context['sfc_unique_id'];

    $unique_ids[] = $id;
    return $id;
  }

}
+3 −0
Original line number Diff line number Diff line
<template>
  {% include 'sfc--simple-unique-id' %}
</template>
+3 −0
Original line number Diff line number Diff line
<template>
  {{ sfc_unique_id }}
</template>
+2 −0
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ class SimpleComponentTest extends KernelTestBase {
    $this->assertEquals("  <template>foo</template>
  bar
  <script>alert(`baz`)</script>", $this->renderComponent('simple_parse', []));
    // Test nested unique IDs.
    $this->assertStringContainsString('simple_unique_id', $this->renderComponent('simple_nested_unique_id', []));
  }

  /**