Projet

Général

Profil

Révision bad4e148

Ajouté par Assos Assos il y a environ 3 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/link/link.module
18 18
define('LINK_TARGET_NEW_WINDOW', '_blank');
19 19
define('LINK_TARGET_TOP', '_top');
20 20
define('LINK_TARGET_USER', 'user');
21
define('LINK_HTTP_PROTOCOL', 'http');
22
define('LINK_HTTPS_PROTOCOL', 'https');
21 23

  
22 24
/**
23 25
 * Maximum URLs length - needs to match value in link.install.
......
67 69
        'title_label_use_field_label' => FALSE,
68 70
        'title_maxlength' => 128,
69 71
        'enable_tokens' => 1,
72
        'convert_aliases' => 0,
70 73
        'display' => array(
71 74
          'url_cutoff' => 80,
72 75
        ),
......
187 190
    );
188 191
  }
189 192

  
193
  $form['convert_aliases'] = array(
194
    '#type' => 'checkbox',
195
    '#title' => t('Convert local aliases'),
196
    '#default_value' => isset($instance['settings']['convert_aliases']) ? $instance['settings']['convert_aliases'] : '',
197
    '#description' => t('If checked, a path alias is converted to the internal system path, the same way as when saving menu links.'),
198
  );
199

  
190 200
  $form['display'] = array(
191 201
    '#tree' => TRUE,
192 202
  );
......
307 317
  foreach ($entities as $id => $entity) {
308 318
    foreach ($items[$id] as $delta => $item) {
309 319
      $items[$id][$delta]['attributes'] = _link_load($field, $item, $instances[$id]);
310
      $items[$id][$delta]['original_title'] = $item['title'];
320
      $items[$id][$delta]['original_title'] = isset($item['title']) ? $item['title'] : NULL;
311 321
      $items[$id][$delta]['original_url'] = $item['url'];
312 322
    }
313 323
  }
......
341 351
      'error_element' => array('url' => FALSE, 'title' => TRUE),
342 352
    );
343 353
  }
354

  
355
  // Specific logic for when using the 'select' option on the title field.
356
  if ($instance['settings']['title'] == 'select') {
357
    // The title is required.
358
    if (!empty($item['title']) && !empty($item['url'])) {
359
      $errors[$field['field_name']][$langcode][$delta][] = array(
360
        'error' => 'link_required',
361
        'message' => t('The title field is required when a URL is provided.'),
362
        'error_element' => array('url' => FALSE, 'title' => TRUE),
363
      );
364
    }
365
  }
344 366
}
345 367

  
346 368
/**
......
449 471
    $item['url'] = trim($item['url']);
450 472
  }
451 473

  
474
  // Optionally convert aliases to the system path.
475
  if (!empty($instance['settings']['convert_aliases'])) {
476
    global $base_url;
477

  
478
    // Check if either the site's absolute URL or the relative base URL are at
479
    // the start of the URL, if so remove them.
480
    $base_paths = array(
481
      $base_url . base_path(),
482
      base_path(),
483
    );
484

  
485
    // Work out the correct base_path to use based on the HTTPS settings.
486
    if (isset($GLOBALS['base_secure_url'])) {
487
      $base_paths[] = $GLOBALS['base_secure_url'] . base_path();
488
    }
489
    if (isset($GLOBALS['base_insecure_url'])) {
490
      $base_paths[] = $GLOBALS['base_insecure_url'] . base_path();
491
    }
492

  
493
    // Add any additional paths.
494
    if ($extra_paths = variable_get('link_base_urls', array())) {
495
      // Create versions with and without the base path.
496
      foreach ($extra_paths as $extra_path) {
497
        $base_paths[] = $extra_path;
498
        $base_paths[] = $extra_path . base_path();
499
      }
500
    }
501

  
502
    $paths_to_test = array(
503
      $item['url'],
504
    );
505

  
506
    foreach ($base_paths as $path) {
507
      // Verify the path is at the beginning of the URL string.
508
      if (strpos($item['url'], $path) === 0) {
509
        $strlen = drupal_strlen($path);
510
        $paths_to_test[] = drupal_substr($item['url'], $strlen);
511
      }
512
    }
513

  
514
    // Check each of the paths to see if one of them is a system path.
515
    foreach (array_unique($paths_to_test) as $path) {
516
      $language = NULL;
517

  
518
      // If we have locale enabled attempt to remove the language prefix first.
519
      if (module_exists('locale')) {
520
        require_once DRUPAL_ROOT . '/includes/language.inc';
521
        list($language, $path) = language_url_split_prefix($path, language_list());
522
      }
523

  
524
      // Attempt to get a system path.
525
      $normal_path = drupal_get_normal_path($path, $language);
526

  
527
      // If we get back a different path it means Drupal found a system path we
528
      // can use.
529
      if ($normal_path != $path) {
530
        $item['url'] = $normal_path;
531
        break;
532
      }
533
    }
534
  }
535

  
452 536
  // If no attributes are set then make sure $item['attributes'] is an empty
453 537
  // array, so $field['attributes'] can override it.
454 538
  if (empty($item['attributes'])) {
......
516 600
      'error_element' => array('url' => FALSE, 'title' => TRUE),
517 601
    );
518 602
  }
603

  
604
  // Specific logic for when using the 'select' option on the title field.
605
  if ($instance['settings']['title'] == 'select') {
606
    // The title is required.
607
    if (!empty($item['title']) && !empty($item['url'])) {
608
      $errors[$field['field_name']][$langcode][$delta][] = array(
609
        'error' => 'link_required',
610
        'message' => t('The title field is required when a URL is provided.'),
611
        'error_element' => array('url' => FALSE, 'title' => TRUE),
612
      );
613
    }
614
  }
519 615
}
520 616

  
521 617
/**
522 618
 * Clean up user-entered values for a link field according to field settings.
523 619
 *
620
 * Note: this cannot be properly unit tested as it checks for certain entity
621
 * values.
622
 *
623
 * @todo Rewrite so that the logic can be unit tested.
624
 *
524 625
 * @param array $item
525 626
 *   A single link item, usually containing url, title, and attributes.
526 627
 * @param int $delta
......
557 658
  $entity_type = $instance['entity_type'];
558 659
  $entity_info = entity_get_info($entity_type);
559 660
  $property_id = $entity_info['entity keys']['id'];
560
  $entity_token_type = isset($entity_info['token type']) ? $entity_info['token type'] : (
561
  $entity_type == 'taxonomy_term' || $entity_type == 'taxonomy_vocabulary' ? str_replace('taxonomy_', '', $entity_type) : $entity_type
562
  );
661
  if (isset($entity_info['token type'])) {
662
    $entity_token_type = $entity_info['token type'];
663
  }
664
  elseif ($entity_type == 'taxonomy_term' || $entity_type == 'taxonomy_vocabulary') {
665
    $entity_token_type = str_replace('taxonomy_', '', $entity_type);
666
  }
667
  else {
668
    $entity_token_type = $entity_type;
669
  }
563 670
  if (isset($instance['settings']['enable_tokens']) && $instance['settings']['enable_tokens']) {
564 671
    $text_tokens = token_scan($item['url']);
565 672
    if (!empty($text_tokens)) {
......
582 689
    $type = LINK_EXTERNAL;
583 690
  }
584 691
  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'];
692
    // Treat fragment or query-only links as external.
693
    if (!empty($instance['settings']['absolute_url'])) {
694
      $item['url'] = $_GET['q'] . $item['url'];
695
    }
696
    else {
697
      $item['external'] = TRUE;
698
    }
587 699
  }
588
  $url = link_cleanup_url($item['url']);
700
  $url = link_cleanup_url($item['url'], variable_get('link_default_protocol', LINK_HTTP_PROTOCOL));
589 701
  $url_parts = _link_parse_url($url);
590 702

  
591 703
  if (!empty($url_parts['url'])) {
......
603 715
    $display_url = str_replace('mailto:', '', $url);
604 716
  }
605 717
  elseif ($type === LINK_EXTERNAL) {
606
    $display_url = $item['url'];
718
    $display_url = url($url_parts['url'],
719
      array(
720
        'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
721
        'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
722
        'absolute' => TRUE,
723
      )
724
    );
607 725
  }
608 726
  elseif ($type == LINK_TEL) {
609 727
    $display_url = str_replace('tel:', '', $url);
......
828 946
  return $query_array;
829 947
}
830 948

  
949
/**
950
 * Implements hook_menu().
951
 */
