Projet

Général

Profil

Révision 8e7483ab

Ajouté par Assos Assos il y a plus de 4 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/link/link.module
8 8
define('LINK_EXTERNAL', 'external');
9 9
define('LINK_INTERNAL', 'internal');
10 10
define('LINK_FRONT', 'front');
11
define('LINK_FRAGMENT', 'fragment');
12
define('LINK_QUERY', 'query');
11 13
define('LINK_EMAIL', 'email');
14
define('LINK_TEL', 'tel');
12 15
define('LINK_NEWS', 'news');
16
define('LINK_FILE', 'file');
13 17
define('LINK_TARGET_DEFAULT', 'default');
14 18
define('LINK_TARGET_NEW_WINDOW', '_blank');
15 19
define('LINK_TARGET_TOP', '_top');
......
26 30
function link_help($path, $arg) {
27 31
  switch ($path) {
28 32
    case 'admin/help#link':
29
      $output = '<p><strong>About</strong></p>';
30
      $output .= '<p>' . 'The link provides a standard custom content field for links. Links can be easily added to any content types and profiles and include advanced validating and different ways of storing internal or external links and URLs. It also supports additional link text title, site wide tokens for titles and title attributes, target attributes, css class attribution, static repeating values, input conversion, and many more.' . '</p>';
31
      $output .= '<p>' . '<strong>Requirements / Dependencies</strong>' . '</p>';
33
      $output = '<p><strong>' . t('About') . '</strong></p>';
34
      $output .= '<p>' . t('The link provides a standard custom content field for links. Links can be easily added to any content types and profiles and include advanced validating and different ways of storing internal or external links and URLs. It also supports additional link text title, site wide tokens for titles and title attributes, target attributes, css class attribution, static repeating values, input conversion, and many more.') . '</p>';
35
      $output .= '<p><strong>' . t('Requirements / Dependencies') . '</strong></p>';
32 36
      $output .= '<p>' . 'Fields API is provided already by core [no dependencies].' . '</p>';
33 37
      $output .= '<p><strong>Configuration</strong></p>';
34 38
      $output .= '<p>' . 'Configuration is only slightly more complicated than a text field. Link text titles for URLs can be made required, set as instead of URL, optional (default), or left out entirely. If no link text title is provided, the trimmed version of the complete URL will be displayed. The target attribute should be set to "_blank", "top", or left out completely (checkboxes provide info). The rel=nofollow attribute prevents the link from being followed by certain search engines.' . '</p>';
......
195 199
    '#size' => 3,
196 200
  );
197 201

  
202
  // Target options. E.g. New window = target="_blank".
198 203
  $target_options = array(
199 204
    LINK_TARGET_DEFAULT => t('Default (no target attribute)'),
200 205
    LINK_TARGET_TOP => t('Open link in window root'),
201 206
    LINK_TARGET_NEW_WINDOW => t('Open link in new window'),
202 207
    LINK_TARGET_USER => t('Allow the user to choose'),
203 208
  );
209

  
204 210
  $form['attributes'] = array(
205 211
    '#tree' => TRUE,
206 212
  );
213

  
207 214
  $form['attributes']['target'] = array(
208 215
    '#type' => 'radios',
209 216
    '#title' => t('Link Target'),
......
300 307
  foreach ($entities as $id => $entity) {
301 308
    foreach ($items[$id] as $delta => $item) {
302 309
      $items[$id][$delta]['attributes'] = _link_load($field, $item, $instances[$id]);
310
      $items[$id][$delta]['original_title'] = $item['title'];
311
      $items[$id][$delta]['original_url'] = $item['url'];
303 312
    }
304 313
  }
305 314
}
......
525 534
 */
526 535
function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
527 536
  // @codingStandardsIgnoreEnd
537
  // As this function can be called multiple times and the item is changed by
538
  // reference we need to ensure that there's always the original data to
539
  // process otherwise processed data are processed again which might leads to
540
  // unexpected results.
541
  if (isset($item['_link_sanitized'])) {
542
    return;
543
  }
544

  
545
  // Store a flag to check in case of a second call.
546
  $item['_link_sanitized'] = TRUE;
547

  
528 548
  // Don't try to process empty links.
529 549
  if (empty($item['url']) && empty($item['title'])) {
530 550
    return;
......
561 581
  if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
562 582
    $type = LINK_EXTERNAL;
563 583
  }
584
  elseif ($type == LINK_FRAGMENT || $type == LINK_QUERY) {
585
    // If type is a fragment or query, then use the current URL.
586
    $item['url'] = $_GET['q'] . $item['url'];
587
  }
564 588
  $url = link_cleanup_url($item['url']);
565 589
  $url_parts = _link_parse_url($url);
566 590

  
567 591
  if (!empty($url_parts['url'])) {
568
    $item['url'] = url($url_parts['url'],
569
      array(
570
        'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
571
        'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
572
        'absolute' => !empty($instance['settings']['absolute_url']),
573
        'html' => TRUE,
574
      )
575
    );
592
    $item = array(
593
      'url' => $url_parts['url'],
594
      'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
595
      'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
596
      'absolute' => !empty($instance['settings']['absolute_url']),
597
      'html' => TRUE,
598
    ) + $item;
576 599
  }
577 600

  
578 601
  // Create a shortened URL for display.
579 602
  if ($type == LINK_EMAIL) {
580 603
    $display_url = str_replace('mailto:', '', $url);
581 604
  }
605
  elseif ($type === LINK_EXTERNAL) {
606
    $display_url = $item['url'];
607
  }
608
  elseif ($type == LINK_TEL) {
609
    $display_url = str_replace('tel:', '', $url);
610
  }
582 611
  else {
583 612
    $display_url = url($url_parts['url'],
584 613
      array(
......
589 618
    );
590 619
  }
591 620
  if ($instance['settings']['display']['url_cutoff'] && strlen($display_url) > $instance['settings']['display']['url_cutoff']) {
592
    $display_url = substr($display_url, 0, $instance['settings']['display']['url_cutoff']) . "...";
621
    $display_url = substr($display_url, 0, $instance['settings']['display']['url_cutoff']) . "";
593 622
  }
594 623
  $item['display_url'] = $display_url;
595 624

  
......
602 631
    }
603 632
  }
