link.module 3.93 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Defines simple link field types.
 */

8
use Drupal\Component\Utility\String;
9
use Symfony\Component\HttpFoundation\Request;
10

11 12 13
/**
 * Implements hook_help().
 */
14 15 16
function link_help($route_name, Request $request) {
  switch ($route_name) {
    case 'help.page.link':
17 18
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
19
      $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>.', array('!field' => \Drupal::url('help.page', array('name' => 'field')), '!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')), '!link_documentation' => 'https://drupal.org/documentation/modules/link')) . '</p>';
20 21 22 23 24 25 26 27 28 29 30
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Managing and displaying link fields') . '</dt>';
      $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.', array('!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
      $output .= '<dt>' . t('Adding link text') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Displaying link text') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Adding attributes to links') . '</dt>';
      $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>';
      $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 38 39 40 41 42
  }
}

/**
 * Implements hook_theme().
 */
function link_theme() {
  return array(
    'link_formatter_link_separate' => array(
43
      'variables' => array('title' => NULL, 'url_title' => NULL, 'url' => NULL),
44
      'template' => 'link-formatter-link-separate',
45 46 47 48 49
    ),
  );
}

/**
50 51 52 53 54 55 56 57 58 59 60
 * Prepares variables for separated link field templates.
 *
 * This template outputs a separate title and link.
 *
 * Default template: link-formatter-link-separate.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - title: (optional) A descriptive or alternate title for the link, which
 *     may be different than the actual link text.
 *   - url_title: The anchor text for the link.
61
 *   - url: A \Drupal\Core\Url object.
62
 */
63 64
function template_preprocess_link_formatter_link_separate(&$variables) {
  if (!empty($variables['title'])) {
65
    $variables['title'] = String::checkPlain($variables['title']);
66
  }
67 68 69 70 71 72 73

  if (!$variables['url']->isExternal()) {
    $variables['link'] = \Drupal::linkGenerator()->generateFromUrl($variables['url_title'], $variables['url']);
  }
  else {
    $variables['link'] = l($variables['url_title'], $variables['url']->getPath(), $variables['url']->getOptions());
  }
74
}