Projet

Général

Profil

Révision 7fe061e8

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

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/link/link.module
207 207
  $form['attributes']['class'] = array(
208 208
    '#type' => 'textfield',
209 209
    '#title' => t('Additional CSS Class'),
210
    '#description' => t('When output, this link will have this class attribute. Multiple classes should be separated by spaces.'),
210
    '#description' => t('When output, this link will have this class attribute. Multiple classes should be separated by spaces. Only alphanumeric characters and hyphens are allowed'),
211 211
    '#default_value' => empty($instance['settings']['attributes']['class']) ? '' : $instance['settings']['attributes']['class'],
212 212
  );
213 213
  $form['attributes']['configurable_title'] = array(
......
291 291
 */
292 292
function link_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
293 293
  foreach ($items as $delta => $value) {
294
    _link_process($items[$delta], $delta, $field, $entity);
294
    _link_process($items[$delta], $delta, $field, $entity, $instance);
295 295
  }
296 296
}
297 297

  
......
300 300
 */
301 301
function link_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
302 302
  foreach ($items as $delta => $value) {
303
    _link_process($items[$delta], $delta, $field, $entity);
303
    _link_process($items[$delta], $delta, $field, $entity, $instance);
304 304
  }
305 305
}
306 306

  
......
371 371

  
372 372
/**
373 373
 * Prepares the item attributes and url for storage.
374
 *
375
 * @param $item
376
 *    Link field values.
377
 *
378
 * @param $delta
379
 *    The sequence number for current values.
380
 *
381
 * @param $field
382
 *    The field structure array.
383
 *
384
 * @param $entity
385
 *    Entity object.
386
 *
387
 * @param $instance
388
 *    The instance structure for $field on $entity's bundle.
389
 *
374 390
 */
