Issue #3067198 by Berdir, mikelutz, alexpott: Properly deprecate UrlGeneratorTrait

merge-requests/55/head
Alex Pott 2019-07-19 21:47:32 +01:00
parent d747decb4b
commit 330e575d4d
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
35 changed files with 176 additions and 45 deletions

View File

@ -8,8 +8,10 @@ use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Utility base class for thin controllers.
@ -282,6 +284,27 @@ abstract class ControllerBase implements ContainerInjectionInterface {
return $this->languageManager;
}
/**
* Returns a redirect response object for the specified route.
*
* @param string $route_name
* The name of the route to which to redirect.
* @param array $route_parameters
* (optional) Parameters for the route.
* @param array $options
* (optional) An associative array of additional options.
* @param int $status
* (optional) The HTTP redirect status code for the redirect. The default is
* 302 Found.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
$options['absolute'] = TRUE;
return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)->toString(), $status);
}
/**
* Returns the service container.
*

View File

@ -16,7 +16,9 @@ use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Provides the add-page and title callbacks for entities.
@ -101,6 +103,27 @@ class EntityController implements ContainerInjectionInterface {
);
}
/**
* Returns a redirect response object for the specified route.
*
* @param string $route_name
* The name of the route to which to redirect.
* @param array $route_parameters
* (optional) Parameters for the route.
* @param array $options
* (optional) An associative array of additional options.
* @param int $status
* (optional) The HTTP redirect status code for the redirect. The default is
* 302 Found.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
$options['absolute'] = TRUE;
return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)->toString(), $status);
}
/**
* Displays add links for the available bundles.
*

View File

@ -10,7 +10,9 @@ use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Messenger\MessengerTrait;
@ -194,6 +196,27 @@ abstract class FormBase implements FormInterface, ContainerInjectionInterface {
return \Drupal::currentUser();
}
/**
* Returns a redirect response object for the specified route.
*
* @param string $route_name
* The name of the route to which to redirect.
* @param array $route_parameters
* (optional) Parameters for the route.
* @param array $options
* (optional) An associative array of additional options.
* @param int $status
* (optional) The HTTP redirect status code for the redirect. The default is
* 302 Found.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
$options['absolute'] = TRUE;
return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)->toString(), $status);
}
/**
* Returns the service container.
*

View File

@ -33,12 +33,13 @@ trait UrlGeneratorTrait {
* @return string
* The generated URL for the given route.
*
* @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Core\Url instead.
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
* Use \Drupal\Core\Url::fromUri() instead.
*
* @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
*/
protected function url($route_name, $route_parameters = [], $options = []) {
@trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::url() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Url::fromUri() instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
}
@ -57,10 +58,14 @@ trait UrlGeneratorTrait {
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
* Use new RedirectResponse(Url::fromRoute()) instead.
*/
protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
@trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::redirect() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use new RedirectResponse(Url::fromRoute()) instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
$options['absolute'] = TRUE;
$url = $this->url($route_name, $route_parameters, $options);
$url = $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
return new RedirectResponse($url, $status);
}
@ -69,8 +74,12 @@ trait UrlGeneratorTrait {
*
* @return \Drupal\Core\Routing\UrlGeneratorInterface
* The URL generator service.
*
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
* Use the url_generator service instead.
*/
protected function getUrlGenerator() {
@trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::getUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the url_generator service instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
if (!$this->urlGenerator) {
$this->urlGenerator = \Drupal::service('url_generator');
}
@ -84,8 +93,11 @@ trait UrlGeneratorTrait {
* The url generator service.
*
* @return $this
*
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
*/
public function setUrlGenerator(UrlGeneratorInterface $generator) {
@trigger_error(__NAMESPACE__ . "\UrlGeneratorTrait::setUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
$this->urlGenerator = $generator;
return $this;

View File

@ -160,7 +160,7 @@ class AggregatorController extends ControllerBase {
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t('No feeds available. <a href=":link">Add feed</a>.', [':link' => $this->url('aggregator.feed_add')]),
'#empty' => $this->t('No feeds available. <a href=":link">Add feed</a>.', [':link' => Url::fromRoute('aggregator.feed_add')->toString()]),
];
return $build;

View File

@ -6,6 +6,7 @@ use Drupal\aggregator\FeedStorageInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\ClientInterface;
@ -84,7 +85,7 @@ class OpmlFeedAdd extends FormBase {
'#title' => $this->t('Update interval'),
'#default_value' => 3600,
'#options' => $period,
'#description' => $this->t('The length of time between feed updates. Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [':cron' => $this->url('system.status')]),
'#description' => $this->t('The length of time between feed updates. Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [':cron' => Url::fromRoute('system.status')->toString()]),
];
$form['actions'] = ['#type' => 'actions'];

View File

@ -16,6 +16,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -117,7 +118,7 @@ class CommentForm extends ContentEntityForm {
// If not replying to a comment, use our dedicated page callback for new
// Comments on entities.
if (!$comment->id() && !$comment->hasParentComment()) {
$form['#action'] = $this->url('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name]);
$form['#action'] = Url::fromRoute('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name])->toString();
}
$comment_preview = $form_state->get('comment_preview');

View File

@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
@ -361,7 +362,7 @@ class CommentController extends ControllerBase {
$query = $page_number ? ['page' => $page_number] : NULL;
$links[$nid] = [
'new_comment_count' => (int) $new,
'first_new_comment_link' => $this->getUrlGenerator()->generateFromRoute('entity.node.canonical', ['node' => $node->id()], ['query' => $query, 'fragment' => 'new']),
'first_new_comment_link' => Url::fromRoute('entity.node.canonical', ['node' => $node->id()], ['query' => $query, 'fragment' => 'new'])->toString(),
];
}

View File

@ -139,7 +139,7 @@ class ConfigTranslationController extends ControllerBase {
$languages = $this->languageManager->getLanguages();
if (count($languages) == 1) {
$this->messenger()->addWarning($this->t('In order to translate configuration, the website must have at least two <a href=":url">languages</a>.', [':url' => $this->url('entity.configurable_language.collection')]));
$this->messenger()->addWarning($this->t('In order to translate configuration, the website must have at least two <a href=":url">languages</a>.', [':url' => Url::fromRoute('entity.configurable_language.collection')->toString()]));
}
try {

View File

@ -5,6 +5,7 @@ namespace Drupal\contact\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\contact\ContactFormInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@ -66,7 +67,7 @@ class ContactController extends ControllerBase {
if (empty($contact_form)) {
if ($this->currentUser()->hasPermission('administer contact forms')) {
$this->messenger()->addError($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
':add' => $this->url('contact.form_add'),
':add' => Url::fromRoute('contact.form_add')->toString(),
]));
return [];
}

View File

@ -71,8 +71,8 @@ class Overview extends OverviewTerms {
// Use the existing taxonomy overview submit handler.
$form['terms']['#empty'] = $this->t('No containers or forums available. <a href=":container">Add container</a> or <a href=":forum">Add forum</a>.', [
':container' => $this->url('forum.add_container'),
':forum' => $this->url('forum.add_forum'),
':container' => Url::fromRoute('forum.add_container')->toString(),
':forum' => Url::fromRoute('forum.add_forum')->toString(),
]);
return $form;
}

View File

@ -5,6 +5,7 @@ namespace Drupal\language\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
/**
@ -93,7 +94,7 @@ class LanguageAddForm extends LanguageFormBase {
if ($this->moduleHandler->moduleExists('block')) {
// Tell the user they have the option to add a language switcher block
// to their theme so they can switch between the languages.
$this->messenger()->addStatus($this->t('Use one of the language switcher blocks to allow site visitors to switch between languages. You can enable these blocks on the <a href=":block-admin">block administration page</a>.', [':block-admin' => $this->url('block.admin_display')]));
$this->messenger()->addStatus($this->t('Use one of the language switcher blocks to allow site visitors to switch between languages. You can enable these blocks on the <a href=":block-admin">block administration page</a>.', [':block-admin' => Url::fromRoute('block.admin_display')->toString()]));
}
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
}

View File

@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
@ -151,7 +152,7 @@ class NegotiationUrlForm extends ConfigFormBase {
// Throw a form error if the prefix is blank for a non-default language,
// although it is required for selected negotiation type.
$form_state->setErrorByName("prefix][$langcode", $this->t('The prefix may only be left blank for the <a href=":url">selected detection fallback language.</a>', [
':url' => $this->getUrlGenerator()->generate('language.negotiation_selected'),
':url' => Url::fromRoute('language.negotiation_selected')->toString(),
]));
}
}

View File

@ -4,6 +4,7 @@ namespace Drupal\locale\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Configure locale settings for this site.
@ -41,14 +42,14 @@ class LocaleSettingsForm extends ConfigFormBase {
'7' => $this->t('Weekly'),
'30' => $this->t('Monthly'),
],
'#description' => $this->t('Select how frequently you want to check for new interface translations for your currently installed modules and themes. <a href=":url">Check updates now</a>.', [':url' => $this->url('locale.check_translation')]),
'#description' => $this->t('Select how frequently you want to check for new interface translations for your currently installed modules and themes. <a href=":url">Check updates now</a>.', [':url' => Url::fromRoute('locale.check_translation')->toString()]),
];
if ($directory = $config->get('translation.path')) {
$description = $this->t('Translation files are stored locally in the %path directory. You can change this directory on the <a href=":url">File system</a> configuration page.', ['%path' => $directory, ':url' => $this->url('system.file_system_settings')]);
$description = $this->t('Translation files are stored locally in the %path directory. You can change this directory on the <a href=":url">File system</a> configuration page.', ['%path' => $directory, ':url' => Url::fromRoute('system.file_system_settings')->toString()]);
}
else {
$description = $this->t('Translation files will not be stored locally. Change the Interface translation directory on the <a href=":url">File system configuration</a> page.', [':url' => $this->url('system.file_system_settings')]);
$description = $this->t('Translation files will not be stored locally. Change the Interface translation directory on the <a href=":url">File system configuration</a> page.', [':url' => Url::fromRoute('system.file_system_settings')->toString()]);
}
$form['#translation_directory'] = $directory;
$form['use_source'] = [
@ -93,7 +94,7 @@ class LocaleSettingsForm extends ConfigFormBase {
parent::validateForm($form, $form_state);
if (empty($form['#translation_directory']) && $form_state->getValue('use_source') == LOCALE_TRANSLATION_USE_SOURCE_LOCAL) {
$form_state->setErrorByName('use_source', $this->t('You have selected local translation source, but no <a href=":url">Interface translation directory</a> was configured.', [':url' => $this->url('system.file_system_settings')]));
$form_state->setErrorByName('use_source', $this->t('You have selected local translation source, but no <a href=":url">Interface translation directory</a> was configured.', [':url' => Url::fromRoute('system.file_system_settings')->toString()]));
}
}

View File

@ -6,6 +6,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -130,7 +131,7 @@ class TranslationStatusForm extends FormBase {
if (!$languages) {
$empty = $this->t('No translatable languages available. <a href=":add_language">Add a language</a> first.', [
':add_language' => $this->url('entity.configurable_language.collection'),
':add_language' => Url::fromRoute('entity.configurable_language.collection')->toString(),
]);
}
elseif ($status) {
@ -138,7 +139,7 @@ class TranslationStatusForm extends FormBase {
}
else {
$empty = $this->t('No translation status available. <a href=":check">Check manually</a>.', [
':check' => $this->url('locale.check_translation'),
':check' => Url::fromRoute('locale.check_translation')->toString(),
]);
}

View File

@ -281,9 +281,9 @@ class MenuForm extends EntityForm {
];
$form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
':url' => $this->url('entity.menu.add_link_form', ['menu' => $this->entity->id()], [
':url' => Url::fromRoute('entity.menu.add_link_form', ['menu' => $this->entity->id()], [
'query' => ['destination' => $this->entity->toUrl('edit-form')->toString()],
]),
])->toString(),
]);
$links = $this->buildOverviewTreeForm($tree, $delta);

View File

@ -125,7 +125,7 @@ class PathController extends ControllerBase {
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t('No URL aliases available. <a href=":link">Add URL alias</a>.', [':link' => $this->url('path.admin_add')]),
'#empty' => $this->t('No URL aliases available. <a href=":link">Add URL alias</a>.', [':link' => Url::fromRoute('path.admin_add')->toString()]),
];
$build['path_pager'] = ['#type' => 'pager'];

View File

@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\search\SearchPageRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -93,7 +94,7 @@ class SearchBlockForm extends FormBase {
}
$route = 'search.view_' . $entity_id;
$form['#action'] = $this->url($route);
$form['#action'] = Url::fromRoute($route)->toString();
$form['#method'] = 'get';
$form['keys'] = [

View File

@ -5,6 +5,7 @@ namespace Drupal\shortcut\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
/**
* Builds the shortcut set customize form.
@ -33,7 +34,7 @@ class SetCustomize extends EntityForm {
$form['shortcuts']['links'] = [
'#type' => 'table',
'#header' => [t('Name'), t('Weight'), t('Operations')],
'#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [':link' => $this->url('shortcut.link_add', ['shortcut_set' => $this->entity->id()])]),
'#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [':link' => Url::fromRoute('shortcut.link_add', ['shortcut_set' => $this->entity->id()])->toString()]),
'#attributes' => ['id' => 'shortcuts'],
'#tabledrag' => [
[

View File

@ -4,6 +4,7 @@ namespace Drupal\shortcut\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\shortcut\Entity\ShortcutSet;
use Drupal\shortcut\ShortcutSetStorageInterface;
use Drupal\user\UserInterface;
@ -183,7 +184,7 @@ class SwitchShortcutSet extends FormBase {
$replacements = [
'%user' => $this->user->label(),
'%set_name' => $set->label(),
':switch-url' => $this->url('<current>'),
':switch-url' => Url::fromRoute('<current>')->toString(),
];
if ($account_is_user) {
// Only administrators can create new shortcut sets, so we know they have

View File

@ -9,7 +9,6 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\simpletest\TestDiscovery;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Test results form for $test_id.
@ -113,7 +112,7 @@ class SimpletestResultsForm extends FormBase {
$results = [];
if (is_numeric($test_id) && !$results = $this->getResults($test_id)) {
$this->messenger()->addError($this->t('No test results to display.'));
return new RedirectResponse($this->url('simpletest.test_form', [], ['absolute' => TRUE]));
return $this->redirect('simpletest.test_form');
}
// Load all classes and include CSS.
@ -122,7 +121,7 @@ class SimpletestResultsForm extends FormBase {
$filter = static::addResultForm($form, $results, $this->getStringTranslation());
// Actions.
$form['#action'] = $this->url('simpletest.result_form', ['test_id' => 're-run']);
$form['#action'] = Url::fromRoute('simpletest.result_form', ['test_id' => 're-run'])->toString();
$form['action'] = [
'#type' => 'fieldset',
'#title' => $this->t('Actions'),

View File

@ -100,7 +100,7 @@ class SystemController extends ControllerBase {
public function overview($link_id) {
// Check for status report errors.
if ($this->systemManager->checkRequirements() && $this->currentUser()->hasPermission('administer site configuration')) {
$this->messenger()->addError($this->t('One or more problems were detected with your Drupal installation. Check the <a href=":status">status report</a> for more information.', [':status' => $this->url('system.status')]));
$this->messenger()->addError($this->t('One or more problems were detected with your Drupal installation. Check the <a href=":status">status report</a> for more information.', [':status' => Url::fromRoute('system.status')->toString()]));
}
// Load all menu links below it.
$parameters = new MenuTreeParameters();

View File

@ -9,6 +9,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
@ -114,7 +115,7 @@ class CronForm extends FormBase {
'#markup' => $status,
];
$cron_url = $this->url('system.cron', ['key' => $this->state->get('system.cron_key')], ['absolute' => TRUE]);
$cron_url = Url::fromRoute('system.cron', ['key' => $this->state->get('system.cron_key')], ['absolute' => TRUE])->toString();
$form['cron_url'] = [
'#markup' => '<p>' . t('To run cron from outside the site, go to <a href=":cron" class="system-cron-settings__link">@cron</a>', [':cron' => $cron_url, '@cron' => $cron_url]) . '</p>',
];

View File

@ -143,7 +143,7 @@ class PerformanceForm extends ConfigFormBase {
$disabled = !$is_writable;
$disabled_message = '';
if (!$is_writable) {
$disabled_message = ' ' . t('<strong class="error">Set up the <a href=":file-system">public files directory</a> to make these optimizations available.</strong>', [':file-system' => $this->url('system.file_system_settings')]);
$disabled_message = ' ' . t('<strong class="error">Set up the <a href=":file-system">public files directory</a> to make these optimizations available.</strong>', [':file-system' => Url::fromRoute('system.file_system_settings')->toString()]);
}
$form['bandwidth_optimization'] = [

View File

@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Url;
use Drupal\user\PermissionHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -82,7 +83,7 @@ class SiteMaintenanceModeForm extends ConfigFormBase {
'#type' => 'checkbox',
'#title' => t('Put site into maintenance mode'),
'#default_value' => $this->state->get('system.maintenance_mode'),
'#description' => t('Visitors will only see the maintenance mode message. Only users with the "@permission-label" <a href=":permissions-url">permission</a> will be able to access the site. Authorized users can log in directly via the <a href=":user-login">user login</a> page.', ['@permission-label' => $permission_label, ':permissions-url' => $this->url('user.admin_permissions'), ':user-login' => $this->url('user.login')]),
'#description' => t('Visitors will only see the maintenance mode message. Only users with the "@permission-label" <a href=":permissions-url">permission</a> will be able to access the site. Authorized users can log in directly via the <a href=":user-login">user login</a> page.', ['@permission-label' => $permission_label, ':permissions-url' => Url::fromRoute('user.admin_permissions')->toString(), ':user-login' => Url::fromRoute('user.login')->toString()]),
];
$form['maintenance_mode_message'] = [
'#type' => 'textarea',

View File

@ -4,6 +4,7 @@ namespace Drupal\update;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
@ -95,7 +96,7 @@ class UpdateSettingsForm extends ConfigFormBase implements ContainerInjectionInt
'all' => t('All newer versions'),
'security' => t('Only security updates'),
],
'#description' => t('You can choose to send email only if a security update is available, or to be notified about all newer versions. If there are updates available of Drupal core or any of your installed modules and themes, your site will always print a message on the <a href=":status_report">status report</a> page, and will also display an error message on administration pages if there is a security update.', [':status_report' => $this->url('system.status')]),
'#description' => t('You can choose to send email only if a security update is available, or to be notified about all newer versions. If there are updates available of Drupal core or any of your installed modules and themes, your site will always print a message on the <a href=":status_report">status report</a> page, and will also display an error message on administration pages if there is a security update.', [':status_report' => Url::fromRoute('system.status')->toString()]),
];
return parent::buildForm($form, $form_state);

View File

@ -11,6 +11,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin;
@ -156,7 +157,7 @@ abstract class AccountForm extends ContentEntityForm implements TrustedCallbackI
$form['account']['current_pass']['#description'] = $this->t('Required if you want to change the %mail or %pass below. <a href=":request_new_url" title="Send password reset instructions via email.">Reset your password</a>.', [
'%mail' => $form['account']['mail']['#title'],
'%pass' => $this->t('Password'),
':request_new_url' => $this->url('user.pass'),
':request_new_url' => Url::fromRoute('user.pass')->toString(),
]);
}
}

View File

@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -171,7 +172,7 @@ class AccountSettingsForm extends ConfigFormBase {
'#type' => 'radios',
'#title' => $this->t('When cancelling a user account'),
'#default_value' => $config->get('cancel_method'),
'#description' => $this->t('Users with the %select-cancel-method or %administer-users <a href=":permissions-url">permissions</a> can override this default method.', ['%select-cancel-method' => $this->t('Select method for cancelling account'), '%administer-users' => $this->t('Administer users'), ':permissions-url' => $this->url('user.admin_permissions')]),
'#description' => $this->t('Users with the %select-cancel-method or %administer-users <a href=":permissions-url">permissions</a> can override this default method.', ['%select-cancel-method' => $this->t('Select method for cancelling account'), '%administer-users' => $this->t('Administer users'), ':permissions-url' => Url::fromRoute('user.admin_permissions')->toString()]),
];
$form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
foreach (Element::children($form['registration_cancellation']['user_cancel_method']) as $key) {

View File

@ -6,6 +6,7 @@ use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Url;
use Drupal\user\Form\UserPasswordResetForm;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;
@ -125,7 +126,7 @@ class UserController extends ControllerBase {
[
'%other_user' => $account->getAccountName(),
'%resetting_user' => $reset_link_user->getAccountName(),
':logout' => $this->url('user.logout'),
':logout' => Url::fromRoute('user.logout')->toString(),
]));
}
else {

View File

@ -6,6 +6,7 @@ use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\user\UserAuthInterface;
use Drupal\user\UserInterface;
use Drupal\user\UserStorageInterface;
@ -221,11 +222,11 @@ class UserLoginForm extends FormBase {
if ($flood_control_triggered = $form_state->get('flood_control_triggered')) {
if ($flood_control_triggered == 'user') {
$form_state->setErrorByName('name', $this->formatPlural($flood_config->get('user_limit'), 'There has been more than one failed login attempt for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', 'There have been more than @count failed login attempts for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', [':url' => $this->url('user.pass')]));
$form_state->setErrorByName('name', $this->formatPlural($flood_config->get('user_limit'), 'There has been more than one failed login attempt for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', 'There have been more than @count failed login attempts for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', [':url' => Url::fromRoute('user.pass')->toString()]));
}
else {
// We did not find a uid, so the limit is IP-based.
$form_state->setErrorByName('name', $this->t('Too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', [':url' => $this->url('user.pass')]));
$form_state->setErrorByName('name', $this->t('Too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', [':url' => Url::fromRoute('user.pass')->toString()]));
}
}
else {
@ -235,7 +236,7 @@ class UserLoginForm extends FormBase {
// handlers that ran earlier than this one.
$user_input = $form_state->getUserInput();
$query = isset($user_input['name']) ? ['name' => $user_input['name']] : [];
$form_state->setErrorByName('name', $this->t('Unrecognized username or password. <a href=":password">Forgot your password?</a>', [':password' => $this->url('user.pass', [], ['query' => $query])]));
$form_state->setErrorByName('name', $this->t('Unrecognized username or password. <a href=":password">Forgot your password?</a>', [':password' => Url::fromRoute('user.pass', [], ['query' => $query])->toString()]));
$accounts = $this->userStorage->loadByProperties(['name' => $form_state->getValue('name')]);
if (!empty($accounts)) {
$this->logger('user')->notice('Login attempt failed for %user.', ['%user' => $form_state->getValue('name')]);

View File

@ -442,7 +442,7 @@ abstract class PathPluginBase extends DisplayPluginBase implements DisplayRouter
'#title' => $this->t('Path'),
'#description' => $this->t('This view will be displayed by visiting this path on your site. You may use "%" in your URL to represent values that will be used for contextual filters: For example, "node/%/feed". If needed you can even specify named route parameters like taxonomy/term/%taxonomy_term'),
'#default_value' => $this->getOption('path'),
'#field_prefix' => '<span dir="ltr">' . $this->url('<none>', [], ['absolute' => TRUE]),
'#field_prefix' => '<span dir="ltr">' . Url::fromRoute('<none>', [], ['absolute' => TRUE])->toString(),
'#field_suffix' => '</span>&lrm;',
'#attributes' => ['dir' => LanguageInterface::DIRECTION_LTR],
// Account for the leading backslash.

View File

@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Url;
use Drupal\views\Entity\View;
use Drupal\views\Views;
use Drupal\views_ui\ViewUI;
@ -215,7 +216,7 @@ abstract class WizardPluginBase extends PluginBase implements WizardInterface {
public function buildForm(array $form, FormStateInterface $form_state) {
$style_options = Views::fetchPluginNames('style', 'normal', [$this->base_table]);
$feed_row_options = Views::fetchPluginNames('row', 'feed', [$this->base_table]);
$path_prefix = $this->url('<none>', [], ['absolute' => TRUE]);
$path_prefix = Url::fromRoute('<none>', [], ['absolute' => TRUE])->toString();
// Add filters and sorts which apply to the view as a whole.
$this->buildFilters($form, $form_state);

View File

@ -37,11 +37,11 @@ class ReorderDisplays extends ViewsFormBase {
$form['#title'] = $this->t('Reorder displays');
$form['#section'] = 'reorder';
$form['#action'] = $this->url('views_ui.form_reorder_displays', [
$form['#action'] = Url::fromRoute('views_ui.form_reorder_displays', [
'js' => 'nojs',
'view' => $view->id(),
'display_id' => $display_id,
]);
])->toString();
$form['view'] = [
'#type' => 'value',
'#value' => $view,

View File

@ -11,6 +11,7 @@ use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Url;
use Drupal\views\Ajax\HighlightCommand;
use Drupal\views\Ajax\ReplaceTitleCommand;
use Drupal\views\Ajax\ShowButtonsCommand;
@ -151,7 +152,7 @@ abstract class ViewsFormBase extends FormBase implements ViewsFormInterface {
elseif (!$form_state->get('ajax')) {
// if nothing on the stack, non-js forms just go back to the main view editor.
$display_id = $form_state->get('display_id');
return new RedirectResponse($this->url('entity.view.edit_display_form', ['view' => $view->id(), 'display_id' => $display_id], ['absolute' => TRUE]));
return new RedirectResponse(Url::fromRoute('entity.view.edit_display_form', ['view' => $view->id(), 'display_id' => $display_id], ['absolute' => TRUE])->toString());
}
else {
$response = new AjaxResponse();
@ -252,7 +253,7 @@ abstract class ViewsFormBase extends FormBase implements ViewsFormInterface {
// Views provides its own custom handling of AJAX form submissions.
// Usually this happens at the same path, but custom paths may be
// specified in $form_state.
$form_url = $form_state->has('url') ? $form_state->get('url')->toString() : $this->url('<current>');
$form_url = $form_state->has('url') ? $form_state->get('url')->toString() : Url::fromRoute('<current>')->toString();
$response->addCommand(new SetFormCommand($form_url));
if ($section = $form_state->get('#section')) {

View File

@ -7,12 +7,16 @@ use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Routing\UrlGeneratorTrait
* @group Routing
* @group legacy
*/
class UrlGeneratorTraitTest extends UnitTestCase {
/**
* @covers ::setUrlGenerator
* @covers ::getUrlGenerator
*
* @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::setUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/2614344
* @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::getUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the url_generator service instead. See https://www.drupal.org/node/2614344
*/
public function testGetUrlGenerator() {
$url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
@ -28,6 +32,8 @@ class UrlGeneratorTraitTest extends UnitTestCase {
/**
* @covers ::redirect
*
* @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::redirect() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use new RedirectResponse(Url::fromRoute()) instead. See https://www.drupal.org/node/2614344
*/
public function testRedirect() {
$route_name = 'some_route_name';
@ -50,4 +56,29 @@ class UrlGeneratorTraitTest extends UnitTestCase {
$this->assertSame($generated_url, $result->getTargetUrl());
}
/**
* @covers ::url
*
* @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::url() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Url::fromUri() instead. See https://www.drupal.org/node/2614344
*/
public function testUrl() {
$route_name = 'some_route_name';
$generated_url = 'some/generated/url';
$url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
$url_generator->expects($this->once())
->method('generateFromRoute')
->with($route_name, [], [])
->willReturn($generated_url);
$url_generator_trait_object = $this->getMockForTrait('Drupal\Core\Routing\UrlGeneratorTrait');
$url_generator_trait_object->setUrlGenerator($url_generator);
$url_generator_method = new \ReflectionMethod($url_generator_trait_object, 'url');
$url_generator_method->setAccessible(TRUE);
$result = $url_generator_method->invoke($url_generator_trait_object, $route_name);
$this->assertSame($generated_url, $result);
}
}