604 633
  // Use the title defined by the user at the widget level.
605
  elseif (drupal_strlen(trim($item['title']))) {
634
  elseif (isset($item['title']) && drupal_strlen(trim($item['title']))) {
606 635
    $title = $item['title'];
607 636
  }
608 637
  // Use the static title if a user-defined title is optional and a static title
......
702 731
      else {
703 732
        $entity_loaded = $entity;
704 733
      }
705
      $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
734
      $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded), array('clear' => TRUE));
706 735
    }
707 736
    $item['attributes']['title'] = filter_xss($item['attributes']['title'], array(
708 737
      'b',
......
851 880
 * Formats a link field widget.
852 881
 */
853 882
function theme_link_field($vars) {
854
  drupal_add_css(drupal_get_path('module', 'link') . '/link.css');
883
  drupal_add_css(drupal_get_path('module', 'link') . '/css/link.css');
855 884
  $element = $vars['element'];
856 885
  // Prefix single value link fields with the name of the field.
857 886
  if (empty($element['#field']['multiple'])) {
......
916 945
 */
917 946
function link_field_process($element, $form_state, $complete_form) {
918 947
  $instance = field_widget_instance($element, $form_state);
948
  if (!$instance) {
949
    // The element comes from a custom form, we have to manually create the
950
    // $instance settings.
951
    $instance['settings'] = array(
952
      'title_maxlength' => isset($element['#title_maxlength']) ? $element['#title_maxlength'] : 128,
953
      'title' => isset($element['#title_mode']) ? $element['#title_mode'] : 'optional',
954
      'title_label_use_field_label' => isset($element['#title_label_use_field_label']) ? $element['#title_label_use_field_label'] : FALSE,
955
      'url' => isset($element['#url']) ? $element['#url'] : 'optional',
956
    );
957
    if (isset($element['#attributes'])) {
958
      $instance['settings']['attributes'] = $element['#attributes'];
959
    }
960
  }
919 961
  $settings = $instance['settings'];
920 962
  $element['url'] = array(
921 963
    '#type' => 'textfield',
......
1017 1059
      'label' => t('Title, as link (default)'),
1018 1060
      'field types' => array('link_field'),
1019 1061
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
1062
      'settings' => array(
1063
        'custom_title' => '',
1064
      ),
1020 1065
    ),
1021 1066
    'link_title_plain' => array(
1022 1067
      'label' => t('Title, as plain text'),
......
1088 1133
      '#default_value' => $settings['strip_www'],
1089 1134
    );
1090 1135
  }
1136
  if ($display['type'] == 'link_default') {
1137
    $element['custom_title'] = array(
1138
      '#title' => t('Override title'),
1139
      '#description' => t('Optionally override the title for the link(s).'),
1140
      '#type' => 'textfield',
1141
      '#default_value' => $settings['custom_title'],
1142
    );
1143
  }
1091 1144
  return $element;
1092 1145
}
1093 1146

  
......
1106 1159
      return t('Leave www. in domain');
1107 1160
    }
1108 1161
  }
1162
  if ($display['type'] == 'link_default') {
1163
    if ($display['settings']['custom_title']) {
1164
      return t('Title: %title', array('%title' => $display['settings']['custom_title']));
1165
    }
1166
  }
1109 1167
  return '';
1110 1168
}
1111 1169

  
......
1115 1173
function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
1116 1174
  $elements = array();
