Projet

Général

Profil

Révision c8740e19

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

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/link/link.module
10 10
define('LINK_FRONT', 'front');
11 11
define('LINK_EMAIL', 'email');
12 12
define('LINK_NEWS', 'news');
13
define('LINK_DOMAINS', 'aero|arpa|asia|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local|xxx');
13
define('LINK_DOMAINS', 'aero|arpa|asia|biz|build|com|cat|ceo|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|post|pro|tel|travel|mobi|local|xxx');
14 14

  
15 15
define('LINK_TARGET_DEFAULT', 'default');
16 16
define('LINK_TARGET_NEW_WINDOW', '_blank');
......
46 46
        'url' => 0,
47 47
        'title' => 'optional',
48 48
        'title_value' => '',
49
        'title_label_use_field_label' => FALSE,
49 50
        'title_maxlength' => 128,
50 51
        'enable_tokens' => 1,
51 52
        'display' => array(
52 53
          'url_cutoff' => 80,
53 54
        ),
54 55
        'validate_url' => 1,
56
        'absolute_url' => 1,
55 57
      ),
56 58
      'default_widget' => 'link_field',
57 59
      'default_formatter' => 'link_default',
......
70 72
    '#element_validate' => array('link_field_settings_form_validate'),
71 73
  );
72 74

  
75
  $form['absolute_url'] = array(
76
    '#type' => 'checkbox',
77
    '#title' => t('Absolute URL'),
78
    '#default_value' => isset($instance['settings']['absolute_url']) && ($instance['settings']['absolute_url'] !== '') ? $instance['settings']['absolute_url'] : TRUE,
79
    '#description' => t('If checked, the URL will always render as an absolute URL.'),
80
  );
81

  
73 82
  $form['validate_url'] = array(
74 83
    '#type' => 'checkbox',
75 84
    '#title' => t('Validate URL'),
......
107 116
    '#description' => t('This title will always be used if “Static Title” is selected above.'),
108 117
  );
109 118

  
119
  $form['title_label_use_field_label'] = array(
120
    '#type' => 'checkbox',
121
    '#title' => t('Use field label as the label for the title field'),
122
    '#default_value' => isset($instance['settings']['title_label_use_field_label']) ? $instance['settings']['title_label_use_field_label'] : FALSE,
123
    '#description' => t('If this is checked the field label will be hidden.'),
124
  );
125

  
110 126
  $form['title_maxlength'] = array(
111 127
    '#type' => 'textfield',
112 128
    '#title' => t('Max length of title field'),
......
117 133
  );
