Commit 28977ae6 authored by alexpott's avatar alexpott

Issue #2350837 by dawehner, Berdir, larowlan, Wim Leers: Convert most usages...

Issue #2350837 by dawehner, Berdir, larowlan, Wim Leers: Convert most usages of EntityInterface::getSystemPath() to use routes
parent 6efd90c8
......@@ -823,7 +823,7 @@ services:
arguments: ['@language_manager', '@config.factory', '@page_cache_request_policy', '@page_cache_response_policy']
redirect_response_subscriber:
class: Drupal\Core\EventSubscriber\RedirectResponseSubscriber
arguments: ['@url_generator']
arguments: ['@url_generator', '@router.request_context']
tags:
- { name: event_subscriber }
request_close_subscriber:
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\EventSubscriber;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
......@@ -32,9 +33,12 @@ class RedirectResponseSubscriber implements EventSubscriberInterface {
*
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service.
* @param \Drupal\Core\Routing\RequestContext $request_context
* The request context.
*/
public function __construct(UrlGeneratorInterface $url_generator) {
public function __construct(UrlGeneratorInterface $url_generator, RequestContext $request_context) {
$this->urlGenerator = $url_generator;
$this->requestContext = $request_context;
}
/**
......@@ -55,16 +59,21 @@ public function checkRedirectUrl(FilterResponseEvent $event) {
// the following exception:
// - Absolute URLs that point to this site (i.e. same base URL and
// base path) are allowed.
if ($destination && (!UrlHelper::isExternal($destination) || UrlHelper::externalIsLocal($destination, $GLOBALS['base_url']))) {
$destination = UrlHelper::parse($destination);
if ($destination) {
if (!UrlHelper::isExternal($destination)) {
$destination = UrlHelper::parse($destination);
$path = $destination['path'];
$options['query'] = $destination['query'];
$options['fragment'] = $destination['fragment'];
// The 'Location' HTTP header must always be absolute.
$options['absolute'] = TRUE;
$path = $destination['path'];
$options['query'] = $destination['query'];
$options['fragment'] = $destination['fragment'];
// The 'Location' HTTP header contain an absolute URL.
$options['absolute'] = TRUE;
$response->setTargetUrl($this->urlGenerator->generateFromPath($path, $options));
$response->setTargetUrl($this->urlGenerator->generateFromPath($path, $options));
}
elseif (UrlHelper::externalIsLocal($destination, $this->requestContext->getCompleteBaseUrl())) {
$response->setTargetUrl($destination);
}
}
}
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RequestContext as SymfonyRequestContext;
......@@ -18,13 +19,54 @@
*/
class RequestContext extends SymfonyRequestContext {
/**
* The scheme, host and base path, for example "http://example.com/d8".
*
* @var string
*/
protected $completeBaseUrl;
/**
* Populates the context from the current request from the request stack.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The current request stack.
*/
public function fromRequestStack(RequestStack $request_stack) {
$this->fromRequest($request_stack->getCurrentRequest());
}
/**
* {@inheritdoc}
*/
public function fromRequest(Request $request) {
parent::fromRequest($request);
// @todo Extract the code in DrupalKernel::initializeRequestGlobals.
// See https://www.drupal.org/node/2404601
if (isset($GLOBALS['base_url'])) {
$this->setCompleteBaseUrl($GLOBALS['base_url']);
}
}
/**
* Gets the scheme, host and base path.
*
* For example, in an installation in a subdirectory "d8", it should be
* "https://example.com/d8".
*/
public function getCompleteBaseUrl() {
return $this->completeBaseUrl;
}
/**
* Sets the complete base URL for the Request context.
*
* @param string $complete_base_url
* The complete base URL.
*/
public function setCompleteBaseUrl($complete_base_url) {
$this->completeBaseUrl = $complete_base_url;
}
}
......@@ -263,7 +263,7 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
$destination = array('destination' => 'comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name . '#comment-form');
}
else {
$destination = array('destination' => $entity->getSystemPath() . '#comment-form');
$destination = array('destination' => $entity->url('canonical', array('fragment' => 'comment-form')));
}
if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
......
......@@ -128,7 +128,7 @@ public function commentPermalink(Request $request, CommentInterface $comment) {
// Find the current display page for this comment.
$page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
// @todo: Cleaner sub request handling.
$redirect_request = Request::create($entity->getSystemPath(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
$redirect_request = Request::create($entity->url(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
$redirect_request->query->set('page', $page);
// @todo: Convert the pager to use the request object.
$request->query->set('page', $page);
......
......@@ -10,7 +10,6 @@
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\entity\Entity\EntityViewDisplay;
/**
* Basic comment links tests to ensure markup present.
......@@ -109,7 +108,7 @@ public function testCommentLinks() {
entity_get_display('node', $this->node->bundle(), 'default')
->removeComponent('links')
->save();
$this->drupalGet($this->node->url());
$this->drupalGet($this->node->urlInfo());
$this->assertNoLink('1 comment');
$this->assertNoLink('Add new comment');
......
......@@ -118,11 +118,10 @@ protected function assertPublishedStatus() {
$languages = $this->container->get('language_manager')->getLanguages();
// Check that simple users cannot see unpublished field translations.
$path = $entity->getSystemPath();
foreach ($this->langcodes as $index => $langcode) {
$translation = $this->getTranslation($entity, $langcode);
$value = $this->getValue($translation, 'comment_body', $langcode);
$this->drupalGet($path, array('language' => $languages[$langcode]));
$this->drupalGet($entity->urlInfo(), array('language' => $languages[$langcode]));
if ($index > 0) {
$this->assertNoRaw($value, 'Unpublished field translation is not shown.');
}
......
......@@ -38,18 +38,18 @@ function testCRUD() {
$entity = entity_load('config_test', $id);
// Disable an entity.
$disable_path = $entity->getSystemPath('disable');
$this->assertLinkByHref($disable_path);
$this->drupalGet($disable_path);
$disable_url = $entity->urlInfo('disable');
$this->assertLinkByHref($disable_url->toString());
$this->drupalGet($disable_url);
$this->assertResponse(200);
$this->assertNoLinkByHref($disable_path);
$this->assertNoLinkByHref($disable_url->toString());
// Enable an entity.
$enable_path = $entity->getSystemPath('enable');
$this->assertLinkByHref($enable_path);
$this->drupalGet($enable_path);
$enable_url = $entity->urlInfo('enable');
$this->assertLinkByHref($enable_url->toString());
$this->drupalGet($enable_url);
$this->assertResponse(200);
$this->assertNoLinkByHref($enable_path);
$this->assertNoLinkByHref($enable_url->toString());
}
}
......@@ -12,6 +12,7 @@
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
/**
......@@ -122,7 +123,7 @@ function testCRUD() {
}
// The entity path can only be checked after saving.
$this->assertIdentical($config_test->getSystemPath(), 'admin/structure/config_test/manage/' . $expected['id']);
$this->assertIdentical($config_test->url(), Url::fromRoute('entity.config_test.edit_form', ['config_test' => $expected['id']])->toString());
// Verify that the correct status is returned and properties did not change.
$this->assertIdentical($status, SAVED_NEW);
......
......@@ -329,7 +329,7 @@ public function testContactConfigEntityTranslation() {
foreach ($this->langcodes as $langcode) {
$langcode_prefixes = array_merge(array(''), $this->langcodes);
foreach ($langcode_prefixes as $langcode_prefix) {
$this->drupalGet(ltrim("$langcode_prefix/$translation_base_url/$langcode/edit"));
$this->drupalGet(ltrim("$langcode_prefix/$translation_base_url/$langcode/edit", '/'));
$this->assertFieldByName('translation[config_names][contact.form.feedback][label]', 'Website feedback - ' . $langcode);
$this->assertText($label);
}
......
......@@ -80,7 +80,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// Remove any existing path alias for the removed translation.
// @todo This should be taken care of by the Path module.
if (\Drupal::moduleHandler()->moduleExists('path')) {
$path = $this->entity->getSystemPath();
$path = $this->entity->urlInfo()->getInternalPath();
$conditions = array('source' => $path, 'langcode' => $this->language->getId());
\Drupal::service('path.alias_storage')->delete($conditions);
}
......
......@@ -10,6 +10,8 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests the Content Translation UI.
......@@ -52,9 +54,9 @@ protected function doTestBasicTranslation() {
$this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this->assertTrue($entity, 'Entity found in the database.');
$this->drupalGet($entity->getSystemPath());
$this->drupalGet($entity->urlInfo());
$this->assertResponse(200, 'Entity URL is valid.');
$this->drupalGet($entity->getSystemPath('drupal:content-translation-overview'));
$this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
$this->assertNoText('Source language', 'Source language column correctly hidden.');
$translation = $this->getTranslation($entity, $default_langcode);
......@@ -67,35 +69,48 @@ protected function doTestBasicTranslation() {
// Add a content translation.
$langcode = 'it';
$language = ConfigurableLanguage::load($langcode);
$values[$langcode] = $this->getNewEntityValues($langcode);
$content_translation_path = $entity->getSystemPath('drupal:content-translation-overview');
$path = $langcode . '/' . $content_translation_path . '/add/' . $default_langcode . '/' . $langcode;
$this->drupalPostForm($path, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode));
$add_url = Url::fromRoute('content_translation.translation_add_' . $entity->getEntityTypeId(), [
$entity->getEntityTypeId() => $entity->id(),
'source' => $default_langcode,
'target' => $langcode
], array('language' => $language));
$this->drupalPostForm($add_url, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode));
if ($this->testLanguageSelector) {
$this->assertNoFieldByXPath('//select[@id="edit-langcode-0-value"]', NULL, 'Language selector correctly disabled on translations.');
}
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this->drupalGet($entity->getSystemPath('drupal:content-translation-overview'));
$this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
$this->assertNoText('Source language', 'Source language column correctly hidden.');
// Switch the source language.
$langcode = 'fr';
$language = ConfigurableLanguage::load($langcode);
$source_langcode = 'it';
$edit = array('source_langcode[source]' => $source_langcode);
$path = $langcode . '/' . $content_translation_path . '/add/' . $default_langcode . '/' . $langcode;
$add_url = Url::fromRoute('content_translation.translation_add_' . $entity->getEntityTypeId(), [
$entity->getEntityTypeId() => $entity->id(),
'source' => $default_langcode,
'target' => $langcode
], array('language' => $language));
// This does not save anything, it merely reloads the form and fills in the
// fields with the values from the different source language.
$this->drupalPostForm($path, $edit, t('Change'));
$this->drupalPostForm($add_url, $edit, t('Change'));
$this->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');
// Add another translation and mark the other ones as outdated.
$values[$langcode] = $this->getNewEntityValues($langcode);
$edit = $this->getEditValues($values, $langcode) + array('content_translation[retranslate]' => TRUE);
$path = $langcode . '/' . $content_translation_path . '/add/' . $source_langcode . '/' . $langcode;
$this->drupalPostForm($path, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode));
$add_url = Url::fromRoute('content_translation.translation_add_' . $entity->getEntityTypeId(), [
$entity->getEntityTypeId() => $entity->id(),
'source' => $source_langcode,
'target' => $langcode
], array('language' => $language));
$this->drupalPostForm($add_url, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this->drupalGet($entity->getSystemPath('drupal:content-translation-overview'));
$this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
$this->assertText('Source language', 'Source language column correctly shown.');
// Check that the entered values have been correctly stored.
......@@ -115,15 +130,15 @@ protected function doTestBasicTranslation() {
*/
protected function doTestTranslationOverview() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this->drupalGet($entity->getSystemPath('drupal:content-translation-overview'));
$this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
foreach ($this->langcodes as $langcode) {
if ($entity->hasTranslation($langcode)) {
$language = new Language(array('id' => $langcode));
$view_path = \Drupal::urlGenerator()->generateFromPath($entity->getSystemPath(), array('language' => $language));
$view_path = $entity->url('canonical', array('language' => $language));
$elements = $this->xpath('//table//a[@href=:href]', array(':href' => $view_path));
$this->assertEqual((string) $elements[0], $entity->getTranslation($langcode)->label(), format_string('Label correctly shown for %language translation.', array('%language' => $langcode)));
$edit_path = \Drupal::urlGenerator()->generateFromPath($entity->getSystemPath('edit-form'), array('language' => $language));
$edit_path = $entity->url('edit-form', array('language' => $language));
$elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a[@href=:href]', array(':href' => $edit_path));
$this->assertEqual((string) $elements[0], t('Edit'), format_string('Edit link correct for %language translation.', array('%language' => $langcode)));
}
......@@ -140,15 +155,15 @@ protected function doTestOutdatedStatus() {
// Mark translations as outdated.
$edit = array('content_translation[retranslate]' => TRUE);
$path = $entity->getSystemPath('edit-form');
$this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode), array('language' => $languages[$langcode]));
$edit_path = $entity->urlInfo('edit-form', array('language' => $languages[$langcode]));
$this->drupalPostForm($edit_path, $edit, $this->getFormSubmitAction($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
// Check that every translation has the correct "outdated" status, and that
// the Translation fieldset is open if the translation is "outdated".
foreach ($this->langcodes as $added_langcode) {
$options = array('language' => $languages[$added_langcode]);
$this->drupalGet($path, $options);
$url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($added_langcode)));
$this->drupalGet($url);
if ($added_langcode == $langcode) {
$this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.');
$this->assertFalse($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab should be collapsed by default.');
......@@ -157,8 +172,8 @@ protected function doTestOutdatedStatus() {
$this->assertFieldByXPath('//input[@name="content_translation[outdated]"]', TRUE, 'The translate flag is checked by default.');
$this->assertTrue($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab is correctly expanded when the translation is outdated.');
$edit = array('content_translation[outdated]' => FALSE);
$this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $added_langcode), $options);
$this->drupalGet($path, $options);
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $added_langcode));
$this->drupalGet($url);
$this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is now shown.');
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this->assertFalse($entity->translation[$added_langcode]['outdated'], 'The "outdated" status has been correctly stored.');
......@@ -171,20 +186,20 @@ protected function doTestOutdatedStatus() {
*/
protected function doTestPublishedStatus() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$path = $entity->getSystemPath('edit-form');
// Unpublish translations.
foreach ($this->langcodes as $index => $langcode) {
if ($index > 0) {
$url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode)));
$edit = array('content_translation[status]' => FALSE);
$this->drupalPostForm($langcode . '/' . $path, $edit, $this->getFormSubmitAction($entity, $langcode));
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$this->assertFalse($entity->translation[$langcode]['status'], 'The translation has been correctly unpublished.');
}
}
// Check that the last published translation cannot be unpublished.
$this->drupalGet($path);
$this->drupalGet($entity->urlInfo('edit-form'));
$this->assertFieldByXPath('//input[@name="content_translation[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.');
}
......@@ -193,7 +208,6 @@ protected function doTestPublishedStatus() {
*/
protected function doTestAuthoringInfo() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$path = $entity->getSystemPath('edit-form');
$values = array();
// Post different authoring information for each translation.
......@@ -207,8 +221,8 @@ protected function doTestAuthoringInfo() {
'content_translation[name]' => $user->getUsername(),
'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
);
$prefix = $index > 0 ? $langcode . '/' : '';
$this->drupalPostForm($prefix . $path, $edit, $this->getFormSubmitAction($entity, $langcode));
$url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode)));
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
}
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
......@@ -224,7 +238,7 @@ protected function doTestAuthoringInfo() {
'content_translation[name]' => $this->randomMachineName(12),
'content_translation[created]' => '19/11/1978',
);
$this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode));
$this->drupalPostForm($entity->urlInfo('edit-form'), $edit, $this->getFormSubmitAction($entity, $langcode));
$this->assertTrue($this->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
$this->assertEqual($entity->translation[$langcode]['uid'], $values[$langcode]['uid'], 'Translation author correctly kept.');
$this->assertEqual($entity->translation[$langcode]['created'], $values[$langcode]['created'], 'Translation date correctly kept.');
......@@ -237,8 +251,8 @@ protected function doTestTranslationDeletion() {
// Confirm and delete a translation.
$langcode = 'fr';
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$path = $entity->getSystemPath('edit-form');
$this->drupalPostForm($langcode . '/' . $path, array(), t('Delete translation'));
$url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode)));
$this->drupalPostForm($url, array(), t('Delete translation'));
$this->drupalPostForm(NULL, array(), t('Delete'));
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
if ($this->assertTrue(is_object($entity), 'Entity found')) {
......
......@@ -8,6 +8,7 @@
namespace Drupal\content_translation\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
/**
......@@ -61,10 +62,8 @@ protected function setupEntity() {
// Create a translation.
$this->drupalLogin($this->translator);
$path = $this->entity->getSystemPath('drupal:content-translation-overview');
$add_translation_path = $path . "/add/$default_langcode/{$this->langcodes[2]}";
$this->drupalPostForm($add_translation_path, array(), t('Save'));
$add_translation_url = Url::fromRoute('content_translation.translation_add_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $this->langcodes[2]]);
$this->drupalPostForm($add_translation_url, array(), t('Save'));
$this->rebuildContainer();
}
......@@ -86,11 +85,11 @@ function testWorkflows() {
// Check that translation permissions governate the associated operations.
$ops = array('create' => t('Add'), 'update' => t('Edit'), 'delete' => t('Delete'));
$translations_path = $this->entity->getSystemPath('drupal:content-translation-overview');
$translations_url = $this->entity->urlInfo('drupal:content-translation-overview');
foreach ($ops as $current_op => $item) {
$user = $this->drupalCreateUser(array($this->getTranslatePermission(), "$current_op content translations"));
$this->drupalLogin($user);
$this->drupalGet($translations_path);
$this->drupalGet($translations_url);
foreach ($ops as $op => $label) {
if ($op != $current_op) {
......@@ -119,23 +118,23 @@ protected function assertWorkflows(UserInterface $user, $expected_status) {
$this->drupalLogin($user);
// Check whether the user is allowed to access the entity form in edit mode.
$edit_path = $this->entity->getSystemPath('edit-form');
$options = array('language' => $languages[$default_langcode]);
$this->drupalGet($edit_path, $options);
$options = array('language' => $languages[$default_langcode], 'absolute' => TRUE);
$edit_url = $this->entity->urlInfo('edit-form', $options);
$this->drupalGet($edit_url, $options);
$this->assertResponse($expected_status['edit'], format_string('The @user_label has the expected edit access.', $args));
// Check whether the user is allowed to access the translation overview.
$langcode = $this->langcodes[1];
$translations_path = $this->entity->getSystemPath('drupal:content-translation-overview');
$options = array('language' => $languages[$langcode]);
$this->drupalGet($translations_path, $options);
$options = array('language' => $languages[$langcode], 'absolute' => TRUE);
$translations_url = $this->entity->url('drupal:content-translation-overview', $options);
$this->drupalGet($translations_url);
$this->assertResponse($expected_status['overview'], format_string('The @user_label has the expected translation overview access.', $args));
// Check whether the user is allowed to create a translation.
$add_translation_path = $translations_path . "/add/$default_langcode/$langcode";
$add_translation_url = Url::fromRoute('content_translation.translation_add_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $langcode], $options);
if ($expected_status['add_translation'] == 200) {
$this->clickLink('Add');
$this->assertUrl($add_translation_path, $options, 'The translation overview points to the translation form when creating translations.');
$this->assertUrl($add_translation_url->toString(), array(), 'The translation overview points to the translation form when creating translations.');
// Check that the translation form does not contain shared elements for
// translators.
if ($expected_status['edit'] == 403) {
......@@ -143,33 +142,37 @@ protected function assertWorkflows(UserInterface $user, $expected_status) {
}
}
else {
$this->drupalGet($add_translation_path, $options);
$this->drupalGet($add_translation_url);
}
$this->assertResponse($expected_status['add_translation'], format_string('The @user_label has the expected translation creation access.', $args));
// Check whether the user is allowed to edit a translation.
$langcode = $this->langcodes[2];
$edit_translation_path = $translations_path . "/edit/$langcode";
$options = array('language' => $languages[$langcode]);
$options['language'] = $languages[$langcode];
$edit_translation_url = Url::fromRoute('content_translation.translation_edit_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'language' => $langcode], $options);
$options = ['language' => $languages[$langcode], 'absolute' => TRUE];
if ($expected_status['edit_translation'] == 200) {
$this->drupalGet($translations_path, $options);
$this->drupalGet($translations_url);
$editor = $expected_status['edit'] == 200;
if ($editor) {
$this->clickLink('Edit', 2);
// An editor should be pointed to the entity form in multilingual mode.
$this->assertUrl($edit_path, $options, 'The translation overview points to the edit form for editors when editing translations.');
// We need a new expected edit path with a new language.
$expected_edit_path = $this->entity->url('edit-form', $options);
$this->assertUrl($expected_edit_path, [], 'The translation overview points to the edit form for editors when editing translations.');
}
else {
$this->clickLink('Edit');
// While a translator should be pointed to the translation form.
$this->assertUrl($edit_translation_path, $options, 'The translation overview points to the translation form for translators when editing translations.');
$this->assertUrl($edit_translation_url->toString(), array(), 'The translation overview points to the translation form for translators when editing translations.');
// Check that the translation form does not contain shared elements.
$this->assertNoSharedElements();
}
}
else {
$this->drupalGet($edit_translation_path, $options);
$this->drupalGet($edit_translation_url);
}
$this->assertResponse($expected_status['edit_translation'], format_string('The @user_label has the expected translation creation access.', $args));
}
......
......@@ -121,6 +121,11 @@ protected function setUp() {
$this->translatedLabel = $this->randomMachineName();
$this->setUpLanguages();
// We setup languages, so we need to ensure that the language manager
// and language path processor is updated.
$this->rebuildContainer();
$this->setUpContentTypes();
$this->enableTranslation();
$this->setUpEntityReferenceField();
......@@ -131,14 +136,14 @@ protected function setUp() {
* Tests if the translated entity is displayed in an entity reference field.
*/
public function testTranslatedEntityReferenceDisplay() {
$path = $this->referrerEntity->getSystemPath();
$translation_path = $this->translateToLangcode . '/' . $path;
$url = $this->referrerEntity->urlInfo();
$translation_url = $this->referrerEntity->urlInfo('canonical', ['language' => ConfigurableLanguage::load($this->translateToLangcode)]);
$this->drupalGet($path);
$this->drupalGet($url);
$this->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
$this->assertText($this->originalLabel, 'The default label of translated reference is displayed.');
$this->assertNoText($this->translatedLabel, 'The translated label of translated reference is not displayed.');
$this->drupalGet($translation_path);
$this->drupalGet($translation_url);
$this->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
$this->assertNoText($this->originalLabel, 'The default label of translated reference is not displayed.');
$this->assertText($this->translatedLabel, 'The translated label of translated reference is displayed.');
......
......@@ -46,7 +46,7 @@ function template_preprocess_image_widget(&$variables) {
* - item_attributes: An optional associative array of html attributes to be
* placed in the img tag.
* - image_style: An optional image style.
* - path: An optional array containing the link 'path' and link 'options'.
* - url: An optional \Drupal\Core\Url object.
*/
function template_preprocess_image_formatter(&$variables) {
if ($variables['image_style']) {
......@@ -79,14 +79,4 @@ function template_preprocess_image_formatter(&$variables) {
foreach (array('width', 'height', 'alt') as $key) {
$variables['image']["#$key"] = $item->$key;
}
// The link path and link options are both optional, but for the options to be
// processed, the link path must at least be an empty string.
// @todo Add support for route names.
$variables['url'] = NULL;
if (isset($variables['path']['path'])) {
$path = $variables['path']['path'];
$options = isset($variables['path']['options']) ? $variables['path']['options'] : array();
$variables['url'] = _url($path, $options);
}
}
......@@ -139,7 +139,7 @@ function image_theme() {
'file' => 'image.field.inc',
),
'image_formatter' => array(
'variables' => array('item' => NULL, 'item_attributes' => NULL, 'path' => NULL, 'image_style' => NULL),
'variables' => array('item' => NULL, 'item_attributes' => NULL, 'url' => NULL, 'image_style' => NULL),
'file' => 'image.field.inc',
),
);
......
......@@ -178,15 +178,14 @@ public function settingsSummary() {
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$url = NULL;
$image_link_setting = $this->getSetting('image_link');
// Check if the formatter involves a link.
if ($image_link_setting == 'content') {
$entity = $items->getEntity();
if (!$entity->isNew()) {
// @todo Remove when theme_image_formatter() has support for route name.
$uri['path'] = $entity->getSystemPath();
$uri['options'] = $entity->urlInfo()->getOptions();
$url = $entity->urlInfo();
}
}
elseif ($image_link_setting == 'file') {
......@@ -206,10 +205,7 @@ public function viewElements(FieldItemListInterface $items) {
if ($item->entity) {
if (isset($link_file)) {
$image_uri = $item->entity->getFileUri();
$uri = array(
'path' => file_create_url($image_uri),
'options' => array(),
);
$url = Url::fromUri(file_create_url($image_uri));
}
// Extract field item attributes for the theme function, and unset them
......@@ -222,7 +218,7 @@ public function viewElements(FieldItemListInterface $items) {
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $image_style_setting,
'#path' => isset($uri) ? $uri : '',
'#url' => $url,
'#cache' => array(
'tags' => $cache_tags,