diff -u b/src/Plugin/Linkit/Matcher/EntityMatcher.php b/src/Plugin/Linkit/Matcher/EntityMatcher.php --- b/src/Plugin/Linkit/Matcher/EntityMatcher.php +++ b/src/Plugin/Linkit/Matcher/EntityMatcher.php @@ -204,12 +204,12 @@ */ public function defaultConfiguration() { return [ - 'metadata' => '', - 'bundles' => [], - 'group_by_bundle' => FALSE, - 'substitution_type' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION, - 'limit' => static::DEFAULT_LIMIT, - ] + parent::defaultConfiguration(); + 'metadata' => '', + 'bundles' => [], + 'group_by_bundle' => FALSE, + 'substitution_type' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION, + 'limit' => static::DEFAULT_LIMIT, + ] + parent::defaultConfiguration(); } /** @@ -519,7 +519,18 @@ * The path for this entity. */ protected function buildPath(EntityInterface $entity) { - return $entity->toUrl('canonical', ['path_processing' => FALSE])->toString(); + $path = $entity->toUrl('canonical', ['path_processing' => FALSE])->toString(); + // For media entities, check if standalone URLs are allowed. If not, then + // strip '/edit' from the end of the canonical URL returned + // by $entity->toUrl(). + if ($entity->getEntityTypeId() == 'media') { + $standalone_url = \Drupal::config('media.settings')->get('standalone_url'); + if ($standalone_url) { + // Strip "/edit". + $path = substr($path, 0, -5); + } + } + return $path; } /** diff -u b/tests/src/Kernel/Matchers/NodeMatcherTest.php b/tests/src/Kernel/Matchers/NodeMatcherTest.php --- b/tests/src/Kernel/Matchers/NodeMatcherTest.php +++ b/tests/src/Kernel/Matchers/NodeMatcherTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\linkit\Kernel\Matchers; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase; @@ -18,7 +19,7 @@ * * @var array */ - public static $modules = ['field', 'node', 'content_moderation', 'workflows']; + public static $modules = ['field', 'node', 'content_moderation', 'workflows', 'language']; /** * The matcher manager. @@ -35,7 +36,7 @@ $this->installEntitySchema('node'); $this->installSchema('node', ['node_access']); - $this->installConfig(['field', 'node']); + $this->installConfig(['field', 'node', 'language']); $this->manager = $this->container->get('plugin.manager.linkit.matcher'); @@ -197,2 +198,31 @@ + /** + * Test node matches generated from an absolute URL input. + */ + public function testNodeMatcherFromAbsoluteUrlWithLanguagePrefix() { + /** @var \Drupal\linkit\MatcherInterface $plugin */ + $plugin = $this->manager->createInstance('entity:node'); + + $langcode = 'nl'; + \Drupal::configFactory()->getEditable('language.negotiation') + ->set('url.prefixes.nl', $langcode) + ->save(); + + // In order to reflect the changes for a multilingual site in the container + // we have to rebuild it. + \Drupal::service('kernel')->rebuildContainer(); + + /** @var \Drupal\node\NodeInterface[] $nodes */ + $nodes = $this->container->get('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Lorem Ipsum 1']); + $node = reset($nodes); + $translation = $node->addTranslation($langcode, $node->toArray()); + $translation->save(); + + $translated_url = $translation->toUrl()->setAbsolute()->toString(); + // Make sure the translated URL contains our prefix. + $this->assertContains('/' . $langcode . '/', $translated_url); + $suggestions = $plugin->execute($translated_url); + $this->assertEquals(1, count($suggestions->getSuggestions())); + } + }