952
function link_menu() {
953
  $items['admin/config/content/link'] = array(
954
    'title' => 'Link settings',
955
    'description' => 'Settings for the link module.',
956
    'page callback' => 'drupal_get_form',
957
    'page arguments' => array('link_admin_settings'),
958
    'access arguments' => array('access administration pages'),
959
    'file' => 'link.admin.inc',
960
  );
961
  return $items;
962
}
963

  
831 964
/**
832 965
 * Implements hook_theme().
833 966
 */
......
1001 1134
      '#description' => t('Select the a title for this link.'),
1002 1135
      '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
1003 1136
      '#options' => $options,
1137
      '#empty_value' => '',
1004 1138
    );
1005 1139
  }
1006 1140

  
......
1148 1282
 * Implements hook_field_formatter_settings_summary().
1149 1283
 */
1150 1284
function link_field_formatter_settings_summary($field, $instance, $view_mode) {
1151

  
1152 1285
  $display = $instance['display'][$view_mode];
1153 1286

  
1154 1287
  if ($display['type'] == 'link_domain') {
......
1239 1372
 * Formats a link as an absolute URL.
1240 1373
 */
1241 1374
function theme_link_formatter_link_absolute($vars) {
1242
  $absolute = array('absolute' => TRUE);
1243
  return empty($vars['element']['url']) ? '' : url($vars['element']['url'], $absolute + $vars['element']);
1375
  // If no URL value is present there's no point in continuing.
1376
  if (empty($vars['element']['url'])) {
1377
    return '';
1378
  }
1379

  
1380
  // Hardcode the 'absolute' argument.
1381
  $vars['element']['absolute'] = TRUE;
1382
  return url($vars['element']['url'], $vars['element']);
1244 1383
}
1245 1384

  
1246 1385
/**
1247 1386
 * Formats a link using the URL's domain for it's link text.
1248 1387
 */
1249 1388
function theme_link_formatter_link_domain($vars) {
1389
  // If no URL value is present there's no point in continuing.
1390
  if (empty($vars['element']['url'])) {
1391
    return '';
1392
  }
1393

  
1250 1394
  $link_options = $vars['element'];
1251 1395
  unset($link_options['title']);
1252 1396
  unset($link_options['url']);
......
1254 1398
  if (!empty($vars['display']['settings']['strip_www'])) {
1255 1399
    $domain = str_replace('www.', '', $domain);
1256 1400
  }
1257
  return $vars['element']['url'] ? l($domain, $vars['element']['url'], $link_options) : '';
1401
  return l($domain, $vars['element']['url'], $link_options);
1258 1402
}
1259 1403

  
1260 1404
/**
1261 1405
 * Formats a link without the http:// or https://.
1262 1406
 */
1263 1407
function theme_link_formatter_link_no_protocol($vars) {
1408
  // If no URL value is present there's no point in continuing.
1409
  if (empty($vars['element']['url'])) {
1410
    return '';
1411
  }
1412

  
1264 1413
  $link_options = $vars['element'];
1265 1414
  unset($link_options['title']);
1266 1415
  unset($link_options['url']);
......
1270 1419
  $replace = '';
1271 1420
  $display_url = preg_replace($search, $replace, $vars['element']['url'], 1);
1272 1421

  
1273
  return $vars['element']['url'] ? l($display_url, $vars['element']['url'], $link_options) : '';
1422
  return l($display_url, $vars['element']['url'], $link_options);
1274 1423
}
1275 1424

  
1276 1425
/**
1277 1426
 * Formats a link's title as plain text.
1278 1427
 */
1279 1428
function theme_link_formatter_link_title_plain($vars) {
1280
  return empty($vars['element']['title']) ? '' : check_plain(decode_entities($vars['element']['title']));
1429
  // If no title value is present there's no point in continuing.
1430
  if (empty($vars['element']['title'])) {
1431
    return '';
1432
  }
1433

  
1434
  return check_plain(decode_entities($vars['element']['title']));
1281 1435
}
1282 1436

  
1283 1437
/**
1284 1438
 * Formats a link using an alternate display URL for its link text.
1285 1439
 */
1286 1440
function theme_link_formatter_link_url($vars) {
1441
  // If no URL value is present there's no point in continuing.
1442
  if (empty($vars['element']['url'])) {
1443
    return '';
1444
  }
1445

  
1287 1446
  $link_options = $vars['element'];
1288 1447
  if (isset($link_options['attributes']['class'])) {
1289 1448
    $link_options['attributes']['class'] = array($link_options['attributes']['class']);
1290 1449
  }
1291 1450
  unset($link_options['title']);
1292 1451
  unset($link_options['url']);
1293
  return $vars['element']['url'] ? l($vars['element']['display_url'], $vars['element']['url'], $link_options) : '';
1452
  return l($vars['element']['display_url'], $vars['element']['url'], $link_options);
1294 1453
}
1295 1454

  
1296 1455
/**
1297 1456
 * Formats a link using "Link" as the link text.
1298 1457
 */
1299 1458
function theme_link_formatter_link_short($vars) {
1459
  // If no URL value is present there's no point in continuing.
1460
  if (empty($vars['element']['url'])) {
1461
    return '';
1462
  }
1463

  
1300 1464
  $link_options = $vars['element'];
1301 1465
  unset($link_options['title']);
1302 1466
  unset($link_options['url']);
1303
  return $vars['element']['url'] ? l(t('Link'), $vars['element']['url'], $link_options) : '';
1467
  return l(t('Link'), $vars['element']['url'], $link_options);
1304 1468
}
1305 1469

  
1306 1470
/**
1307 1471
 * Formats a link using the field's label as link text.
1308 1472
 */
1309 1473
function theme_link_formatter_link_label($vars) {
1474
  // If no URL value is present there's no point in continuing.
1475
  if (empty($vars['element']['url'])) {
1476
    return '';
1477
  }
1478

  
1310 1479
  $link_options = $vars['element'];
1311 1480
  unset($link_options['title']);
1312 1481
  unset($link_options['url']);
......
1315 1484
    $i18n_string_name = "field:{$vars['field']['field_name']}:{$vars['field']['bundle']}:label";
1316 1485
    $label = i18n_string_translate($i18n_string_name, $label);
1317 1486
  }
1318
  return $vars['element']['url'] ? l($label, $vars['element']['url'], $link_options) : '';
1487
  return l($label, $vars['element']['url'], $link_options);
1319 1488
}
1320 1489

  
1321 1490
/**
......
1329 1498
  unset($link_options['url']);
1330 1499
  $title = empty($vars['element']['title']) ? '' : check_plain($vars['element']['title']);
1331 1500

  
1332
  // @TODO static html markup looks not very elegant
1333
  // needs smarter output solution and an optional title/url seperator
1501
  // @todo Static html markup looks not very elegant, needs smarter output
1502
  // solution and an optional title/URL seperator.
1334 1503
  $url_parts = _link_parse_url($vars['element']['url']);
1335 1504
  $output = '';
1336 1505
  $output .= '<div class="link-item ' . $class . '">';
......
1344 1513

  
1345 1514
/**
1346 1515
 * Implements hook_token_list().
1347
 *
1348
 * @TODO: hook_token_list no longer exists - this should change to
1349
 *   hook_token_info().
1350 1516
 */
1351 1517
function link_token_list($type = 'all') {
1518
  // @todo hook_token_list() no longer exists, this should be rewritten as
1519
  // hook_token_info().
1352 1520
  if ($type === 'field' || $type === 'all') {
1353 1521
    $tokens = array();
1354 1522
    $tokens['link']['url'] = t("Link URL");
......
1360 1528

  
1361 1529
/**
1362 1530
 * Implements hook_token_values().
1363
 *
1364
 * @TODO: hook_token_values no longer exists - this should change to
1365
 *   hook_tokens().
1366 1531
 */
1367 1532
function link_token_values($type, $object = NULL) {
1533
  // @todo hook_token_values() no longer exists, this should be rewritten as
1534
  // hook_tokens().
1368 1535
  if ($type === 'field') {
1369 1536
    $item = $object[0];
1370 1537

  
......
1398 1565
 *   The protocol to be prepended to the url if one is not specified.
1399 1566
 */
1400 1567
function link_cleanup_url($url, $protocol = 'http') {
1568
  try {
1569
    link_ensure_valid_default_protocol();
1570
  }
1571
  catch (Exception $e) {
1572
    watchdog('link', $e->getMessage(), array(), WATCHDOG_ERROR);
1573
  }
1401 1574
  $url = trim($url);
1402 1575
  $type = link_url_type($url);
1403 1576

  
......
1418 1591
  return $url;
1419 1592
}
1420 1593

  
1594
/**
1595
 * Validate that the protocol is either HTTP or HTTPS.
1596
 *
1597
 * @throws Exception
1598
 */
1599
function link_ensure_valid_default_protocol() {
1600
  $protocol = variable_get('link_default_protocol', LINK_HTTP_PROTOCOL);
1601
  if ($protocol !== LINK_HTTP_PROTOCOL && $protocol !== LINK_HTTPS_PROTOCOL) {
1602
    variable_set('link_default_protocol', LINK_HTTP_PROTOCOL);
1603
    throw new Exception(t('Protocol was set to !protocol but must be !HTTP or !HTTPS. Set to default HTTP.', array(
1604
      '!protocol' => $protocol,
1605
      '!HTTP' => LINK_HTTP_PROTOCOL,
1606
      '!HTTPS' => LINK_HTTPS_PROTOCOL,
1607
    )));
1608
  }
1609
}
1610

  
1421 1611
/**
1422 1612
 * Validates a URL.
1423 1613
 *
......
1430 1620
 *   True if a valid link, FALSE otherwise.
1431 1621
 */
1432 1622
function link_validate_url($text, $langcode = NULL) {
1433

  
1434 1623
  $text = _link_clean_relative($text);
1435 1624
  $text = link_cleanup_url($text);
1436 1625
  $type = link_url_type($text);
......
1499 1688
 *   the LINK_(linktype) constants.
1500 1689
 */
1501 1690
function link_url_type($text) {
1502
  // @TODO Complete letters.
1691
  // @todo Complete letters.
1503 1692
  // @codingStandardsIgnoreStart
1504 1693
  $link_ichars_domain = (string) html_entity_decode(implode("", array(
1505 1694
    "&#x00BF;", // ¿
......
1574 1763
  // @codingStandardsIgnoreEnd
1575 1764

  
1576 1765
  $link_ichars = $link_ichars_domain . (string) html_entity_decode(implode("", array(
1577
      // ß.
1766
    // ß.
1578 1767
    "&#x00DF;",
1579 1768
  )), ENT_QUOTES, 'UTF-8');
1580 1769
  $allowed_protocols = variable_get('filter_allowed_protocols', array(
......
1598 1787
  // captured.
1599 1788
  $protocol = '((?:' . implode("|", $allowed_protocols) . '):\/\/)';
1600 1789
  $authentication = "(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=" . $link_ichars . "]|%[0-9a-f]{2})+(?::(?:[\w" . $link_ichars . "\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})*)?)?@)";
1601
  $domain = '(?:(?:[a-z0-9' . $link_ichars_domain . ']([a-z0-9' . $link_ichars_domain . '\-_\[\]])*)(\.(([a-z0-9' . $link_ichars_domain . '\-_\[\]])+\.)*(' . $link_domains . '|[a-z]{2}))?)';
1790
  $domain = '(?:(?:[a-zA-Z0-9' . $link_ichars_domain . ']([a-zA-Z0-9' . $link_ichars_domain . '\-_\[\]])*)(\.(([a-zA-Z0-9' . $link_ichars_domain . '\-_\[\]])+\.)*(' . $link_domains . '|[a-z]{2}))?)';
1602 1791
  $ipv4 = '(?:[0-9]{1,3}(\.[0-9]{1,3}){3})';
1603 1792
  $ipv6 = '(?:[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
1604 1793
  $port = '(?::([0-9]{1,5}))';
......
1805 1994
    'type' => 'text',
1806 1995
    'label' => t('The URL of the link.'),
1807 1996
    'setter callback' => 'entity_property_verbatim_set',
1997
    'getter callback' => 'link_url_property_get',
1808 1998
  );
1809 1999
  $properties['attributes'] = array(
1810 2000
    'type' => 'struct',
......
1820 2010
  return $properties;
1821 2011
}
1822 2012

  
2013
/**
2014
 * Callback for getting the URL property.
2015
 *
2016
 * @see entity_metadata_entity_get_properties()
2017
 */
2018
function link_url_property_get($data, array $options, $name, $type, $info) {
2019
  $url = entity_property_verbatim_get($data, $options, $name, $type, $info);
2020

  
2021
  return url($url, $data + $options);
2022
}
2023

  
1823 2024
/**
1824 2025
 * Entity property info getter callback for link attributes.
1825 2026
 */

Formats disponibles : Unified diff