linkit.module | 27 +++++++++++++++++++++++++++ src/Plugin/Filter/LinkitFilter.php | 20 ++++---------------- src/Plugin/Linkit/Matcher/EntityMatcher.php | 6 ++++-- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/linkit.module b/linkit.module index 8e6939b..7765139 100644 --- a/linkit.module +++ b/linkit.module @@ -56,6 +56,9 @@ function linkit_form_editor_link_dialog_alter(&$form, FormStateInterface $form_s ], '#weight' => 0, ]; + // Add #submit callback that generates data-entity-type and data-entity-uuid + // attributes from the href attribute when appropriate. + array_unshift($form['#submit'], 'linkit_form_editor_link_dialog_submit'); if ($linkitProfile->getAttributes()->count()) { $form['linkit_attributes'] = [ @@ -80,6 +83,30 @@ function linkit_form_editor_link_dialog_alter(&$form, FormStateInterface $form_s } /** + * Generates data-entity-type and data-entity-uuid attributes from href. + */ +function linkit_form_editor_link_dialog_submit(array &$form, FormStateInterface $form_state) { + // Check if the 'href' attribute contains a entity: URI. + $href = $form_state->getValue(['attributes', 'href']); + $uri_parts = parse_url($href); + if ($uri_parts['scheme'] !== 'entity') { + return; + } + + // Parse the entity: URI into an entity type ID and entity ID. + list($entity_type_id, $entity_id) = explode('/', $uri_parts['path'], 2); + + // Load the entity and populate the data-entity-type and data-entity-uuid + // attributes as expected by filters. + // @see \Drupal\editor\Plugin\Filter\EditorFileReference + // @see \Drupal\linkit\Plugin\Filter\LinkitFilter + $entity = entity_load($entity_type_id, $entity_id); + $form_state->setValue(['attributes', 'data-entity-type'], $entity_type_id); + $form_state->setValue(['attributes', 'data-entity-uuid'], $entity->uuid()); +} + + +/** * Implements hook_form_BASE_FORM_ID_alter() for linkit_profile_form on behalf * of the 'imce' module. * diff --git a/src/Plugin/Filter/LinkitFilter.php b/src/Plugin/Filter/LinkitFilter.php index 836b1b4..cef1c6e 100644 --- a/src/Plugin/Filter/LinkitFilter.php +++ b/src/Plugin/Filter/LinkitFilter.php @@ -9,10 +9,8 @@ namespace Drupal\linkit\Plugin\Filter; use Drupal\Component\Utility\Html; use Drupal\Core\Entity\EntityRepositoryInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Url; use Drupal\filter\FilterProcessResult; use Drupal\filter\Plugin\FilterBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -46,15 +44,12 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository * The entity repository service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->entityTypeManager = $entity_type_manager; $this->entityRepository = $entity_repository; } @@ -66,7 +61,6 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface $configuration, $plugin_id, $plugin_definition, - $container->get('entity_type.manager'), $container->get('entity.repository') ); } @@ -81,18 +75,12 @@ class LinkitFilter extends FilterBase implements ContainerFactoryPluginInterface $xpath = new \DOMXPath($dom); /** @var \DOMElement $node */ - foreach ($xpath->query('//a[starts-with(@href, "entity:")]') as $node) { - $href = $node->getAttribute('href'); - - // Parse the entity: URI into an entity type ID and entity ID. - $uri_parts = parse_url($href); - list($entity_type_id, $entity_id) = explode('/', $uri_parts['path'], 2); - + foreach ($xpath->query('//a[@data-entity-type and @data-entity-uuid]') as $node) { // Load the appropriate translation of the linked entity. - $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); + $entity = $this->entityRepository->loadEntityByUuid($node->getAttribute('data-entity-type'), $node->getAttribute('data-entity-uuid')); $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); - // Set the appropriate + // Set the appropriate href and title attributes. $url = $entity->toUrl()->toString(TRUE); $node->setAttribute('href', $url->getGeneratedUrl()); if ($this->settings['title']) { diff --git a/src/Plugin/Linkit/Matcher/EntityMatcher.php b/src/Plugin/Linkit/Matcher/EntityMatcher.php index 96cf1c5..1a580ae 100644 --- a/src/Plugin/Linkit/Matcher/EntityMatcher.php +++ b/src/Plugin/Linkit/Matcher/EntityMatcher.php @@ -316,10 +316,12 @@ class EntityMatcher extends ConfigurableMatcherBase { * The matched entity. * * @return string - * The URL for this entity. + * The entity: URL for this entity. + * + * @see \Drupal\Core\Url::fromEntityUri() */ protected function buildPath($entity) { - return 'entity:' . $entity->toUrl()->getInternalPath(); + return 'entity:' . $entity->getEntityTypeId() . '/' . $entity->id(); } /**