375
function _link_process(&$item, $delta, $field, $entity) {
391
function _link_process(&$item, $delta, $field, $entity, $instance) {
376 392
  // Trim whitespace from URL.
377 393
  if (!empty($item['url'])) {
378 394
    $item['url'] = trim($item['url']);
......
391 407

  
392 408
  // Don't save an invalid default value (e.g. 'http://').
393 409
  if ((isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url']) && is_object($entity)) {
394
    if (!link_validate_url($item['url'])) {
410
    $langcode = !empty($entity) ? field_language($instance['entity_type'], $entity, $instance['field_name']) : LANGUAGE_NONE;
411
    if (!link_validate_url($item['url'], $langcode)) {
395 412
      unset($item['url']);
396 413
    }
397 414
  }
......
403 420
function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) {
404 421
  if ($item['url'] && !(isset($instance['default_value'][$delta]['url']) && $item['url'] === $instance['default_value'][$delta]['url'] && !$instance['required'])) {
405 422
    // Validate the link.
406
    if (link_validate_url(trim($item['url'])) == FALSE) {
423
    if (!link_validate_url(trim($item['url']), $langcode)) {
407 424
      $errors[$field['field_name']][$langcode][$delta][] = array(
408 425
        'error' => 'link_required',
409 426
        'message' => t('The value %value provided for %field is not a valid URL.', array(
......
474 491
    $entity_type == 'taxonomy_term' || $entity_type == 'taxonomy_vocabulary' ? str_replace('taxonomy_', '', $entity_type) : $entity_type
475 492
  );
476 493
  if (isset($instance['settings']['enable_tokens']) && $instance['settings']['enable_tokens']) {
477
    global $user;
478
    // Load the entity if necessary for entities in views.
479
    if (isset($entity->{$property_id})) {
480
      $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
481
      $entity_loaded = array_pop($entity_loaded);
482
    }
483
    else {
484
      $entity_loaded = $entity;
494
    $text_tokens = token_scan($item['url']);
495
    if (!empty($text_tokens)) {
496
      // Load the entity if necessary for entities in views.
497
      if (isset($entity->{$property_id})) {
498
        $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
499
        $entity_loaded = array_pop($entity_loaded);
500
      }
501
      else {
502
        $entity_loaded = $entity;
503
      }
504
      $item['url'] = token_replace($item['url'], array($entity_token_type => $entity_loaded));
485 505
    }
486
    $item['url'] = token_replace($item['url'], array($entity_token_type => $entity_loaded));
487 506
  }
488 507

  
489
  $type = link_validate_url($item['url']);
508
  $type = link_url_type($item['url']);
490 509
  // If the type of the URL cannot be determined and URL validation is disabled,
491 510
  // then assume LINK_EXTERNAL for later processing.
492 511
  if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
......
496 515
  $url_parts = _link_parse_url($url);
497 516

  
498 517
  if (!empty($url_parts['url'])) {
499
    $item['url'] = $url_parts['url'];
500
    $item += array(
501
      'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
518
    $item['url'] = url($url_parts['url'],
519
      array('query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
502 520
      'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
503 521
      'absolute' => !empty($instance['settings']['absolute_url']),
504 522
      'html' => TRUE,
523
      )
505 524
    );
506 525
  }
507 526

  
......
541 560

  
542 561
  // Replace title tokens.
543 562
  if ($title && ($instance['settings']['title'] == 'value' || $instance['settings']['enable_tokens'])) {
544
    // Load the entity if necessary for entities in views.
545
    if (isset($entity->{$property_id})) {
546
      $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
547
      $entity_loaded = array_pop($entity_loaded);
548
    }
549
    else {
550
      $entity_loaded = $entity;
563
    $text_tokens = token_scan($title);
564
    if (!empty($text_tokens)) {
565
      // Load the entity if necessary for entities in views.
566
      if (isset($entity->{$property_id})) {
567
        $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
568
        $entity_loaded = array_pop($entity_loaded);
569
      }
570
      else {
571
        $entity_loaded = $entity;
572
      }
573
      $title = token_replace($title, array($entity_token_type => $entity_loaded));
551 574
    }
552
    $title = token_replace($title, array($entity_token_type => $entity_loaded));
553 575
    $title = filter_xss($title, array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
554 576
    $item['html'] = TRUE;
555 577
  }
......
599 621

  
600 622
  // Handle "title" link attribute.
601 623
  if (!empty($item['attributes']['title']) && module_exists('token')) {
602
    // Load the entity (necessary for entities in views).
603
    if (isset($entity->{$property_id})) {
604
      $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
605
      $entity_loaded = array_pop($entity_loaded);
606
    }
607
    else {
608
      $entity_loaded = $entity;
624
    $text_tokens = token_scan($item['attributes']['title']);
625
      if (!empty($text_tokens)) {
626
      // Load the entity (necessary for entities in views).
627
      if (isset($entity->{$property_id})) {
628
        $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
629
        $entity_loaded = array_pop($entity_loaded);
630
      }
631
      else {
632
        $entity_loaded = $entity;
633
      }
634
      $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
609 635
    }
610
    $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
611 636
    $item['attributes']['title'] = filter_xss($item['attributes']['title'], array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
612 637
  }
613 638
  // Handle attribute classes.
614 639
  if (!empty($item['attributes']['class'])) {
615 640
    $classes = explode(' ', $item['attributes']['class']);
616 641
    foreach ($classes as &$class) {
617
      $class = drupal_html_class($class);
642
      $class = drupal_clean_css_identifier($class);
618 643
    }
619 644
    $item['attributes']['class'] = implode(' ', $classes);
620 645
  }
......
660 685
 * Replaces the PHP parse_str() function.
661 686
 *
662 687
 * Because parse_str replaces the following characters in query parameters name
663
 * in order to maintain compability with deprecated register_globals directive:
688
 * in order to maintain compatibility with deprecated register_globals directive:
664 689
 *
665 690
 *   - chr(32) ( ) (space)
666 691
 *   - chr(46) (.) (dot)
......
700 725
    'link_formatter_link_plain' => array(
701 726
      'variables' => array('element' => NULL, 'field' => NULL),
702 727
    ),
728
    'link_formatter_link_host' => array(
729
      'variables' => array('element' => NULL),
730
    ),
703 731
    'link_formatter_link_absolute' => array(
704 732
      'variables' => array('element' => NULL, 'field' => NULL),
705 733
    ),
......
859 887
    );
860 888
  }
861 889

  
862
  // If the title field is avaliable or there are field accepts multiple values
890
  // If the title field is available or there are field accepts multiple values
863 891
  // then allow the individual field items display the required asterisk if needed.
864 892
  if (isset($element['title']) || isset($element['_weight'])) {
865 893
    // To prevent an extra required indicator, disable the required flag on the
......
885 913
      'field types' => array('link_field'),
886 914
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
887 915
    ),
916
    'link_host' => array(
917
      'label' => t('Host, as plain text'),
918
      'field types' => array('link_field'),
919
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
920
    ),
888 921
    'link_url' => array(
889 922
      'label' => t('URL, as link'),
890 923
      'field types' => array('link_field'),
......
1015 1048
  return empty($vars['element']['url']) ? check_plain($vars['element']['title']) : url($vars['element']['url'], $link_options);
1016 1049
}
1017 1050

  
1051
/**
1052
 * Theme function for 'host' text field formatter.
1053
 */
1054
function theme_link_formatter_link_host($vars) {
1055
  $host = @parse_url($vars['element']['url']);
1056
  return isset($host['host']) ? check_plain($host['host']) : '';
1057
}
1058

  
1018 1059
/**
1019 1060
 * Formats a link as an absolute URL.
1020 1061
 */
......
1142 1183

  
1143 1184
/**
1144 1185
 * Forms a valid URL if possible from an entered address.
1145
 * 
1186
 *
1146 1187
 * Trims whitespace and automatically adds an http:// to addresses without a
1147 1188
 * protocol specified
1148 1189
 *
......
1153 1194
 */
1154 1195
function link_cleanup_url($url, $protocol = 'http') {
1155 1196
  $url = trim($url);
1156
  $type = link_validate_url($url);
1197
  $type = link_url_type($url);
1157 1198

  
1158 1199
  if ($type === LINK_EXTERNAL) {
1159 1200
    // Check if there is no protocol specified.
......
1173 1214

  
1174 1215
/**
1175 1216
 * Validates a URL.
1176
 * 
1217
 *
1218
 * @param $text
1219
 *   Url to be validated.
1220
 *
1221
 * @param $langcode
1222
 *   An optional language code to look up the path in.
1223
 *
1224
 * @return boolean
1225
 *   True if a valid link, FALSE otherwise.
1226
 */
1227
function link_validate_url($text, $langcode = NULL) {
1228
  $text = link_cleanup_url($text);
1229
  $type = link_url_type($text);
1230

  
1231
  if ($type && ($type == LINK_INTERNAL || $type == LINK_EXTERNAL)) {
1232
    $flag = valid_url($text, TRUE);
1233
    if (!$flag) {
1234
      $normal_path = drupal_get_normal_path($text, $langcode);
1235
      $parsed_link = parse_url($normal_path, PHP_URL_PATH);
1236
      if ($normal_path != $parsed_link) {
1237
        $normal_path = $parsed_link;
1238
      }
1239
      $flag = drupal_valid_path($normal_path);
1240
    }
1241
    if (!$flag) {
1242
      $flag = file_exists($normal_path);
1243
    }
1244
    if (!$flag) {
1245
      $uri = file_build_uri($normal_path);
1246
      $flag = file_exists($uri);
1247
    }
1248
  }
1249
  else {
1250
    $flag = (bool) $type;
1251
  }
1252

  
1253
  return $flag;
1254
}
1255

  
1256
/**
1257
 * Type check a URL.
1258
 *
1177 1259
 * Accepts all URLs following RFC 1738 standard for URL formation and all e-mail
1178 1260
 * addresses following the RFC 2368 standard for mailto address formation.
1179 1261
 *
1180 1262
 * @param string $text
1181
 *   Url to be validated.
1263
 *   Url to be checked.
1182 1264
 * 
1183 1265
 * @return mixed
1184 1266
 *   Returns boolean FALSE if the URL is not valid. On success, returns one of
1185 1267
 *   the LINK_(linktype) constants.
1186 1268
 */
1187
function link_validate_url($text) {
1269
function link_url_type($text) {
1188 1270
  // @TODO Complete letters.
1189 1271
  $LINK_ICHARS_DOMAIN = (string) html_entity_decode(implode("", array(
1190 1272
    "æ", // æ

Formats disponibles : Unified diff