PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Utility/LinkGenerator.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 148 lines | 63 code | 21 blank | 64 comment | 7 complexity | 6fc910b0928e212c144eaa03dd9cdfff MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\Utility\LinkGenerator.
  5. */
  6. namespace Drupal\Core\Utility;
  7. use Drupal\Component\Utility\Json;
  8. use Drupal\Component\Utility\String;
  9. use Drupal\Core\Extension\ModuleHandlerInterface;
  10. use Drupal\Core\Path\AliasManagerInterface;
  11. use Drupal\Core\Template\Attribute;
  12. use Drupal\Core\Routing\UrlGeneratorInterface;
  13. use Drupal\Core\Url;
  14. /**
  15. * Provides a class which generates a link with route names and parameters.
  16. */
  17. class LinkGenerator implements LinkGeneratorInterface {
  18. /**
  19. * The url generator.
  20. *
  21. * @var \Drupal\Core\Routing\UrlGeneratorInterface
  22. */
  23. protected $urlGenerator;
  24. /**
  25. * The module handler firing the route_link alter hook.
  26. *
  27. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  28. */
  29. protected $moduleHandler;
  30. /**
  31. * The path alias manager.
  32. *
  33. * @var \Drupal\Core\Path\AliasManagerInterface
  34. */
  35. protected $aliasManager;
  36. /**
  37. * Constructs a LinkGenerator instance.
  38. *
  39. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
  40. * The url generator.
  41. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  42. * The module handler.
  43. * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
  44. * The path alias manager.
  45. */
  46. public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AliasManagerInterface $alias_manager) {
  47. $this->urlGenerator = $url_generator;
  48. $this->moduleHandler = $module_handler;
  49. $this->aliasManager = $alias_manager;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. *
  54. * For anonymous users, the "active" class will be calculated on the server,
  55. * because most sites serve each anonymous user the same cached page anyway.
  56. * For authenticated users, the "active" class will be calculated on the
  57. * client (through JavaScript), only data- attributes are added to links to
  58. * prevent breaking the render cache. The JavaScript is added in
  59. * system_page_build().
  60. *
  61. * @see system_page_build()
  62. */
  63. public function generateFromUrl($text, Url $url) {
  64. // Start building a structured representation of our link to be altered later.
  65. $variables = array(
  66. // @todo Inject the service when drupal_render() is converted to one.
  67. 'text' => is_array($text) ? drupal_render($text) : $text,
  68. 'url' => $url,
  69. 'options' => $url->getOptions(),
  70. );
  71. // Merge in default options.
  72. $variables['options'] += array(
  73. 'attributes' => array(),
  74. 'query' => array(),
  75. 'html' => FALSE,
  76. 'language' => NULL,
  77. 'set_active_class' => FALSE,
  78. 'absolute' => FALSE,
  79. );
  80. // Add a hreflang attribute if we know the language of this link's url and
  81. // hreflang has not already been set.
  82. if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
  83. $variables['options']['attributes']['hreflang'] = $variables['options']['language']->id;
  84. }
  85. // Set the "active" class if the 'set_active_class' option is not empty.
  86. if (!empty($variables['options']['set_active_class'])) {
  87. // Add a "data-drupal-link-query" attribute to let the
  88. // drupal.active-link library know the query in a standardized manner.
  89. if (!empty($variables['options']['query'])) {
  90. $query = $variables['options']['query'];
  91. ksort($query);
  92. $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
  93. }
  94. // Add a "data-drupal-link-system-path" attribute to let the
  95. // drupal.active-link library know the path in a standardized manner.
  96. if (!isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
  97. $path = $url->getInternalPath();
  98. $variables['options']['attributes']['data-drupal-link-system-path'] = $this->aliasManager->getSystemPath($path);
  99. }
  100. }
  101. // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
  102. // only when a quick strpos() gives suspicion tags are present.
  103. if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
  104. $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
  105. }
  106. // Allow other modules to modify the structure of the link.
  107. $this->moduleHandler->alter('link', $variables);
  108. // Move attributes out of options. generateFromRoute(() doesn't need them.
  109. $attributes = new Attribute($variables['options']['attributes']);
  110. unset($variables['options']['attributes']);
  111. $url->setOptions($variables['options']);
  112. // The result of the url generator is a plain-text URL. Because we are using
  113. // it here in an HTML argument context, we need to encode it properly.
  114. $url = String::checkPlain($url->toString());
  115. // Sanitize the link text if necessary.
  116. $text = $variables['options']['html'] ? $variables['text'] : String::checkPlain($variables['text']);
  117. return '<a href="' . $url . '"' . $attributes . '>' . $text . '</a>';
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function generate($text, $route_name, array $parameters = array(), array $options = array()) {
  123. $url = new Url($route_name, $parameters, $options);
  124. $url->setUrlGenerator($this->urlGenerator);
  125. return $this->generateFromUrl($text, $url);
  126. }
  127. }