diff --git a/linkit.module b/linkit.module index 5c853f5cf342a9016816d15c3b2031edeb001cbc..dd32845d9a2f15e4f0e02b57dd2b5c0f90398314 100644 --- a/linkit.module +++ b/linkit.module @@ -100,6 +100,8 @@ function linkit_form_editor_link_dialog_alter(&$form, FormStateInterface $form_s /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = \Drupal::service('entity.repository') ->loadEntityByUuid($input['data-entity-type'], $input['data-entity-uuid']); + $entity = \Drupal::service('entity.repository') + ->getTranslationFromContext($entity); $access = !$entity->access('view', NULL, TRUE)->isForbidden(); $autocomplete_default_value = !empty($access) && $access ? $entity->label() : ''; } diff --git a/tests/src/FunctionalJavascript/LinkitDialogTest.php b/tests/src/FunctionalJavascript/LinkitDialogTest.php index f6d201dcf9a9ba230a56ef85c3aceeb018c9a0d6..002000f9c46b4834292a30ed6668b048cce7c897 100644 --- a/tests/src/FunctionalJavascript/LinkitDialogTest.php +++ b/tests/src/FunctionalJavascript/LinkitDialogTest.php @@ -4,10 +4,12 @@ namespace Drupal\Tests\linkit\FunctionalJavascript; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\editor\Entity\Editor; +use Drupal\entity_test\Entity\EntityTestMul; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\filter\Entity\FilterFormat; use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\linkit\Tests\ProfileCreationTrait; use Drupal\node\Entity\NodeType; @@ -25,7 +27,7 @@ class LinkitDialogTest extends JavascriptTestBase { * * @var array */ - public static $modules = ['node', 'ckeditor', 'filter', 'linkit']; + public static $modules = ['node', 'ckeditor', 'filter', 'linkit', 'entity_test', 'language']; /** * An instance of the "CKEditor" text editor plugin. @@ -56,8 +58,9 @@ class LinkitDialogTest extends JavascriptTestBase { $matcherManager = $this->container->get('plugin.manager.linkit.matcher'); /** @var \Drupal\linkit\MatcherInterface $plugin */ - $plugin = $matcherManager->createInstance('entity:node', []); + $this->linkitProfile = $this->createProfile(); + $plugin = $matcherManager->createInstance('entity:entity_test_mul'); $this->linkitProfile->addMatcher($plugin->getConfiguration()); $this->linkitProfile->save(); @@ -127,8 +130,18 @@ class LinkitDialogTest extends JavascriptTestBase { $web_assert = $this->assertSession(); $page = $session->getPage(); + // Adds additional languages. + $langcodes = ['sv', 'da', 'fi']; + foreach ($langcodes as $langcode) { + ConfigurableLanguage::createFromLangcode($langcode)->save(); + } + + // Create a test entity. + $entity = EntityTestMul::create(['name' => 'Foo']); + $entity->save(); + // Create test nodes. - $this->demoEntity = $this->createNode(['title' => 'Foo']); + $this->demoEntity = $entity; // Go to node creation page. $this->drupalGet('node/add/page'); @@ -221,6 +234,8 @@ class LinkitDialogTest extends JavascriptTestBase { JS; $session->executeScript($javascript); + // Test edit link in the editor. + // Click on the drupallink plugin. $page->find('css', 'a.cke_button__drupallink')->click(); @@ -288,6 +303,94 @@ JS; $this->assertEquals('http://example.com', $href_attribute, 'The link href is correct.'); } + /** + * Test the link dialog with translated entities. + */ + public function testLinkDialogTranslations() { + $session = $this->getSession(); + $web_assert = $this->assertSession(); + $page = $session->getPage(); + + // Adds additional languages. + $langcodes = ['sv', 'da', 'fi']; + foreach ($langcodes as $langcode) { + ConfigurableLanguage::createFromLangcode($langcode)->save(); + } + + // Create a test entity. + $entity = EntityTestMul::create(['name' => 'Foo']); + + foreach ($langcodes as $langcode) { + $entity->addTranslation($langcode, ['name' => 'Foo ' . $langcode]); + } + + $entity->save(); + + // Create test nodes. + $this->demoEntity = $entity; + + $this->config('system.site')->set('default_langcode', 'sv')->save(); + + // Go to node creation page. + $this->drupalGet('node/add/page'); + + // Wait until the editor has been loaded. + $ckeditor_loaded = $this->getSession()->wait(5000, "jQuery('.cke_contents').length > 0"); + $this->assertTrue($ckeditor_loaded, 'The editor has been loaded.'); + + // Click on the drupallink plugin. + $page->find('css', 'a.cke_button__drupallink')->click(); + + // Wait for the form to load. + $web_assert->assertWaitOnAjaxRequest(); + + // Find the linkit field. + $linkit_field = $page->findField('linkit'); + + // Trigger a keydown event to active a autocomplete search. + $linkit_field->keyDown('f'); + + // Wait for the results to load. + $this->getSession()->wait(5000, "jQuery('.linkit-result.ui-menu-item').length > 0"); + + // Find all the autocomplete results. + $results = $page->findAll('css', '.linkit-result.ui-menu-item'); + $this->assertEquals(1, count($results), 'Found autocomplete result'); + + // Find the first result and click it. + $page->find('xpath', '(//li[contains(@class, "linkit-result") and contains(@class, "ui-menu-item")])[1]')->click(); + + // Save the dialog input. + $page->find('css', '.editor-link-dialog')->find('css', '.button.form-submit span')->click(); + + // Wait for the dialog to close. + $web_assert->assertWaitOnAjaxRequest(); + + // Select the link in the editor. + $javascript = <<executeScript($javascript); + + // Click on the drupallink plugin. + $page->find('css', 'a.cke_button__drupallink')->click(); + + // Wait for the form to load. + $web_assert->assertWaitOnAjaxRequest(); + + // Find the linkit field. + $linkit_field = $page->findField('linkit'); + $this->assertEquals($this->demoEntity->getTranslation('sv')->label(), $linkit_field->getValue(), 'Linkit field has the correct value.'); + + // Make sure the link information is populated with the old label. + $this->assertEquals($this->demoEntity->getTranslation('sv')->label(), $this->getLinkInfoText(), 'Link information is populated'); + } + /** * Asserts that a variable is empty. *