LinkGeneratorInterface.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Drupal\Core\Utility;
  3. use Drupal\Core\Link;
  4. use Drupal\Core\Url;
  5. /**
  6. * Defines an interface for generating links from route names and parameters.
  7. */
  8. interface LinkGeneratorInterface {
  9. /**
  10. * Renders a link to a URL.
  11. *
  12. * Examples:
  13. * @code
  14. * $link_generator = \Drupal::service('link_generator');
  15. * $installer_url = \Drupal\Core\Url::fromUri('base://core/install.php');
  16. * $installer_link = $link_generator->generate($text, $installer_url);
  17. * $external_url = \Drupal\Core\Url::fromUri('http://example.com', ['query' => ['foo' => 'bar']]);
  18. * $external_link = $link_generator->generate($text, $external_url);
  19. * $internal_url = \Drupal\Core\Url::fromRoute('system.admin');
  20. * $internal_link = $link_generator->generate($text, $internal_url);
  21. * @endcode
  22. * However, for links enclosed in translatable text you should use t() and
  23. * embed the HTML anchor tag directly in the translated string. For example:
  24. * @code
  25. * $text = t('Visit the <a href=":url">content types</a> page', array(':url' => \Drupal::url('entity.node_type.collection')));
  26. * @endcode
  27. * This keeps the context of the link title ('settings' in the example) for
  28. * translators.
  29. *
  30. * @param string|array|\Drupal\Component\Render\MarkupInterface $text
  31. * The link text for the anchor tag as a translated string or render array.
  32. * Strings will be sanitized automatically. If you need to output HTML in
  33. * the link text, use a render array or an already sanitized string such as
  34. * the output of \Drupal\Component\Utility\Xss::filter() or
  35. * \Drupal\Component\Render\FormattableMarkup.
  36. * @param \Drupal\Core\Url $url
  37. * The URL object used for the link. Amongst its options, the following may
  38. * be set to affect the generated link:
  39. * - attributes: An associative array of HTML attributes to apply to the
  40. * anchor tag. If element 'class' is included, it must be an array; 'title'
  41. * must be a string; other elements are more flexible, as they just need
  42. * to work as an argument for the constructor of the class
  43. * Drupal\Core\Template\Attribute($options['attributes']).
  44. * - language: An optional language object. If the path being linked to is
  45. * internal to the site, $options['language'] is used to determine whether
  46. * the link is "active", or pointing to the current page (the language as
  47. * well as the path must match).
  48. * - 'set_active_class': Whether this method should compare the $route_name,
  49. * $parameters, language and query options to the current URL to determine
  50. * whether the link is "active". Defaults to FALSE. If TRUE, an "active"
  51. * class will be applied to the link. It is important to use this
  52. * sparingly since it is usually unnecessary and requires extra
  53. * processing.
  54. *
  55. * @return \Drupal\Core\GeneratedLink
  56. * A GeneratedLink object containing a link to the given route and
  57. * parameters and bubbleable metadata.
  58. *
  59. * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
  60. * Thrown when the named route doesn't exist.
  61. * @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
  62. * Thrown when some parameters are missing that are mandatory for the route.
  63. * @throws \Symfony\Component\Routing\Exception\InvalidParameterException
  64. * Thrown when a parameter value for a placeholder is not correct because it
  65. * does not match the requirement.
  66. *
  67. * @internal
  68. * Should not be used in user code. Use \Drupal\Core\Link instead.
  69. */
  70. public function generate($text, Url $url);
  71. /**
  72. * Renders a link from a link object.
  73. *
  74. * @param \Drupal\Core\Link $link
  75. * A link object to convert to a string.
  76. *
  77. * @return \Drupal\Core\GeneratedLink
  78. * A GeneratedLink object containing a link to the given route and
  79. * parameters and bubbleable metadata.
  80. *
  81. * @internal
  82. * Should not be used in user code.
  83. * Use \Drupal\Core\Link instead.
  84. */
  85. public function generateFromLink(Link $link);
  86. }