diff --git a/src/Plugin/Filter/LinkitFilter.php b/src/Plugin/Filter/LinkitFilter.php index cef1c6e..894ebe7 100644 --- a/src/Plugin/Filter/LinkitFilter.php +++ b/src/Plugin/Filter/LinkitFilter.php @@ -71,32 +71,34 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface public function process($text, $langcode) { $result = new FilterProcessResult($text); - $dom = Html::load($text); - $xpath = new \DOMXPath($dom); - - /** @var \DOMElement $node */ - foreach ($xpath->query('//a[@data-entity-type and @data-entity-uuid]') as $node) { - // Load the appropriate translation of the linked entity. - $entity = $this->entityRepository->loadEntityByUuid($node->getAttribute('data-entity-type'), $node->getAttribute('data-entity-uuid')); - $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); - - // Set the appropriate href and title attributes. - $url = $entity->toUrl()->toString(TRUE); - $node->setAttribute('href', $url->getGeneratedUrl()); - if ($this->settings['title']) { - $node->setAttribute('title', $entity->label()); + if (strpos($text, 'data-entity-type') !== FALSE && strpos($text, 'data-entity-uuid') !== FALSE) { + $dom = Html::load($text); + $xpath = new \DOMXPath($dom); + + /** @var \DOMElement $node */ + foreach ($xpath->query('//a[@data-entity-type and @data-entity-uuid]') as $node) { + // Load the appropriate translation of the linked entity. + $entity = $this->entityRepository->loadEntityByUuid($node->getAttribute('data-entity-type'), $node->getAttribute('data-entity-uuid')); + $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); + + // Set the appropriate href and title attributes. + $url = $entity->toUrl()->toString(TRUE); + $node->setAttribute('href', $url->getGeneratedUrl()); + if ($this->settings['title']) { + $node->setAttribute('title', $entity->label()); + } + + // The processed text now depends on: + $result + // - the generated URL (which has undergone path & route processing) + ->addCacheableDependency($url) + // - the linked entity (whose URL and title may change) + ->addCacheableDependency($entity); } - // The processed text now depends on: - $result - // - the generated URL (which has undergone path & route processing) - ->addCacheableDependency($url) - // - the linked entity (whose URL and title may change) - ->addCacheableDependency($entity); + $result->setProcessedText(Html::serialize($dom)); } - $result->setProcessedText(Html::serialize($dom)); - return $result; } diff --git a/src/Tests/LinkitFilterTest.php b/src/Tests/LinkitFilterTest.php new file mode 100644 index 0000000..ced056a --- /dev/null +++ b/src/Tests/LinkitFilterTest.php @@ -0,0 +1,93 @@ +container->get('plugin.manager.filter'); + $bag = new FilterPluginCollection($manager, array()); + $this->filter = $bag->get('linkit'); + } + + /** + * Tests the linkit filter. + */ + public function testFilter() { + // Create a content type. + $this->drupalCreateContentType(['type' => 'test', 'name' => 'Test']); + + $node = $this->drupalCreateNode([ + 'type' => 'test', + 'title' => $this->randomGenerator->string() + ]); + + $account = $this->drupalCreateUser(); + + // Don't automatically set the title. + $this->filter->setConfiguration(['settings' => ['title' => 0]]); + + // Test with a node. + $input = 'Link text'; + $expected = 'Link text'; + $this->assertIdentical($expected, $this->process($input)->getProcessedText()); + + // Test with a user. + $input = 'Link text'; + $expected = 'Link text'; + $this->assertIdentical($expected, $this->process($input)->getProcessedText()); + + // Automatically set the title. + $this->filter->setConfiguration(['settings' => ['title' => 1]]); + + // Test with a node. + $input = 'Link text'; + $expected = 'Link text'; + $this->assertIdentical($expected, $this->process($input)->getProcessedText()); + + // Test with a user. + $input = 'Link text'; + $expected = 'Link text'; + $this->assertIdentical($expected, $this->process($input)->getProcessedText()); + } + + /** + * Test helper method that wraps the filter process method. + * + * @see \Drupal\filter\Plugin\FilterInterface::process + */ + private function process($input, $langcode = 'und') { + return $this->filter->process($input, $langcode); + } + +}