1117 1175
  foreach ($items as $delta => $item) {
1176
    if (!empty($display['settings']['custom_title'])) {
1177
      $item['title'] = $display['settings']['custom_title'];
1178
    }
1118 1179
    $elements[$delta] = array(
1119 1180
      '#theme' => 'link_formatter_' . $display['type'],
1120 1181
      '#element' => $item,
1121 1182
      '#field' => $instance,
1122
      '#display' => $display,
1183
      '#display' => array(
1184
        'settings' => $display['settings'],
1185
      ),
1123 1186
    );
1124 1187
  }
1125 1188
  return $elements;
......
1138 1201
  }
1139 1202
  // Display a normal link if both title and URL are available.
1140 1203
  if (!empty($vars['element']['title']) && !empty($vars['element']['url'])) {
1141
    return l($vars['element']['title'], $vars['element']['url'], $link_options);
1204
    return l($vars['element']['title'], rawurldecode($vars['element']['url']), $link_options);
1142 1205
  }
1143 1206
  // If only a title, display the title.
1144 1207
  elseif (!empty($vars['element']['title'])) {
1145 1208
    return !empty($link_options['html']) ? $vars['element']['title'] : check_plain($vars['element']['title']);
1146 1209
  }
1147 1210
  elseif (!empty($vars['element']['url'])) {
1148
    return l($vars['element']['title'], $vars['element']['url'], $link_options);
1211
    return l($vars['element']['title'], rawurldecode($vars['element']['url']), $link_options);
1149 1212
  }
1150 1213
}
1151 1214

  
......
1222 1285
 */
