link.module

  1. cis7 sites/all/modules/ulmus/link/link.module
  2. cle7 sites/all/modules/ulmus/link/link.module
  3. ecd7 sites/all/modules/ulmus/link/link.module
  4. elmsmedia7 sites/all/modules/ulmus/link/link.module
  5. harmony7 sites/all/modules/ulmus/link/link.module
  6. icor7 sites/all/modules/ulmus/link/link.module
  7. meedjum_blog7 sites/all/modules/ulmus/link/link.module
  8. mooc7 sites/all/modules/ulmus/link/link.module

Defines simple link field types.

Functions

Namesort descending Description
link_cleanup_url Forms a valid URL if possible from an entered address.
link_content_migrate_field_alter Implements hook_migrate_field_alter().
link_content_migrate_instance_alter Implements hook_migrate_instance_alter().
link_element_info Implements hook_element_info().
link_field_formatter_info Implements hook_field_formatter_info().
link_field_formatter_settings_form Implements hook_field_formatter_settings_form().
link_field_formatter_settings_summary Implements hook_field_formatter_settings_summary().
link_field_formatter_view Implements hook_field_formatter_view().
link_field_info Implements hook_field_info().
link_field_insert Implements hook_field_insert().
link_field_instance_settings_form Implements hook_field_instance_settings_form().
link_field_is_empty Implements hook_field_is_empty().
link_field_item_create Callback for creating a new, empty link field item.
link_field_item_property_info Defines info for the properties of the link-field item data structure.
link_field_load Implements hook_field_load().
link_field_prepare_view Implements hook_field_prepare_view().
link_field_process Processes the link type element before displaying the field.
link_field_property_info_callback Additional callback to adapt the property info of link fields.
link_field_settings_form Implements hook_field_settings_form().
link_field_settings_form_validate #element_validate handler for link_field_instance_settings_form().
link_field_update Implements hook_field_update().
link_field_update_instance Implements hook_field_update_instance().
link_field_validate Implements hook_field_validate().
link_field_widget_error Implements hook_field_widget_error().
link_field_widget_form Implements hook_field_widget_form().
link_field_widget_info Implements hook_field_widget_info().
link_i18n_string_list_field_alter Implements hook_i18n_string_list_TEXTGROUP_alter().
link_theme Implements hook_theme().
link_token_list Implements hook_token_list().
link_token_values
link_validate_url Validates a URL.
link_views_api Implements hook_views_api().
theme_link_field Formats a link field widget.
theme_link_formatter_link_absolute Formats a link as an absolute URL
theme_link_formatter_link_default Formats a link.
theme_link_formatter_link_domain Formats a link using the URL's domain for it's link text.
theme_link_formatter_link_label Formats a link using the field's label as link text.
theme_link_formatter_link_plain Formats a link (or its title) as plain text.
theme_link_formatter_link_separate Formats a link as separate title and URL elements.
theme_link_formatter_link_short Formats a link using "Link" as the link text.
theme_link_formatter_link_title_plain Formats a link's title as plain text.
theme_link_formatter_link_url Formats a link using an alternate display URL for its link text.
_link_default_attributes Returns the default attributes and their values.
_link_domains Returns the list of allowed domains, including domains added by admins via variable_set/$config.
_link_load Unpacks the item attributes for use.
_link_parse_str Bacause parse_str replaces the following characters in query parameters name in order to maintain compability with deprecated register_globals directive:
_link_parse_url Because parse_url doesn't work with relative urls.
_link_process Prepares the item attributes and url for storage.
_link_sanitize Clean up user-entered values for a link field according to field settings.
_link_validate Validates that the link field has been entered properly.

Constants

File