118 134

  
119 135
  if (module_exists('token')) {
120
    // Add token module replacements fields
136
    // Add token module replacements fields.
121 137
    $form['enable_tokens'] = array(
122 138
      '#type' => 'checkbox',
123 139
      '#title' => t('Allow user-entered tokens'),
124 140
      '#default_value' => isset($instance['settings']['enable_tokens']) ? $instance['settings']['enable_tokens'] : 1,
125 141
      '#description' => t('Checking will allow users to enter tokens in URLs and Titles on the entity edit form. This does not affect the field settings on this page.'),
126 142
    );
127
    
143

  
128 144
    $entity_info = entity_get_info($instance['entity_type']);
129 145
    $form['tokens_help'] = array(
130 146
      '#theme' => 'token_tree',
......
183 199
    '#description' => t('Turn on/off if rel attribute should be removed automatically, if user given link is internal/external'),
184 200
    '#options' => $rel_remove_options,
185 201
  );
202
  $form['attributes']['configurable_class'] = array(
203
    '#title' => t("Allow the user to enter a custom link class per link"),
204
    '#type' => 'checkbox',
205
    '#default_value' => empty($instance['settings']['attributes']['configurable_class']) ? '' : $instance['settings']['attributes']['configurable_class'],
206
  );
186 207
  $form['attributes']['class'] = array(
187 208
    '#type' => 'textfield',
188 209
    '#title' => t('Additional CSS Class'),
......
351 372
/**
352 373
 * Prepares the item attributes and url for storage.
353 374
 */
354
function _link_process(&$item, $delta = 0, $field, $entity) {
375
function _link_process(&$item, $delta, $field, $entity) {
355 376
  // Trim whitespace from URL.
356
  $item['url'] = trim($item['url']);
377
  if (!empty($item['url'])) {
378
    $item['url'] = trim($item['url']);
379
  }
357 380

  
358 381
  // If no attributes are set then make sure $item['attributes'] is an empty
359 382
  // array, so $field['attributes'] can override it.
......
383 406
    if (link_validate_url(trim($item['url'])) == FALSE) {
384 407
      $errors[$field['field_name']][$langcode][$delta][] = array(
385 408
        'error' => 'link_required',
386
        'message' => t('The value provided for %field is not a valid URL.', array('%field' => $instance['label'])),
409
        'message' => t('The value %value provided for %field is not a valid URL.', array(
410
          '%value' => trim($item['url']),
411
          '%field' => $instance['label'],
412
        )),
387 413
        'error_element' => array('url' => TRUE, 'title' => FALSE),
388 414
      );
389 415
    }
......
405 431
    );
406 432
  }
407 433
  // In a totally bizzaro case, where URLs and titles are optional but the field is required, ensure there is at least one link.
408
  if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && (strlen(trim($item['url'])) !== 0 || strlen(trim($item['title'])) !== 0)) {
434
  if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional'
435
      && (strlen(trim($item['url'])) !== 0 || strlen(trim($item['title'])) !== 0)) {
409 436
    $optional_field_found = TRUE;
410 437
  }
411
  // Require entire field
438
  // Require entire field.
412 439
  if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] == 1 && !$optional_field_found && isset($instance['id'])) {
413 440
    $errors[$field['field_name']][$langcode][$delta][] = array(
414 441
      'error' => 'link_required',
......
421 448
/**
422 449
 * Clean up user-entered values for a link field according to field settings.
423 450
 *
424
 * @param $item
451
 * @param array $item 
425 452
 *   A single link item, usually containing url, title, and attributes.
426
 * @param $delta
453
 * @param int $delta 
427 454
 *   The delta value if this field is one of multiple fields.
428
 * @param $field
455
 * @param array $field 
429 456
 *   The CCK field definition.
430
 * @param $entity
457
 * @param object $entity 
431 458
 *   The entity containing this link.
432 459
 */
433 460
function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
......
435 462
  if (empty($item['url']) && empty($item['title'])) {
436 463
    return;
437 464
  }
465
  if (empty($item['html'])) {
466
    $item['html'] = FALSE;
467
  }
438 468

  
439 469
  // Replace URL tokens.
440 470
  $entity_type = $instance['entity_type'];
......
466 496
  $url_parts = _link_parse_url($url);
467 497

  
468 498
  if (!empty($url_parts['url'])) {
469
    $item['url'] = url($url_parts['url'],
470
      array(
471
        'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
472
        'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
473
        'absolute' => TRUE,
474
        'html' => TRUE,
475
      )
499
    $item['url'] = $url_parts['url'];
500
    $item += array(
501
      'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
502
      'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
503
      'absolute' => !empty($instance['settings']['absolute_url']),
504
      'html' => TRUE,
476 505
    );
477 506
  }
478 507

  
......
485 514
      array(
486 515
        'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
487 516
        'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
488
        'absolute' => TRUE,
517
        'absolute' => !empty($instance['settings']['absolute_url']),
489 518
      )
490 519
    );
491 520
  }
......
510 539
    $title = '';
511 540
  }
512 541

  
513
  // Replace tokens.
542
  // Replace title tokens.