1223 1286
function theme_link_formatter_link_url($vars) {
1224 1287
  $link_options = $vars['element'];
1288
  if (isset($link_options['attributes']['class'])) {
1289
    $link_options['attributes']['class'] = array($link_options['attributes']['class']);
1290
  }
1225 1291
  unset($link_options['title']);
1226 1292
  unset($link_options['url']);
1227 1293
  return $vars['element']['url'] ? l($vars['element']['display_url'], $vars['element']['url'], $link_options) : '';
......
1244 1310
  $link_options = $vars['element'];
1245 1311
  unset($link_options['title']);
1246 1312
  unset($link_options['url']);
1247
  return $vars['element']['url'] ? l($vars['field']['label'], $vars['element']['url'], $link_options) : '';
1313
  $label = $vars['field']['label'];
1314
  if (function_exists('i18n_string_translate')) {
1315
    $i18n_string_name = "field:{$vars['field']['field_name']}:{$vars['field']['bundle']}:label";
1316
    $label = i18n_string_translate($i18n_string_name, $label);
1317
  }
1318
  return $vars['element']['url'] ? l($label, $vars['element']['url'], $link_options) : '';
1248 1319
}
1249 1320

  
1250 1321
/**
......
1365 1436
  $type = link_url_type($text);
1366 1437

  
1367 1438
  if ($type && ($type == LINK_INTERNAL || $type == LINK_EXTERNAL)) {
1368
    $flag = valid_url($text, TRUE);
1439
    $flag = valid_url($text, $type == LINK_EXTERNAL);
1369 1440
    if (!$flag) {
1370 1441
      $normal_path = drupal_get_normal_path($text, $langcode);
1371 1442
      $parsed_link = parse_url($normal_path, PHP_URL_PATH);
......
1504 1575

  
1505 1576
  $link_ichars = $link_ichars_domain . (string) html_entity_decode(implode("", array(
1506 1577
      // ß.
1507
      "&#x00DF;",
1508
    )), ENT_QUOTES, 'UTF-8');
1578
    "&#x00DF;",
1579
  )), ENT_QUOTES, 'UTF-8');
1509 1580
  $allowed_protocols = variable_get('filter_allowed_protocols', array(
1510 1581
    'http',
1511 1582
    'https',
1512 1583
    'ftp',
1584
    'file',
1513 1585
    'news',
1514 1586
    'nntp',
1515 1587
    'telnet',
......
1518 1590
    'ssh',
1519 1591
    'sftp',
1520 1592
    'webcal',
1593
    'tel',
1521 1594
  ));
1522 1595
  $link_domains = _link_domains();
1523 1596

  
......
1538 1611

  
1539 1612
  $directories = "(?:\/[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'#!():;*@\[\]]*)*";
1540 1613
  // Yes, four backslashes == a single backslash.
1541
  $query = "(?:\/?\?([?a-z0-9" . $link_ichars . "+_|\-\.~\/\\\\%=&,$'!():;*@\[\]{} ]*))";
1542
  $anchor = "(?:#[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'():;*@\[\]\/\?]*)";
1614
  $query = "(?:\/?\?([?a-zA-Z0-9" . $link_ichars . "+_|\-\.~\/\\\\%=&,$'!():;*@\[\]{} ]*))";
1615
  $anchor = "(?:#[a-zA-Z0-9" . $link_ichars . "_\-\.~+%=&,$'():;*@\[\]\/\?!]*)";
1543 1616

  
1544 1617
  // The rest of the path for a standard URL.
1545 1618
  // @codingStandardsIgnoreLine
......
1551 1624

  
1552 1625
  $user = '[a-zA-Z0-9' . $link_ichars . '_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
1553 1626
  $email_pattern = '/^mailto:' . $user . '@' . '(?:' . $domain . '|' . $ipv4 . '|' . $ipv6 . '|localhost)' . $query . '?$/';
1627
  $tel_pattern = '/^tel:(?:\+[1-9]\d{1,14}|\d{2,15})$/';
1628

  
1629
  $file_pattern = "/^(?:file:\/\/)" . "(?:\/?[a-z0-9" . $link_ichars . "_\-\.\\\~+%=&,$'#!():;*@\[\]]*)*" . '$/i';
1554 1630

  
1555 1631
  if (strpos($text, '<front>') === 0) {
1556 1632
    return LINK_FRONT;
......
1558 1634
  if (in_array('mailto', $allowed_protocols) && preg_match($email_pattern, $text)) {
1559 1635
    return LINK_EMAIL;
1560 1636
  }
1637
  if (strpos($text, '#') === 0) {
1638
    return LINK_FRAGMENT;
1639
  }
1640
  if (strpos($text, '?') === 0) {
1641
    return LINK_QUERY;
1642
  }
1643
  if (in_array('tel', $allowed_protocols) && strpos($text, 'tel:') === 0) {
1644
    if (preg_match($tel_pattern, $text)) {
1645
      // Based on our tel pattern this is a 'valid' phone number so return tel
1646
      // type.
1647
      return LINK_TEL;
1648
    }
1649
    else {
1650
      // Based on our tel pattern this is using the tel protocol, but is not a
1651
      // 'valid' phone number. If we don't return false here $text will match
1652
      // LINK_EXTERNAL which is incorrect.
1653
      return FALSE;
1654
    }
1655
  }
1561 1656
  if (in_array('news', $allowed_protocols) && preg_match($news_pattern, $text)) {
1562 1657
    return LINK_NEWS;
1563 1658
  }
1659
  if (in_array('file', $allowed_protocols) && preg_match($file_pattern, $text, $as)) {
1660
    return LINK_FILE;
1661
  }
1564 1662
  if (preg_match($internal_pattern . $end, $text)) {
1565 1663
    return LINK_INTERNAL;
1566 1664
  }
......
1599 1697
    $field_value['type'] = 'link_field';
1600 1698
    // Remove settings that are now on the instance.
1601 1699
    foreach (array(
1602
               'attributes',
1603
               'display',
1604
               'url',
1605
               'title',
1606
               'title_value',
1607
               'enable_tokens',
1608
               'validate_url',
1609
             ) as $setting) {
1700
      'attributes',
1701
      'display',
1702
      'url',
1703
      'title',
1704
      'title_value',
1705
      'enable_tokens',
1706
      'validate_url',
1707
    ) as $setting) {
1610 1708
      unset($field_value['settings'][$setting]);
1611 1709
    }
1612 1710
  }
......
1621 1719
  if ($field_value['type'] == 'link') {
1622 1720
    // Grab settings that were previously on the field.
1623 1721
    foreach (array(
1624
               'attributes',
1625
               'display',
1626
               'url',
1627
               'title',
1628
               'title_value',
1629
               'enable_tokens',
1630
               'validate_url',
1631
             ) as $setting) {
1722
      'attributes',
1723
      'display',
1724
      'url',
1725
      'title',
1726
      'title_value',
1727
      'enable_tokens',
1728
      'validate_url',
1729
    ) as $setting) {
1632 1730
      if (isset($field_value['settings'][$setting])) {
1633 1731
        $instance_value['settings'][$setting] = $field_value['settings'][$setting];
1634 1732
      }
......
1677 1775
  $property['auto creation'] = 'link_field_item_create';
1678 1776

  
1679 1777
  $property['property info'] = link_field_item_property_info();
1680
  $property['property info']['url']['required'] = !$instance['settings']['url'];
1681
  $property['property info']['title']['required'] = ($instance['settings']['title'] == 'required');
1778
  $property['property info']['url']['required'] = $instance['required'] && !$instance['settings']['url'];
1779
  $property['property info']['title']['required'] = $instance['required'] && ($instance['settings']['title'] == 'required');
1682 1780
  if ($instance['settings']['title'] == 'none') {
1683 1781
    unset($property['property info']['title']);
1684 1782
  }

Formats disponibles : Unified diff