sites/all/modules/ulmus/link/link.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Defines simple link field types.
  5. */
  6. define('LINK_EXTERNAL', 'external');
  7. define('LINK_INTERNAL', 'internal');
  8. define('LINK_FRONT', 'front');
  9. define('LINK_EMAIL', 'email');
  10. define('LINK_NEWS', 'news');
  11. 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');
  12. define('LINK_TARGET_DEFAULT', 'default');
  13. define('LINK_TARGET_NEW_WINDOW', '_blank');
  14. define('LINK_TARGET_TOP', '_top');
  15. define('LINK_TARGET_USER', 'user');
  16. /**
  17. * Maximum URLs length - needs to match value in link.install.
  18. */
  19. define('LINK_URL_MAX_LENGTH', 2048);
  20. /**
  21. * Implements hook_field_info().
  22. */
  23. function link_field_info() {
  24. return array(
  25. 'link_field' => array(
  26. 'label' => t('Link'),
  27. 'description' => t('Store a title, href, and attributes in the database to assemble a link.'),
  28. 'settings' => array(
  29. 'attributes' => _link_default_attributes(),
  30. 'url' => 0,
  31. 'title' => 'optional',
  32. 'title_value' => '',
  33. 'title_maxlength' => 128,
  34. 'enable_tokens' => 1,
  35. 'display' => array(
  36. 'url_cutoff' => 80,
  37. ),
  38. ),
  39. 'instance_settings' => array(
  40. 'attributes' => _link_default_attributes(),
  41. 'url' => 0,
  42. 'title' => 'optional',
  43. 'title_value' => '',
  44. 'title_maxlength' => 128,
  45. 'enable_tokens' => 1,
  46. 'display' => array(
  47. 'url_cutoff' => 80,
  48. ),
  49. 'validate_url' => 1,
  50. ),
  51. 'default_widget' => 'link_field',
  52. 'default_formatter' => 'link_default',
  53. // Support hook_entity_property_info() from contrib "Entity API".
  54. 'property_type' => 'field_item_link',
  55. 'property_callbacks' => array('link_field_property_info_callback'),
  56. ),
  57. );
  58. }
  59. /**
  60. * Implements hook_field_instance_settings_form().
  61. */
  62. function link_field_instance_settings_form($field, $instance) {
  63. $form = array(
  64. '#element_validate' => array('link_field_settings_form_validate'),
  65. );
  66. $form['validate_url'] = array(
  67. '#type' => 'checkbox',
  68. '#title' => t('Validate URL'),
  69. '#default_value' => isset($instance['settings']['validate_url']) && ($instance['settings']['validate_url'] !== '') ? $instance['settings']['validate_url'] : TRUE,
  70. '#description' => t('If checked, the URL field will be verified as a valid URL during validation.'),
  71. );
  72. $form['url'] = array(
  73. '#type' => 'checkbox',
  74. '#title' => t('Optional URL'),
  75. '#default_value' => isset($instance['settings']['url']) ? $instance['settings']['url'] : '',
  76. '#return_value' => 'optional',
  77. '#description' => t('If checked, the URL field is optional and submitting a title alone will be acceptable. If the URL is omitted, the title will be displayed as plain text.'),
  78. );
  79. $title_options = array(
  80. 'optional' => t('Optional Title'),
  81. 'required' => t('Required Title'),
  82. 'value' => t('Static Title'),
  83. 'none' => t('No Title'),
  84. );
  85. $form['title'] = array(
  86. '#type' => 'radios',
  87. '#title' => t('Link Title'),
  88. '#default_value' => isset($instance['settings']['title']) ? $instance['settings']['title'] : 'optional',
  89. '#options' => $title_options,
  90. '#description' => t('If the link title is optional or required, a field will be displayed to the end user. If the link title is static, the link will always use the same title. If <a href="http://drupal.org/project/token">token module</a> is installed, the static title value may use any other entity field as its value. Static and token-based titles may include most inline XHTML tags such as <em>strong</em>, <em>em</em>, <em>img</em>, <em>span</em>, etc.'),
  91. );
  92. $form['title_value'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('Static title'),
  95. '#default_value' => isset($instance['settings']['title_value']) ? $instance['settings']['title_value'] : '',
  96. '#description' => t('This title will always be used if &ldquo;Static Title&rdquo; is selected above.'),
  97. );
  98. $form['title_maxlength'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Max length of title field'),
  101. '#default_value' => isset($instance['settings']['title_maxlength']) ? $instance['settings']['title_maxlength'] : '128',
  102. '#description' => t('Set a maximum length on the title field (applies only if Link Title is optional or required). The maximum limit is 255 characters.'),
  103. '#maxlength' => 3,
  104. '#size' => 3,
  105. );
  106. if (module_exists('token')) {
  107. // Add token module replacements fields
  108. $form['enable_tokens'] = array(
  109. '#type' => 'checkbox',
  110. '#title' => t('Allow user-entered tokens'),
  111. '#default_value' => isset($instance['settings']['enable_tokens']) ? $instance['settings']['enable_tokens'] : 1,
  112. '#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.'),
  113. );
  114. $entity_info = entity_get_info($instance['entity_type']);
  115. $form['tokens_help'] = array(
  116. '#theme' => 'token_tree',
  117. '#token_types' => array($entity_info['token type']),
  118. '#global_types' => TRUE,
  119. '#click_insert' => TRUE,
  120. '#dialog' => TRUE,
  121. );
  122. }
  123. $form['display'] = array(
  124. '#tree' => TRUE,
  125. );
  126. $form['display']['url_cutoff'] = array(
  127. '#type' => 'textfield',
  128. '#title' => t('URL Display Cutoff'),
  129. '#default_value' => isset($instance['settings']['display']['url_cutoff']) ? $instance['settings']['display']['url_cutoff'] : '80',
  130. '#description' => t('If the user does not include a title for this link, the URL will be used as the title. When should the link title be trimmed and finished with an elipsis (&hellip;)? Leave blank for no limit.'),
  131. '#maxlength' => 3,
  132. '#size' => 3,
  133. );
  134. $target_options = array(
  135. LINK_TARGET_DEFAULT => t('Default (no target attribute)'),
  136. LINK_TARGET_TOP => t('Open link in window root'),
  137. LINK_TARGET_NEW_WINDOW => t('Open link in new window'),
  138. LINK_TARGET_USER => t('Allow the user to choose'),
  139. );
  140. $form['attributes'] = array(
  141. '#tree' => TRUE,
  142. );
  143. $form['attributes']['target'] = array(
  144. '#type' => 'radios',
  145. '#title' => t('Link Target'),
  146. '#default_value' => empty($instance['settings']['attributes']['target']) ? LINK_TARGET_DEFAULT : $instance['settings']['attributes']['target'],
  147. '#options' => $target_options,
  148. );
  149. $form['attributes']['rel'] = array(
  150. '#type' => 'textfield',
  151. '#title' => t('Rel Attribute'),
  152. '#description' => t('When output, this link will have this rel attribute. The most common usage is <a href="http://en.wikipedia.org/wiki/Nofollow">rel=&quot;nofollow&quot;</a> which prevents some search engines from spidering entered links.'),
  153. '#default_value' => empty($instance['settings']['attributes']['rel']) ? '' : $instance['settings']['attributes']['rel'],
  154. '#field_prefix' => 'rel = "',
  155. '#field_suffix' => '"',
  156. '#size' => 20,
  157. );
  158. $rel_remove_options = array(
  159. 'default' => t('Keep rel as set up above (untouched/default)'),
  160. 'rel_remove_external' => t('Remove rel if given link is external'),
  161. 'rel_remove_internal' => t('Remove rel if given link is internal'),
  162. );
  163. $form['rel_remove'] = array(
  164. '#type' => 'radios',
  165. '#title' => t('Remove rel attribute automatically'),
  166. '#default_value' => !isset($instance['settings']['rel_remove']) ? 'default' : $instance['settings']['rel_remove'],
  167. '#description' => t('Turn on/off if rel attribute should be removed automatically, if user given link is internal/external'),
  168. '#options' => $rel_remove_options,
  169. );
  170. $form['attributes']['class'] = array(
  171. '#type' => 'textfield',
  172. '#title' => t('Additional CSS Class'),
  173. '#description' => t('When output, this link will have this class attribute. Multiple classes should be separated by spaces.'),
  174. '#default_value' => empty($instance['settings']['attributes']['class']) ? '' : $instance['settings']['attributes']['class'],
  175. );
  176. $form['attributes']['configurable_title'] = array(
  177. '#title' => t("Allow the user to enter a link 'title' attribute"),
  178. '#type' => 'checkbox',
  179. '#default_value' => empty($instance['settings']['attributes']['configurable_title']) ? '' : $instance['settings']['attributes']['configurable_title'],
  180. );
  181. $form['attributes']['title'] = array(
  182. '#title' => t("Default link 'title' Attribute"),
  183. '#type' => 'textfield',
  184. '#description' => t('When output, links will use this "title" attribute if the user does not provide one and when different from the link text. Read <a href="http://www.w3.org/TR/WCAG10-HTML-TECHS/#links">WCAG 1.0 Guidelines</a> for links comformances. Tokens values will be evaluated.'),
  185. '#default_value' => empty($instance['settings']['attributes']['title']) ? '' : $instance['settings']['attributes']['title'],
  186. '#field_prefix' => 'title = "',
  187. '#field_suffix' => '"',
  188. '#size' => 20,
  189. );
  190. return $form;
  191. }
  192. /**
  193. * #element_validate handler for link_field_instance_settings_form().
  194. */
  195. function link_field_settings_form_validate($element, &$form_state, $complete_form) {
  196. if ($form_state['values']['instance']['settings']['title'] === 'value' && empty($form_state['values']['instance']['settings']['title_value'])) {
  197. form_set_error('title_value', t('A default title must be provided if the title is a static value.'));
  198. }
  199. if (!empty($form_state['values']['instance']['settings']['display']['url_cutoff']) && !is_numeric($form_state['values']['instance']['settings']['display']['url_cutoff'])) {
  200. form_set_error('display', t('URL Display Cutoff value must be numeric.'));
  201. }
  202. if (empty($form_state['values']['instance']['settings']['title_maxlength'])) {
  203. form_set_value($element['title_maxlength'], '128', $form_state);
  204. }
  205. elseif (!is_numeric($form_state['values']['instance']['settings']['title_maxlength'])) {
  206. form_set_error('title_maxlength', t('The max length of the link title must be numeric.'));
  207. }
  208. elseif ($form_state['values']['instance']['settings']['title_maxlength'] > 255) {
  209. form_set_error('title_maxlength', t('The max length of the link title cannot be greater than 255 characters.'));
  210. }
  211. }
  212. /**
  213. * Implements hook_field_is_empty().
  214. */
  215. function link_field_is_empty($item, $field) {
  216. return empty($item['title']) && empty($item['url']);
  217. }
  218. /**
  219. * Implements hook_field_load().
  220. */
  221. function link_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  222. foreach ($entities as $id => $entity) {
  223. foreach ($items[$id] as $delta => $item) {
  224. $items[$id][$delta]['attributes'] = _link_load($field, $item, $instances[$id]);
  225. }
  226. }
  227. }
  228. /**
  229. * Implements hook_field_validate().
  230. */
  231. function link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  232. $optional_field_found = FALSE;
  233. if ($instance['settings']['validate_url'] !== 0 || is_null($instance['settings']['validate_url']) || !isset($instance['settings']['validate_url'])) {
  234. foreach ($items as $delta => $value) {
  235. _link_validate($items[$delta], $delta, $field, $entity, $instance, $langcode, $optional_field_found, $errors);
  236. }
  237. }
  238. if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] && !$optional_field_found) {
  239. $errors[$field['field_name']][$langcode][0][] = array(
  240. 'error' => 'link_required',
  241. 'message' => t('At least one title or URL must be entered.'),
  242. 'error_element' => array('url' => FALSE, 'title' => TRUE),
  243. );
  244. }
  245. }
  246. /**
  247. * Implements hook_field_insert().
  248. */
  249. function link_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  250. foreach ($items as $delta => $value) {
  251. _link_process($items[$delta], $delta, $field, $entity);
  252. }
  253. }
  254. /**
  255. * Implements hook_field_update().
  256. */
  257. function link_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  258. foreach ($items as $delta => $value) {
  259. _link_process($items[$delta], $delta, $field, $entity);
  260. }
  261. }
  262. /**
  263. * Implements hook_field_prepare_view().
  264. */
  265. function link_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  266. foreach ($items as $entity_id => $entity_items) {
  267. foreach ($entity_items as $delta => $value) {
  268. _link_sanitize($items[$entity_id][$delta], $delta, $field, $instances[$entity_id], $entities[$entity_id]);
  269. }
  270. }
  271. }
  272. /**
  273. * Implements hook_field_widget_info().
  274. */
  275. function link_field_widget_info() {
  276. return array(
  277. 'link_field' => array(
  278. 'label' => 'Link',
  279. 'field types' => array('link_field'),
  280. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  281. ),
  282. );
  283. }
  284. /**
  285. * Implements hook_field_widget_form().
  286. */
  287. function link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  288. $element += array(
  289. '#type' => $instance['widget']['type'],
  290. '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  291. );
  292. return $element;
  293. }
  294. /**
  295. * Implements hook_field_widget_error().
  296. */
  297. function link_field_widget_error($element, $error, $form, &$form_state) {
  298. if ($error['error_element']['title']) {
  299. form_error($element['title'], $error['message']);
  300. }
  301. elseif ($error['error_element']['url']) {
  302. form_error($element['url'], $error['message']);
  303. }
  304. }
  305. /**
  306. * Unpacks the item attributes for use.
  307. */
  308. function _link_load($field, $item, $instance) {
  309. if (isset($item['attributes'])) {
  310. if (!is_array($item['attributes'])) {
  311. $item['attributes'] = unserialize($item['attributes']);
  312. }
  313. return $item['attributes'];
  314. }
  315. elseif (isset($instance['settings']['attributes'])) {
  316. return $instance['settings']['attributes'];
  317. }
  318. else {
  319. return $field['settings']['attributes'];
  320. }
  321. }
  322. /**
  323. * Prepares the item attributes and url for storage.
  324. */
  325. function _link_process(&$item, $delta = 0, $field, $entity) {
  326. // Trim whitespace from URL.
  327. $item['url'] = trim($item['url']);
  328. // If no attributes are set then make sure $item['attributes'] is an empty
  329. // array, so $field['attributes'] can override it.
  330. if (empty($item['attributes'])) {
  331. $item['attributes'] = array();
  332. }
  333. // Serialize the attributes array.
  334. if (!is_string($item['attributes'])) {
  335. $item['attributes'] = serialize($item['attributes']);
  336. }
  337. // Don't save an invalid default value (e.g. 'http://').
  338. if ((isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url']) && is_object($entity)) {
  339. if (!link_validate_url($item['url'])) {
  340. unset($item['url']);
  341. }
  342. }
  343. }
  344. /**
  345. * Validates that the link field has been entered properly.
  346. */
  347. function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) {
  348. if ($item['url'] && !(isset($instance['default_value'][$delta]['url']) && $item['url'] === $instance['default_value'][$delta]['url'] && !$instance['required'])) {
  349. // Validate the link.
  350. if (link_validate_url(trim($item['url'])) == FALSE) {
  351. $errors[$field['field_name']][$langcode][$delta][] = array(
  352. 'error' => 'link_required',
  353. 'message' => t('The value provided for %field is not a valid URL.', array('%field' => $instance['label'])),
  354. 'error_element' => array('url' => TRUE, 'title' => FALSE),
  355. );
  356. }
  357. // Require a title for the link if necessary.
  358. if ($instance['settings']['title'] == 'required' && strlen(trim($item['title'])) == 0) {
  359. $errors[$field['field_name']][$langcode][$delta][] = array(
  360. 'error' => 'link_required',
  361. 'message' => t('Titles are required for all links.'),
  362. 'error_element' => array('url' => FALSE, 'title' => TRUE),
  363. );
  364. }
  365. }
  366. // Require a link if we have a title.
  367. if ($instance['settings']['url'] !== 'optional' && strlen(isset($item['title']) ? $item['title'] : NULL) > 0 && strlen(trim($item['url'])) == 0) {
  368. $errors[$field['field_name']][$langcode][$delta][] = array(
  369. 'error' => 'link_required',
  370. 'message' => t('You cannot enter a title without a link url.'),
  371. 'error_element' => array('url' => TRUE, 'title' => FALSE),
  372. );
  373. }
  374. // In a totally bizzaro case, where URLs and titles are optional but the field is required, ensure there is at least one link.
  375. if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && (strlen(trim($item['url'])) !== 0 || strlen(trim($item['title'])) !== 0)) {
  376. $optional_field_found = TRUE;
  377. }
  378. // Require entire field
  379. if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] == 1 && !$optional_field_found && isset($instance['id'])) {
  380. $errors[$field['field_name']][$langcode][$delta][] = array(
  381. 'error' => 'link_required',
  382. 'message' => t('At least one title or URL must be entered.'),
  383. 'error_element' => array('url' => FALSE, 'title' => TRUE),
  384. );
  385. }
  386. }
  387. /**
  388. * Clean up user-entered values for a link field according to field settings.
  389. *
  390. * @param $item
  391. * A single link item, usually containing url, title, and attributes.
  392. * @param $delta
  393. * The delta value if this field is one of multiple fields.
  394. * @param $field
  395. * The CCK field definition.
  396. * @param $entity
  397. * The entity containing this link.
  398. */
  399. function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
  400. // Don't try to process empty links.
  401. if (empty($item['url']) && empty($item['title'])) {
  402. return;
  403. }
  404. // Replace URL tokens.
  405. $entity_type = $instance['entity_type'];
  406. $entity_info = entity_get_info($entity_type);
  407. $property_id = $entity_info['entity keys']['id'];
  408. $entity_token_type = isset($entity_info['token type']) ? $entity_info['token type'] : (
  409. $entity_type == 'taxonomy_term' || $entity_type == 'taxonomy_vocabulary' ? str_replace('taxonomy_', '', $entity_type) : $entity_type
  410. );
  411. if (isset($instance['settings']['enable_tokens']) && $instance['settings']['enable_tokens']) {
  412. global $user;
  413. // Load the entity if necessary for entities in views.
  414. if (isset($entity->{$property_id})) {
  415. $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
  416. $entity_loaded = array_pop($entity_loaded);
  417. }
  418. else {
  419. $entity_loaded = $entity;
  420. }
  421. $item['url'] = token_replace($item['url'], array($entity_token_type => $entity_loaded));
  422. }
  423. $type = link_validate_url($item['url']);
  424. // If the type of the URL cannot be determined and URL validation is disabled,
  425. // then assume LINK_EXTERNAL for later processing.
  426. if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
  427. $type = LINK_EXTERNAL;
  428. }
  429. $url = link_cleanup_url($item['url']);
  430. $url_parts = _link_parse_url($url);
  431. if (!empty($url_parts['url'])) {
  432. $item['url'] = url($url_parts['url'],
  433. array(
  434. 'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
  435. 'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
  436. 'absolute' => TRUE,
  437. 'html' => TRUE,
  438. )
  439. );
  440. }
  441. // Create a shortened URL for display.
  442. if ($type == LINK_EMAIL) {
  443. $display_url = str_replace('mailto:', '', $url);
  444. }
  445. else {
  446. $display_url = url($url_parts['url'],
  447. array(
  448. 'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
  449. 'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
  450. 'absolute' => TRUE,
  451. )
  452. );
  453. }
  454. if ($instance['settings']['display']['url_cutoff'] && strlen($display_url) > $instance['settings']['display']['url_cutoff']) {
  455. $display_url = substr($display_url, 0, $instance['settings']['display']['url_cutoff']) . "...";
  456. }
  457. $item['display_url'] = $display_url;
  458. // Use the title defined at the instance level.
  459. if ($instance['settings']['title'] == 'value' && strlen(trim($instance['settings']['title_value']))) {
  460. $title = $instance['settings']['title_value'];
  461. if (function_exists('i18n_string_translate')) {
  462. $i18n_string_name = "field:{$instance['field_name']}:{$instance['bundle']}:title_value";
  463. $title = i18n_string_translate($i18n_string_name, $title);
  464. }
  465. }
  466. // Use the title defined by the user at the widget level.
  467. elseif (isset($item['title'])) {
  468. $title = $item['title'];
  469. }
  470. else {
  471. $title = '';
  472. }
  473. // Replace tokens.
  474. if ($title && ($instance['settings']['title'] == 'value' || $instance['settings']['enable_tokens'])) {
  475. // Load the entity if necessary for entities in views.
  476. if (isset($entity->{$property_id})) {
  477. $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
  478. $entity_loaded = array_pop($entity_loaded);
  479. }
  480. else {
  481. $entity_loaded = $entity;
  482. }
  483. $title = token_replace($title, array($entity_token_type => $entity_loaded));
  484. $title = filter_xss($title, array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
  485. $item['html'] = TRUE;
  486. }
  487. $item['title'] = empty($title) ? $item['display_url'] : $title;
  488. if (!isset($item['attributes'])) {
  489. $item['attributes'] = array();
  490. }
  491. // Unserialize attributtes array if it has not been unserialized yet.
  492. if (!is_array($item['attributes'])) {
  493. $item['attributes'] = (array)unserialize($item['attributes']);
  494. }
  495. // Add default attributes.
  496. if (!is_array($instance['settings']['attributes'])) {
  497. $instance['settings']['attributes'] = _link_default_attributes();
  498. }
  499. else {
  500. $instance['settings']['attributes'] += _link_default_attributes();
  501. }
  502. // Merge item attributes with attributes defined at the field level.
  503. $item['attributes'] += $instance['settings']['attributes'];
  504. // If user is not allowed to choose target attribute, use default defined at
  505. // field level.
  506. if ($instance['settings']['attributes']['target'] != LINK_TARGET_USER) {
  507. $item['attributes']['target'] = $instance['settings']['attributes']['target'];
  508. }
  509. elseif ($item['attributes']['target'] == LINK_TARGET_USER) {
  510. $item['attributes']['target'] = LINK_TARGET_DEFAULT;
  511. }
  512. // Remove the target attribute if the default (no target) is selected.
  513. if (empty($item['attributes']) || (isset($item['attributes']['target']) && $item['attributes']['target'] == LINK_TARGET_DEFAULT)) {
  514. unset($item['attributes']['target']);
  515. }
  516. // Remove rel attribute for internal or external links if selected.
  517. if (isset($item['attributes']['rel']) && isset($instance['settings']['rel_remove']) && $instance['settings']['rel_remove'] != 'default') {
  518. if (($instance['settings']['rel_remove'] != 'rel_remove_internal' && $type != LINK_INTERNAL) ||
  519. ($instance['settings']['rel_remove'] != 'rel_remove_external' && $type != LINK_EXTERNAL)) {
  520. unset($item['attributes']['rel']);
  521. }
  522. }
  523. // Handle "title" link attribute.
  524. if (!empty($item['attributes']['title']) && module_exists('token')) {
  525. // Load the entity (necessary for entities in views).
  526. if (isset($entity->{$property_id})) {
  527. $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
  528. $entity_loaded = array_pop($entity_loaded);
  529. }
  530. else {
  531. $entity_loaded = $entity;
  532. }
  533. $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
  534. $item['attributes']['title'] = filter_xss($item['attributes']['title'], array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
  535. }
  536. // Remove title attribute if it's equal to link text.
  537. if (isset($item['attributes']['title']) && $item['attributes']['title'] == $item['title']) {
  538. unset($item['attributes']['title']);
  539. }
  540. unset($item['attributes']['configurable_title']);
  541. // Remove empty attributes.
  542. $item['attributes'] = array_filter($item['attributes']);
  543. }
  544. /**
  545. * Because parse_url doesn't work with relative urls.
  546. *
  547. * @param string $url
  548. * URL to parse.
  549. *
  550. * @return Array
  551. * Array of url pieces - only 'url', 'query', and 'fragment'.
  552. */
  553. function _link_parse_url($url) {
  554. $url_parts = array();
  555. // Separate out the anchor, if any.
  556. if (strpos($url, '#') !== FALSE) {
  557. $url_parts['fragment'] = substr($url, strpos($url, '#') + 1);
  558. $url = substr($url, 0, strpos($url, '#'));
  559. }
  560. // Separate out the query string, if any.
  561. if (strpos($url, '?') !== FALSE) {
  562. $query = substr($url, strpos($url, '?') + 1);
  563. $url_parts['query'] = _link_parse_str($query);
  564. $url = substr($url, 0, strpos($url, '?'));
  565. }
  566. $url_parts['url'] = $url;
  567. return $url_parts;
  568. }
  569. /**
  570. * Bacause parse_str replaces the following characters in query parameters name
  571. * in order to maintain compability with deprecated register_globals directive:
  572. *
  573. * - chr(32) ( ) (space)
  574. * - chr(46) (.) (dot)
  575. * - chr(91) ([) (open square bracket)
  576. * - chr(128) - chr(159) (various)
  577. *
  578. * @param string $query
  579. * Query to parse.
  580. *
  581. * @return Array
  582. * Array of query parameters.
  583. *
  584. * @see http://php.net/manual/en/language.variables.external.php#81080
  585. */
  586. function _link_parse_str($query) {
  587. $query_array = array();
  588. $pairs = explode('&', $query);
  589. foreach ($pairs as $pair) {
  590. $name_value = explode('=', $pair);
  591. $name = urldecode($name_value[0]);
  592. $value = isset($name_value[1]) ? urldecode($name_value[1]) : NULL;
  593. $query_array[$name] = $value;
  594. }
  595. return $query_array;
  596. }
  597. /**
  598. * Implements hook_theme().
  599. */
  600. function link_theme() {
  601. return array(
  602. 'link_formatter_link_default' => array(
  603. 'variables' => array('element' => NULL, 'field' => NULL),
  604. ),
  605. 'link_formatter_link_plain' => array(
  606. 'variables' => array('element' => NULL, 'field' => NULL),
  607. ),
  608. 'link_formatter_link_absolute' => array(
  609. 'variables' => array('element' => NULL, 'field' => NULL),
  610. ),
  611. 'link_formatter_link_domain' => array(
  612. 'variables' => array('element' => NULL, 'display' => NULL, 'field' => NULL),
  613. ),
  614. 'link_formatter_link_title_plain' => array(
  615. 'variables' => array('element' => NULL, 'field' => NULL),
  616. ),
  617. 'link_formatter_link_url' => array(
  618. 'variables' => array('element' => NULL, 'field' => NULL),
  619. ),
  620. 'link_formatter_link_short' => array(
  621. 'variables' => array('element' => NULL, 'field' => NULL),
  622. ),
  623. 'link_formatter_link_label' => array(
  624. 'variables' => array('element' => NULL, 'field' => NULL),
  625. ),
  626. 'link_formatter_link_separate' => array(
  627. 'variables' => array('element' => NULL, 'field' => NULL),
  628. ),
  629. 'link_field' => array(
  630. 'render element' => 'element',
  631. ),
  632. );
  633. }
  634. /**
  635. * Formats a link field widget.
  636. */
  637. function theme_link_field($vars) {
  638. drupal_add_css(drupal_get_path('module', 'link') . '/link.css');
  639. $element = $vars['element'];
  640. // Prefix single value link fields with the name of the field.
  641. if (empty($element['#field']['multiple'])) {
  642. if (isset($element['url']) && !isset($element['title'])) {
  643. $element['url']['#title_display'] = 'invisible';
  644. }
  645. }
  646. $output = '';
  647. $output .= '<div class="link-field-subrow clearfix">';
  648. if (isset($element['title'])) {
  649. $output .= '<div class="link-field-title link-field-column">' . drupal_render($element['title']) . '</div>';
  650. }
  651. $output .= '<div class="link-field-url' . (isset($element['title']) ? ' link-field-column' : '') . '">' . drupal_render($element['url']) . '</div>';
  652. $output .= '</div>';
  653. if (!empty($element['attributes']['target'])) {
  654. $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['target']) . '</div>';
  655. }
  656. if (!empty($element['attributes']['title'])) {
  657. $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['title']) . '</div>';
  658. }
  659. return $output;
  660. }
  661. /**
  662. * Implements hook_element_info().
  663. */
  664. function link_element_info() {
  665. $elements = array();
  666. $elements['link_field'] = array(
  667. '#input' => TRUE,
  668. '#process' => array('link_field_process'),
  669. '#theme' => 'link_field',
  670. '#theme_wrappers' => array('form_element'),
  671. );
  672. return $elements;
  673. }
  674. /**
  675. * Returns the default attributes and their values.
  676. */
  677. function _link_default_attributes() {
  678. return array(
  679. 'target' => LINK_TARGET_DEFAULT,
  680. 'class' => '',
  681. 'rel' => '',
  682. );
  683. }
  684. /**
  685. * Processes the link type element before displaying the field.
  686. *
  687. * Build the form element. When creating a form using FAPI #process,
  688. * note that $element['#value'] is already set.
  689. *
  690. * The $fields array is in $complete_form['#field_info'][$element['#field_name']].
  691. */
  692. function link_field_process($element, $form_state, $complete_form) {
  693. $instance = field_widget_instance($element, $form_state);
  694. $settings = $instance['settings'];
  695. $element['url'] = array(
  696. '#type' => 'textfield',
  697. '#maxlength' => LINK_URL_MAX_LENGTH,
  698. '#title' => t('URL'),
  699. '#required' => ($element['#delta'] == 0 && $settings['url'] !== 'optional') ? $element['#required'] : FALSE,
  700. '#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
  701. );
  702. if ($settings['title'] !== 'none' && $settings['title'] !== 'value') {
  703. $element['title'] = array(
  704. '#type' => 'textfield',
  705. '#maxlength' => $settings['title_maxlength'],
  706. '#title' => t('Title'),
  707. '#description' => t('The link title is limited to @maxlength characters maximum.', array('@maxlength' => $settings['title_maxlength'])),
  708. '#required' => ($settings['title'] == 'required' && (($element['#delta'] == 0 && $element['#required']) || !empty($element['#value']['url']))) ? TRUE : FALSE,
  709. '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
  710. );
  711. }
  712. // Initialize field attributes as an array if it is not an array yet.
  713. if (!is_array($settings['attributes'])) {
  714. $settings['attributes'] = array();
  715. }
  716. // Add default attributes.
  717. $settings['attributes'] += _link_default_attributes();
  718. $attributes = isset($element['#value']['attributes']) ? $element['#value']['attributes'] : $settings['attributes'];
  719. if (!empty($settings['attributes']['target']) && $settings['attributes']['target'] == LINK_TARGET_USER) {
  720. $element['attributes']['target'] = array(
  721. '#type' => 'checkbox',
  722. '#title' => t('Open URL in a New Window'),
  723. '#return_value' => LINK_TARGET_NEW_WINDOW,
  724. '#default_value' => isset($attributes['target']) ? $attributes['target'] : FALSE,
  725. );
  726. }
  727. if (!empty($settings['attributes']['configurable_title']) && $settings['attributes']['configurable_title'] == 1) {
  728. $element['attributes']['title'] = array(
  729. '#type' => 'textfield',
  730. '#title' => t('Link "title" attribute'),
  731. '#default_value' => isset($attributes['title']) ? $attributes['title'] : '',
  732. '#field_prefix' => 'title = "',
  733. '#field_suffix' => '"',
  734. );
  735. }
  736. // If the title field is avaliable or there are field accepts multiple values
  737. // then allow the individual field items display the required asterisk if needed.
  738. if (isset($element['title']) || isset($element['_weight'])) {
  739. // To prevent an extra required indicator, disable the required flag on the
  740. // base element since all the sub-fields are already required if desired.
  741. $element['#required'] = FALSE;
  742. }
  743. return $element;
  744. }
  745. /**
  746. * Implements hook_field_formatter_info().
  747. */
  748. function link_field_formatter_info() {
  749. return array(
  750. 'link_default' => array(
  751. 'label' => t('Title, as link (default)'),
  752. 'field types' => array('link_field'),
  753. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  754. ),
  755. 'link_title_plain' => array(
  756. 'label' => t('Title, as plain text'),
  757. 'field types' => array('link_field'),
  758. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  759. ),
  760. 'link_url' => array(
  761. 'label' => t('URL, as link'),
  762. 'field types' => array('link_field'),
  763. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  764. ),
  765. 'link_plain' => array(
  766. 'label' => t('URL, as plain text'),
  767. 'field types' => array('link_field'),
  768. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  769. ),
  770. 'link_absolute' => array(
  771. 'label' => t('URL, absolute'),
  772. 'field types' => array('link_field'),
  773. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  774. ),
  775. 'link_domain' => array(
  776. 'label' => t('Domain, as link'),
  777. 'field types' => array('link_field'),
  778. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  779. 'settings' => array(
  780. 'strip_www' => FALSE,
  781. ),
  782. ),
  783. 'link_short' => array(
  784. 'label' => t('Short, as link with title "Link"'),
  785. 'field types' => array('link_field'),
  786. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  787. ),
  788. 'link_label' => array(
  789. 'label' => t('Label, as link with label as title'),
  790. 'field types' => array('link_field'),
  791. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  792. ),
  793. 'link_separate' => array(
  794. 'label' => t('Separate title and URL'),
  795. 'field types' => array('link_field'),
  796. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  797. ),
  798. );
  799. }
  800. /**
  801. * Implements hook_field_formatter_settings_form().
  802. */
  803. function link_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  804. $display = $instance['display'][$view_mode];
  805. $settings = $display['settings'];
  806. $element = array();
  807. if ($display['type'] == 'link_domain') {
  808. $element['strip_www'] = array(
  809. '#title' => t('Strip www. from domain'),
  810. '#type' => 'checkbox',
  811. '#default_value' => $settings['strip_www'],
  812. );
  813. }
  814. return $element;
  815. }
  816. /**
  817. * Implements hook_field_formatter_settings_summary().
  818. */
  819. function link_field_formatter_settings_summary($field, $instance, $view_mode) {
  820. $display = $instance['display'][$view_mode];
  821. $settings = $display['settings'];
  822. if ($display['type'] == 'link_domain') {
  823. if ($display['settings']['strip_www']) {
  824. return t('Strip www. from domain');
  825. }
  826. else {
  827. return t('Leave www. in domain');
  828. }
  829. }
  830. return '';
  831. }
  832. /**
  833. * Implements hook_field_formatter_view().
  834. */
  835. function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  836. $elements = array();
  837. foreach ($items as $delta => $item) {
  838. $elements[$delta] = array(
  839. '#theme' => 'link_formatter_' . $display['type'],
  840. '#element' => $item,
  841. '#field' => $instance,
  842. '#display' => $display,
  843. );
  844. }
  845. return $elements;
  846. }
  847. /**
  848. * Formats a link.
  849. */
  850. function theme_link_formatter_link_default($vars) {
  851. $link_options = $vars['element'];
  852. unset($link_options['title']);
  853. unset($link_options['url']);
  854. if (isset($link_options['attributes']['class'])) {
  855. $link_options['attributes']['class'] = array($link_options['attributes']['class']);
  856. }
  857. // Display a normal link if both title and URL are available.
  858. if (!empty($vars['element']['title']) && !empty($vars['element']['url'])) {
  859. return l($vars['element']['title'], $vars['element']['url'], $link_options);
  860. }
  861. // If only a title, display the title.
  862. elseif (!empty($vars['element']['title'])) {
  863. return check_plain($vars['element']['title']);
  864. }
  865. elseif (!empty($vars['element']['url'])) {
  866. return l($vars['element']['title'], $vars['element']['url'], $link_options);
  867. }
  868. }
  869. /**
  870. * Formats a link (or its title) as plain text.
  871. */
  872. function theme_link_formatter_link_plain($vars) {
  873. $link_options = $vars['element'];
  874. if (isset($link_options['title'])) {
  875. unset($link_options['title']);
  876. }
  877. else {
  878. $vars['element']['title'] = '';
  879. }
  880. unset($link_options['url']);
  881. return empty($vars['element']['url']) ? check_plain($vars['element']['title']) : url($vars['element']['url'], $link_options);
  882. }
  883. /**
  884. * Formats a link as an absolute URL
  885. */
  886. function theme_link_formatter_link_absolute($vars) {
  887. $absolute = array('absolute' => TRUE);
  888. return empty($vars['element']['url']) ? '' : url($vars['element']['url'], $absolute + $vars['element']);
  889. }
  890. /**
  891. * Formats a link using the URL's domain for it's link text.
  892. */
  893. function theme_link_formatter_link_domain($vars) {
  894. $link_options = $vars['element'];
  895. unset($link_options['title']);
  896. unset($link_options['url']);
  897. $domain = parse_url($vars['element']['display_url'], PHP_URL_HOST);
  898. if (!empty($vars['display']['settings']['strip_www'])) {
  899. $domain = str_replace('www.', '', $domain);
  900. }
  901. return $vars['element']['url'] ? l($domain, $vars['element']['url'], $link_options) : '';
  902. }
  903. /**
  904. * Formats a link's title as plain text.
  905. */
  906. function theme_link_formatter_link_title_plain($vars) {
  907. return empty($vars['element']['title']) ? '' : check_plain($vars['element']['title']);
  908. }
  909. /**
  910. * Formats a link using an alternate display URL for its link text.
  911. */
  912. function theme_link_formatter_link_url($vars) {
  913. $link_options = $vars['element'];
  914. unset($link_options['title']);
  915. unset($link_options['url']);
  916. return $vars['element']['url'] ? l($vars['element']['display_url'], $vars['element']['url'], $link_options) : '';
  917. }
  918. /**
  919. * Formats a link using "Link" as the link text.
  920. */
  921. function theme_link_formatter_link_short($vars) {
  922. $link_options = $vars['element'];
  923. unset($link_options['title']);
  924. unset($link_options['url']);
  925. return $vars['element']['url'] ? l(t('Link'), $vars['element']['url'], $link_options) : '';
  926. }
  927. /**
  928. * Formats a link using the field's label as link text.
  929. */
  930. function theme_link_formatter_link_label($vars) {
  931. $link_options = $vars['element'];
  932. unset($link_options['title']);
  933. unset($link_options['url']);
  934. return $vars['element']['url'] ? l($vars['field']['label'], $vars['element']['url'], $link_options) : '';
  935. }
  936. /**
  937. * Formats a link as separate title and URL elements.
  938. */
  939. function theme_link_formatter_link_separate($vars) {
  940. $class = empty($vars['element']['attributes']['class']) ? '' : ' ' . $vars['element']['attributes']['class'];
  941. unset($vars['element']['attributes']['class']);
  942. $link_options = $vars['element'];
  943. unset($link_options['title']);
  944. unset($link_options['url']);
  945. $title = empty($vars['element']['title']) ? '' : check_plain($vars['element']['title']);
  946. /**
  947. * @TODO static html markup looks not very elegant
  948. * needs smarter output solution and an optional title/url seperator
  949. */
  950. $url_parts = _link_parse_url($vars['element']['url']);
  951. $output = '';
  952. $output .= '<div class="link-item ' . $class . '">';
  953. if (!empty($title)) {
  954. $output .= '<div class="link-title">' . $title . '</div>';
  955. }
  956. $output .= '<div class="link-url">' . l($url_parts['url'], $vars['element']['url'], $link_options) . '</div>';
  957. $output .= '</div>';
  958. return $output;
  959. }
  960. /**
  961. * Implements hook_token_list().
  962. */
  963. function link_token_list($type = 'all') {
  964. if ($type === 'field' || $type === 'all') {
  965. $tokens = array();
  966. $tokens['link']['url'] = t("Link URL");
  967. $tokens['link']['title'] = t("Link title");
  968. $tokens['link']['view'] = t("Formatted html link");
  969. return $tokens;
  970. }
  971. }
  972. function link_token_values($type, $object = NULL) {
  973. if ($type === 'field') {
  974. $item = $object[0];
  975. $tokens['url'] = $item['url'];
  976. $tokens['title'] = $item['title'];
  977. $tokens['view'] = isset($item['view']) ? $item['view'] : '';
  978. return $tokens;
  979. }
  980. }
  981. /**
  982. * Implements hook_views_api().
  983. */
  984. function link_views_api() {
  985. return array(
  986. 'api' => 2,
  987. 'path' => drupal_get_path('module', 'link') . '/views',
  988. );
  989. }
  990. /**
  991. * Forms a valid URL if possible from an entered address.
  992. *
  993. * Trims whitespace and automatically adds an http:// to addresses without a
  994. * protocol specified
  995. *
  996. * @param string $url
  997. * @param string $protocol
  998. * The protocol to be prepended to the url if one is not specified
  999. */
  1000. function link_cleanup_url($url, $protocol = 'http') {
  1001. $url = trim($url);
  1002. $type = link_validate_url($url);
  1003. if ($type === LINK_EXTERNAL) {
  1004. // Check if there is no protocol specified.
  1005. $protocol_match = preg_match("/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i", $url);
  1006. if (empty($protocol_match)) {
  1007. // But should there be? Add an automatic http:// if it starts with a domain name.
  1008. $LINK_DOMAINS = _link_domains();
  1009. $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\-_]*\.)+)(' . $LINK_DOMAINS . '|[a-z]{2}))/i', $url);
  1010. if (!empty($domain_match)) {
  1011. $url = $protocol . "://" . $url;
  1012. }
  1013. }
  1014. }
  1015. return $url;
  1016. }
  1017. /**
  1018. * Validates a URL.
  1019. *
  1020. * Accepts all URLs following RFC 1738 standard for URL formation and all e-mail
  1021. * addresses following the RFC 2368 standard for mailto address formation.
  1022. *
  1023. * @param string $text
  1024. *
  1025. * @return mixed
  1026. * Returns boolean FALSE if the URL is not valid. On success, returns one of
  1027. * the LINK_(linktype) constants.
  1028. */
  1029. function link_validate_url($text) {
  1030. // @TODO Complete letters.
  1031. $LINK_ICHARS_DOMAIN = (string) html_entity_decode(implode("", array(
  1032. "&#x00E6;", // æ
  1033. "&#x00C6;", // Æ
  1034. "&#x00C0;", // À
  1035. "&#x00E0;", // à
  1036. "&#x00C1;", // Á
  1037. "&#x00E1;", // á
  1038. "&#x00C2;", // Â
  1039. "&#x00E2;", // â
  1040. "&#x00E5;", // å
  1041. "&#x00C5;", // Å
  1042. "&#x00E4;", // ä
  1043. "&#x00C4;", // Ä
  1044. "&#x00C7;", // Ç
  1045. "&#x00E7;", // ç
  1046. "&#x00D0;", // Ð
  1047. "&#x00F0;", // ð
  1048. "&#x00C8;", // È
  1049. "&#x00E8;", // è
  1050. "&#x00C9;", // É
  1051. "&#x00E9;", // é
  1052. "&#x00CA;", // Ê
  1053. "&#x00EA;", // ê
  1054. "&#x00CB;", // Ë
  1055. "&#x00EB;", // ë
  1056. "&#x00CE;", // Î
  1057. "&#x00EE;", // î
  1058. "&#x00CF;", // Ï
  1059. "&#x00EF;", // ï
  1060. "&#x00F8;", // ø
  1061. "&#x00D8;", // Ø
  1062. "&#x00F6;", // ö
  1063. "&#x00D6;", // Ö
  1064. "&#x00D4;", // Ô
  1065. "&#x00F4;", // ô
  1066. "&#x00D5;", // Õ
  1067. "&#x00F5;", // õ
  1068. "&#x0152;", // Œ
  1069. "&#x0153;", // œ
  1070. "&#x00FC;", // ü
  1071. "&#x00DC;", // Ü
  1072. "&#x00D9;", // Ù
  1073. "&#x00F9;", // ù
  1074. "&#x00DB;", // Û
  1075. "&#x00FB;", // û
  1076. "&#x0178;", // Ÿ
  1077. "&#x00FF;", // ÿ
  1078. "&#x00D1;", // Ñ
  1079. "&#x00F1;", // ñ
  1080. "&#x00FE;", // þ
  1081. "&#x00DE;", // Þ
  1082. "&#x00FD;", // ý
  1083. "&#x00DD;", // Ý
  1084. "&#x00BF;", // ¿
  1085. )), ENT_QUOTES, 'UTF-8');
  1086. $LINK_ICHARS = $LINK_ICHARS_DOMAIN . (string) html_entity_decode(implode("", array(
  1087. "&#x00DF;", // ß
  1088. )), ENT_QUOTES, 'UTF-8');
  1089. $allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
  1090. $LINK_DOMAINS = _link_domains();
  1091. // Starting a parenthesis group with (?: means that it is grouped, but is not captured
  1092. $protocol = '((?:' . implode("|", $allowed_protocols) . '):\/\/)';
  1093. $authentication = "(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=" . $LINK_ICHARS . "]|%[0-9a-f]{2})+(?::(?:[\w" . $LINK_ICHARS . "\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})*)?)?@)";
  1094. $domain = '(?:(?:[a-z0-9' . $LINK_ICHARS_DOMAIN . ']([a-z0-9' . $LINK_ICHARS_DOMAIN . '\-_\[\]])*)(\.(([a-z0-9' . $LINK_ICHARS_DOMAIN . '\-_\[\]])+\.)*(' . $LINK_DOMAINS . '|[a-z]{2}))?)';
  1095. $ipv4 = '(?:[0-9]{1,3}(\.[0-9]{1,3}){3})';
  1096. $ipv6 = '(?:[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
  1097. $port = '(?::([0-9]{1,5}))';
  1098. // Pattern specific to external links.
  1099. $external_pattern = '/^' . $protocol . '?' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
  1100. // Pattern specific to internal links.
  1101. $internal_pattern = "/^(?:[a-z0-9" . $LINK_ICHARS . "_\-+\[\] ]+)";
  1102. $internal_pattern_file = "/^(?:[a-z0-9" . $LINK_ICHARS . "_\-+\[\]\. \/\(\)][a-z0-9" . $LINK_ICHARS . "_\-+\[\]\. \(\)][a-z0-9" . $LINK_ICHARS . "_\-+\[\]\. \/\(\)]+)$/i";
  1103. $directories = "(?:\/[a-z0-9" . $LINK_ICHARS . "_\-\.~+%=&,$'#!():;*@\[\]]*)*";
  1104. // Yes, four backslashes == a single backslash.
  1105. $query = "(?:\/?\?([?a-z0-9" . $LINK_ICHARS . "+_|\-\.~\/\\\\%=&,$'():;*@\[\]{} ]*))";
  1106. $anchor = "(?:#[a-z0-9" . $LINK_ICHARS . "_\-\.~+%=&,$'():;*@\[\]\/\?]*)";
  1107. // The rest of the path for a standard URL.
  1108. $end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
  1109. $message_id = '[^@].*@' . $domain;
  1110. $newsgroup_name = '(?:[0-9a-z+-]*\.)*[0-9a-z+-]*';
  1111. $news_pattern = '/^news:(' . $newsgroup_name . '|' . $message_id . ')$/i';
  1112. $user = '[a-zA-Z0-9' . $LINK_ICHARS . '_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
  1113. $email_pattern = '/^mailto:' . $user . '@' . '(?:' . $domain . '|' . $ipv4 . '|' . $ipv6 . '|localhost)' . $query . '?$/';
  1114. if (strpos($text, '<front>') === 0) {
  1115. return LINK_FRONT;
  1116. }
  1117. if (in_array('mailto', $allowed_protocols) && preg_match($email_pattern, $text)) {
  1118. return LINK_EMAIL;
  1119. }
  1120. if (in_array('news', $allowed_protocols) && preg_match($news_pattern, $text)) {
  1121. return LINK_NEWS;
  1122. }
  1123. if (preg_match($internal_pattern . $end, $text)) {
  1124. return LINK_INTERNAL;
  1125. }
  1126. if (preg_match($external_pattern . $end, $text)) {
  1127. return LINK_EXTERNAL;
  1128. }
  1129. if (preg_match($internal_pattern_file, $text)) {
  1130. return LINK_INTERNAL;
  1131. }
  1132. return FALSE;
  1133. }
  1134. /**
  1135. * Returns the list of allowed domains, including domains added by admins via variable_set/$config.
  1136. */
  1137. function _link_domains() {
  1138. $link_extra_domains = variable_get('link_extra_domains', array());
  1139. return empty($link_extra_domains) ? LINK_DOMAINS : LINK_DOMAINS . '|' . implode('|', $link_extra_domains);
  1140. }
  1141. /**
  1142. * Implements hook_migrate_field_alter().
  1143. */
  1144. function link_content_migrate_field_alter(&$field_value, $instance_value) {
  1145. if ($field_value['type'] == 'link') {
  1146. // Adjust the field type.
  1147. $field_value['type'] = 'link_field';
  1148. // Remove settings that are now on the instance.
  1149. foreach (array('attributes', 'display', 'url', 'title', 'title_value', 'enable_tokens', 'validate_url') as $setting) {
  1150. unset($field_value['settings'][$setting]);
  1151. }
  1152. }
  1153. }
  1154. /**
  1155. * Implements hook_migrate_instance_alter().
  1156. *
  1157. * Widget type also changed to link_field.
  1158. */
  1159. function link_content_migrate_instance_alter(&$instance_value, $field_value) {
  1160. if ($field_value['type'] == 'link') {
  1161. // Grab settings that were previously on the field.
  1162. foreach (array('attributes', 'display', 'url', 'title', 'title_value', 'enable_tokens', 'validate_url') as $setting) {
  1163. if (isset($field_value['settings'][$setting])) {
  1164. $instance_value['settings'][$setting] = $field_value['settings'][$setting];
  1165. }
  1166. }
  1167. // Adjust widget type.
  1168. if ($instance_value['widget']['type'] == 'link') {
  1169. $instance_value['widget']['type'] = 'link_field';
  1170. }
  1171. // Adjust formatter types.
  1172. foreach ($instance_value['display'] as $context => $settings) {
  1173. if (in_array($settings['type'], array('default', 'title_plain', 'url', 'plain', 'short', 'label', 'separate'))) {
  1174. $instance_value['display'][$context]['type'] = 'link_' . $settings['type'];
  1175. }
  1176. }
  1177. }
  1178. }
  1179. /**
  1180. * Implements hook_field_settings_form().
  1181. */
  1182. function link_field_settings_form() {
  1183. return array();
  1184. }
  1185. /**
  1186. * Additional callback to adapt the property info of link fields.
  1187. *
  1188. * @see entity_metadata_field_entity_property_info().
  1189. */
  1190. function link_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  1191. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  1192. // Define a data structure so it's possible to deal with both the link title
  1193. // and URL.
  1194. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  1195. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  1196. // Auto-create the field item as soon as a property is set.
  1197. $property['auto creation'] = 'link_field_item_create';
  1198. $property['property info'] = link_field_item_property_info();
  1199. $property['property info']['url']['required'] = !$instance['settings']['url'];
  1200. $property['property info']['title']['required'] = ($instance['settings']['title'] == 'required');
  1201. if ($instance['settings']['title'] == 'none') {
  1202. unset($property['property info']['title']);
  1203. }
  1204. unset($property['query callback']);
  1205. }
  1206. /**
  1207. * Callback for creating a new, empty link field item.
  1208. *
  1209. * @see link_field_property_info_callback()
  1210. */
  1211. function link_field_item_create() {
  1212. return array('title' => NULL, 'url' => NULL);
  1213. }
  1214. /**
  1215. * Defines info for the properties of the link-field item data structure.
  1216. */
  1217. function link_field_item_property_info() {
  1218. $properties['title'] = array(
  1219. 'type' => 'text',
  1220. 'label' => t('The title of the link.'),
  1221. 'setter callback' => 'entity_property_verbatim_set',
  1222. );
  1223. $properties['url'] = array(
  1224. 'type' => 'uri',
  1225. 'label' => t('The URL of the link.'),
  1226. 'setter callback' => 'entity_property_verbatim_set',
  1227. );
  1228. return $properties;
  1229. }
  1230. /**
  1231. * Implements hook_field_update_instance().
  1232. */
  1233. function link_field_update_instance($instance, $prior_instance) {
  1234. if (function_exists('i18n_string_update') && $instance['widget']['type'] == 'link_field' && $prior_instance['settings']['title_value'] != $instance['settings']['title_value']) {
  1235. $i18n_string_name = "field:{$instance['field_name']}:{$instance['bundle']}:title_value";
  1236. i18n_string_update($i18n_string_name, $instance['settings']['title_value']);
  1237. }
  1238. }
  1239. /**
  1240. * Implements hook_i18n_string_list_TEXTGROUP_alter().
  1241. */
  1242. function link_i18n_string_list_field_alter(&$strings, $type = NULL, $object = NULL) {
  1243. if ($type != 'field_instance' || !is_array($object) || !isset($object['widget']['type'])) {
  1244. return;
  1245. }
  1246. if ($object['widget']['type'] == 'link_field' && isset($object['settings']['title_value'])) {
  1247. $strings['field'][$object['field_name']][$object['bundle']]['title_value']['string'] = $object['settings']['title_value'];
  1248. }
  1249. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.