Skip to content
Snippets Groups Projects

Remove URL text if it contains the URL

Merged Alex Pott requested to merge issue/entity_usage_updater-3516676:3516676-if-the-link into 1.0.x
Files
4
+ 9
3
@@ -3,6 +3,7 @@
namespace Drupal\entity_usage_updater;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\TypedData\Type\StringInterface;
@@ -67,9 +68,14 @@ trait LinkRemoverTrait {
) {
// Copy inner HTML to a new fragment.
$fragment = $dom->createDocumentFragment();
$fragment->appendXML(
array_reduce(iterator_to_array($element->childNodes), fn(string $carry, \DomNode $child) => $carry . $dom->saveXML($child), '')
);
$parsed_url = UrlHelper::parse($href);
// If the text content contains the href then remove it completely.
if (!str_contains($element->textContent, $parsed_url['path'])) {
$fragment->appendXML(
array_reduce(iterator_to_array($element->childNodes), fn(string $carry, \DomNode $child) => $carry . $dom->saveXML($child), '')
);
}
// Replace link with fragment.
$element->parentNode->replaceChild($fragment, $element);
$elements_replaced = TRUE;
Loading