514 543
  if ($title && ($instance['settings']['title'] == 'value' || $instance['settings']['enable_tokens'])) {
515 544
    // Load the entity if necessary for entities in views.
516 545
    if (isset($entity->{$property_id})) {
......
532 561

  
533 562
  // Unserialize attributtes array if it has not been unserialized yet.
534 563
  if (!is_array($item['attributes'])) {
535
    $item['attributes'] = (array)unserialize($item['attributes']);
564
    $item['attributes'] = (array) unserialize($item['attributes']);
536 565
  }
537 566

  
538 567
  // Add default attributes.
......
581 610
    $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
582 611
    $item['attributes']['title'] = filter_xss($item['attributes']['title'], array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
583 612
  }
613
  // Handle attribute classes.
614
  if (!empty($item['attributes']['class'])) {
615
    $classes = explode(' ', $item['attributes']['class']);
616
    foreach ($classes as &$class) {
617
      $class = drupal_html_class($class);
618
    }
619
    $item['attributes']['class'] = implode(' ', $classes);
620
  }
621
  unset($item['attributes']['configurable_class']);
622

  
584 623
  // Remove title attribute if it's equal to link text.
585 624
  if (isset($item['attributes']['title']) && $item['attributes']['title'] == $item['title']) {
586 625
    unset($item['attributes']['title']);
......
597 636
 * @param string $url
598 637
 *   URL to parse.
599 638
 *
600
 * @return Array
639
 * @return array
601 640
 *   Array of url pieces - only 'url', 'query', and 'fragment'.
602 641
 */
603 642
function _link_parse_url($url) {
......
618 657
}
619 658

  
620 659
/**
621
 * Bacause parse_str replaces the following characters in query parameters name
660
 * Replaces the PHP parse_str() function.
661
 *
662
 * Because parse_str replaces the following characters in query parameters name
622 663
 * in order to maintain compability with deprecated register_globals directive:
623 664
 *
624 665
 *   - chr(32) ( ) (space)
......
629 670
 * @param string $query
630 671
 *   Query to parse.
631 672
 *
632
 * @return Array
673
 * @return array
633 674
 *   Array of query parameters.
634 675
 *
635 676
 * @see http://php.net/manual/en/language.variables.external.php#81080
......
639 680

  
640 681
  $pairs = explode('&', $query);
641 682
  foreach ($pairs as $pair) {
642
    $name_value = explode('=', $pair);
683
    $name_value = explode('=', $pair, 2);
643 684
    $name = urldecode($name_value[0]);
644 685
    $value = isset($name_value[1]) ? urldecode($name_value[1]) : NULL;
645 686
    $query_array[$name] = $value;
......
712 753
  if (!empty($element['attributes']['title'])) {
713 754
    $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['title']) . '</div>';
714 755
  }
756
  if (!empty($element['attributes']['class'])) {
757
    $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['class']) . '</div>';
758
  }
759
  $output .= drupal_render_children($element);
715 760
  return $output;
716 761
}
717 762

  
......
759 804
    '#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
760 805
  );
761 806
  if ($settings['title'] !== 'none' && $settings['title'] !== 'value') {
807
    // Figure out the label of the title field.
808
    if (!empty($settings['title_label_use_field_label'])) {
809
      // Use the element label as the title field label.
810
      $title_label = $element['#title'];
811
      // Hide the field label because there is no need for the duplicate labels.
812
      $element['#title_display'] = 'invisible';
813
    }
814
    else {
815
      $title_label = t('Title');
816
    }
817

  
762 818
    $element['title'] = array(
763 819
      '#type' => 'textfield',
764 820
      '#maxlength' => $settings['title_maxlength'],
765
      '#title' => t('Title'),
821
      '#title' => $title_label,
766 822
      '#description' => t('The link title is limited to @maxlength characters maximum.', array('@maxlength' => $settings['title_maxlength'])),
767 823
      '#required' => ($settings['title'] == 'required' && (($element['#delta'] == 0 && $element['#required']) || !empty($element['#value']['url']))) ? TRUE : FALSE,
768 824
      '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
......
793 849
      '#field_suffix' => '"',
794 850
    );
795 851
  }
852
  if (!empty($settings['attributes']['configurable_class']) && $settings['attributes']['configurable_class'] == 1) {
853
    $element['attributes']['class'] = array(
854
      '#type' => 'textfield',
855
      '#title' => t('Custom link class'),
856
      '#default_value' => isset($attributes['class']) ? $attributes['class'] : '',
857
      '#field_prefix' => 'class = "',
858
      '#field_suffix' => '"',
859
    );
860
  }
796 861

  
797 862
  // If the title field is avaliable or there are field accepts multiple values
798 863
  // then allow the individual field items display the required asterisk if needed.
......
922 987
  if (isset($link_options['attributes']['class'])) {
923 988
    $link_options['attributes']['class'] = array($link_options['attributes']['class']);
924 989
  }
925

  
926 990
  // Display a normal link if both title and URL are available.
927 991
  if (!empty($vars['element']['title']) && !empty($vars['element']['url'])) {
928 992
    return l($vars['element']['title'], $vars['element']['url'], $link_options);
929 993
  }
930 994
  // If only a title, display the title.
931 995
  elseif (!empty($vars['element']['title'])) {
932
    return check_plain($vars['element']['title']);
996
    return $link_options['html'] ? $vars['element']['title'] : check_plain($vars['element']['title']);
933 997
  }
934 998
  elseif (!empty($vars['element']['url'])) {
935 999
    return l($vars['element']['title'], $vars['element']['url'], $link_options);
......
952 1016
}
953 1017

  
954 1018
/**
955
 * Formats a link as an absolute URL
1019
 * Formats a link as an absolute URL.
956 1020
 */
957 1021
function theme_link_formatter_link_absolute($vars) {
958 1022
  $absolute = array('absolute' => TRUE);
......
1021 1085
  unset($link_options['url']);
1022 1086
  $title = empty($vars['element']['title']) ? '' : check_plain($vars['element']['title']);
1023 1087

  
1024
  /**
1025
   * @TODO static html markup looks not very elegant
1026
   * needs smarter output solution and an optional title/url seperator
1027
   */
1088
  // @TODO static html markup looks not very elegant
1089
  // needs smarter output solution and an optional title/url seperator
1028 1090
  $url_parts = _link_parse_url($vars['element']['url']);
1029 1091
  $output = '';
1030 1092
  $output .= '<div class="link-item ' . $class . '">';
......
1038 1100

  
1039 1101
/**
1040 1102
 * Implements hook_token_list().
1103
 *
1104
 * @TODO: hook_token_list no longer exists - this should change to hook_token_info().
1041 1105
 */
1042 1106
function link_token_list($type = 'all') {
1043 1107
  if ($type === 'field' || $type === 'all') {
......
1049 1113
  }
1050 1114
}
1051 1115

  
1116
/**
1117
 * Implements hook_token_values().
1118
 *
1119
 * @TODO: hook_token_values no longer exists - this should change to hook_tokens().
1120
 */
1052 1121
function link_token_values($type, $object = NULL) {
1053 1122
  if ($type === 'field') {
1054 1123
    $item = $object[0];
......
1078 1147
 * protocol specified
1079 1148
 *
1080 1149
 * @param string $url
1150
 *   The url entered by the user.
1081 1151
 * @param string $protocol
1082 1152
 *   The protocol to be prepended to the url if one is not specified
1083 1153
 */
......
1108 1178
 * addresses following the RFC 2368 standard for mailto address formation.
1109 1179
 *
1110 1180
 * @param string $text
1181
 *   Url to be validated.
1111 1182
 * 
1112 1183
 * @return mixed
1113 1184
 *   Returns boolean FALSE if the URL is not valid. On success, returns one of
......
1177 1248
  $allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
1178 1249
  $LINK_DOMAINS = _link_domains();
1179 1250

  
1180
  // Starting a parenthesis group with (?: means that it is grouped, but is not captured
1251
  // Starting a parenthesis group with (?: means that it is grouped, but is not captured.
1181 1252
  $protocol = '((?:' . implode("|", $allowed_protocols) . '):\/\/)';
1182 1253
  $authentication = "(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=" . $LINK_ICHARS . "]|%[0-9a-f]{2})+(?::(?:[\w" . $LINK_ICHARS . "\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})*)?)?@)";
1183 1254
  $domain = '(?:(?:[a-z0-9' . $LINK_ICHARS_DOMAIN . ']([a-z0-9' . $LINK_ICHARS_DOMAIN . '\-_\[\]])*)(\.(([a-z0-9' . $LINK_ICHARS_DOMAIN . '\-_\[\]])+\.)*(' . $LINK_DOMAINS . '|[a-z]{2}))?)';
......
1194 1265

  
1195 1266
  $directories = "(?:\/[a-z0-9" . $LINK_ICHARS . "_\-\.~+%=&,$'#!():;*@\[\]]*)*";
1196 1267
  // Yes, four backslashes == a single backslash.
1197
  $query = "(?:\/?\?([?a-z0-9" . $LINK_ICHARS . "+_|\-\.~\/\\\\%=&,$'():;*@\[\]{} ]*))";
1268
  $query = "(?:\/?\?([?a-z0-9" . $LINK_ICHARS . "+_|\-\.~\/\\\\%=&,$'!():;*@\[\]{} ]*))";
1198 1269
  $anchor = "(?:#[a-z0-9" . $LINK_ICHARS . "_\-\.~+%=&,$'():;*@\[\]\/\?]*)";
1199 1270

  
1200 1271
  // The rest of the path for a standard URL.
......
1287 1358
/**
1288 1359
 * Additional callback to adapt the property info of link fields.
1289 1360
 * 
1290
 * @see entity_metadata_field_entity_property_info().
1361
 * @see entity_metadata_field_entity_property_info()
1291 1362
 */
1292 1363
function link_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
1293 1364
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
......
1331 1402
    'label' => t('The URL of the link.'),
1332 1403
    'setter callback' => 'entity_property_verbatim_set',
1333 1404
  );
1405
  $properties['attributes'] = array(
1406
    'type' => 'struct',
1407
    'label' => t('The attributes of the link.'),
1408
    'setter callback' => 'entity_property_verbatim_set',
1409
    'getter callback' => 'link_attribute_property_get',
1410
  );
1334 1411
  return $properties;
1335 1412
}
1336 1413

  
1414
/**
1415
 * Entity property info getter callback for link attributes.
1416
 */
1417
function link_attribute_property_get($data, array $options, $name, $type, $info) {
1418
  return isset($data[$name]) ? array_filter($data[$name]) : array();
1419
}
1420

  
1337 1421
/**
1338 1422
 * Implements hook_field_update_instance().
1339 1423
 */

Formats disponibles : Unified diff