link.module 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @file
  4. * Defines simple link field types.
  5. */
  6. use Drupal\Core\Link;
  7. use Drupal\Core\Url;
  8. use Drupal\Core\Routing\RouteMatchInterface;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function link_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. case 'help.page.link':
  15. $output = '';
  16. $output .= '<h3>' . t('About') . '</h3>';
  17. $output .= '<p>' . t('The Link module allows you to create fields that contain internal or external URLs and optional link text. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":link_documentation">online documentation for the Link module</a>.', [':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#', ':link_documentation' => 'https://www.drupal.org/documentation/modules/link']) . '</p>';
  18. $output .= '<h3>' . t('Uses') . '</h3>';
  19. $output .= '<dl>';
  20. $output .= '<dt>' . t('Managing and displaying link fields') . '</dt>';
  21. $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the link field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
  22. $output .= '<dt>' . t('Setting the allowed link type') . '</dt>';
  23. $output .= '<dd>' . t('In the field settings you can define the allowed link type to be <em>internal links only</em>, <em>external links only</em>, or <em>both internal and external links</em>. <em>Internal links only</em> and <em>both internal and external links</em> options enable an autocomplete widget for internal links, so a user does not have to copy or remember a URL.') . '</dd>';
  24. $output .= '<dt>' . t('Adding link text') . '</dt>';
  25. $output .= '<dd>' . t('In the field settings you can define additional link text to be <em>optional</em> or <em>required</em> in any link field.') . '</dd>';
  26. $output .= '<dt>' . t('Displaying link text') . '</dt>';
  27. $output .= '<dd>' . t('If link text has been submitted for a URL, then by default this link text is displayed as a link to the URL. If you want to display both the link text <em>and</em> the URL, choose the appropriate link format from the drop-down menu in the <em>Manage display</em> page. If you only want to display the URL even if link text has been submitted, choose <em>Link</em> as the format, and then change its <em>Format settings</em> to display <em>URL only</em>.') . '</dd>';
  28. $output .= '<dt>' . t('Adding attributes to links') . '</dt>';
  29. $output .= '<dd>' . t('You can add attributes to links, by changing the <em>Format settings</em> in the <em>Manage display</em> page. Adding <em>rel="nofollow"</em> notifies search engines that links should not be followed.') . '</dd>';
  30. $output .= '<dt>' . t('Validating URLs') . '</dt>';
  31. $output .= '<dd>' . t('All links are validated after a link field is filled in. They can include anchors or query strings.') . '</dd>';
  32. $output .= '</dl>';
  33. return $output;
  34. }
  35. }
  36. /**
  37. * Implements hook_theme().
  38. */
  39. function link_theme() {
  40. return [
  41. 'link_formatter_link_separate' => [
  42. 'variables' => ['title' => NULL, 'url_title' => NULL, 'url' => NULL],
  43. ],
  44. ];
  45. }
  46. /**
  47. * Prepares variables for separated link field templates.
  48. *
  49. * This template outputs a separate title and link.
  50. *
  51. * Default template: link-formatter-link-separate.html.twig.
  52. *
  53. * @param array $variables
  54. * An associative array containing:
  55. * - title: (optional) A descriptive or alternate title for the link, which
  56. * may be different than the actual link text.
  57. * - url_title: The anchor text for the link.
  58. * - url: A \Drupal\Core\Url object.
  59. */
  60. function template_preprocess_link_formatter_link_separate(&$variables) {
  61. $variables['link'] = Link::fromTextAndUrl($variables['url_title'], $variables['url'])->toString();
  62. }