link.module 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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_TARGET_DEFAULT', 'default');
  12. define('LINK_TARGET_NEW_WINDOW', '_blank');
  13. define('LINK_TARGET_TOP', '_top');
  14. define('LINK_TARGET_USER', 'user');
  15. /**
  16. * Maximum URLs length - needs to match value in link.install.
  17. */
  18. define('LINK_URL_MAX_LENGTH', 2048);
  19. /**
  20. * Implements hook_help().
  21. */
  22. function link_help($path, $arg) {
  23. switch ($path) {
  24. case 'admin/help#link':
  25. $output = '<p><strong>About</strong></p>';
  26. $output .= '<p>' . 'The link provides a standard custom content field for links. Links can be easily added to any content types and profiles and include advanced validating and different ways of storing internal or external links and URLs. It also supports additional link text title, site wide tokens for titles and title attributes, target attributes, css class attribution, static repeating values, input conversion, and many more.' . '</p>';
  27. $output .= '<p>' . '<strong>Requirements / Dependencies</strong>' . '</p>';
  28. $output .= '<p>' . 'Fields API is provided already by core [no dependencies].' . '</p>';
  29. $output .= '<p><strong>Configuration</strong></p>';
  30. $output .= '<p>' . 'Configuration is only slightly more complicated than a text field. Link text titles for URLs can be made required, set as instead of URL, optional (default), or left out entirely. If no link text title is provided, the trimmed version of the complete URL will be displayed. The target attribute should be set to "_blank", "top", or left out completely (checkboxes provide info). The rel=nofollow attribute prevents the link from being followed by certain search engines.' . '</p>';
  31. return $output;
  32. }
  33. }
  34. /**
  35. * Implements hook_field_info().
  36. */
  37. function link_field_info() {
  38. return array(
  39. 'link_field' => array(
  40. 'label' => t('Link'),
  41. 'description' => t('Store a title, href, and attributes in the database to assemble a link.'),
  42. 'settings' => array(
  43. 'attributes' => _link_default_attributes(),
  44. 'url' => 0,
  45. 'title' => 'optional',
  46. 'title_value' => '',
  47. 'title_maxlength' => 128,
  48. 'enable_tokens' => 1,
  49. 'display' => array(
  50. 'url_cutoff' => 80,
  51. ),
  52. ),
  53. 'instance_settings' => array(
  54. 'attributes' => _link_default_attributes(),
  55. 'url' => 0,
  56. 'title' => 'optional',
  57. 'title_value' => '',
  58. 'title_label_use_field_label' => FALSE,
  59. 'title_maxlength' => 128,
  60. 'enable_tokens' => 1,
  61. 'display' => array(
  62. 'url_cutoff' => 80,
  63. ),
  64. 'validate_url' => 1,
  65. 'absolute_url' => 1,
  66. ),
  67. 'default_widget' => 'link_field',
  68. 'default_formatter' => 'link_default',
  69. // Support hook_entity_property_info() from contrib "Entity API".
  70. 'property_type' => 'field_item_link',
  71. 'property_callbacks' => array('link_field_property_info_callback'),
  72. ),
  73. );
  74. }
  75. /**
  76. * Implements hook_field_instance_settings_form().
  77. */
  78. function link_field_instance_settings_form($field, $instance) {
  79. $form = array(
  80. '#element_validate' => array('link_field_settings_form_validate'),
  81. );
  82. $form['absolute_url'] = array(
  83. '#type' => 'checkbox',
  84. '#title' => t('Absolute URL'),
  85. '#default_value' => isset($instance['settings']['absolute_url']) && ($instance['settings']['absolute_url'] !== '') ? $instance['settings']['absolute_url'] : TRUE,
  86. '#description' => t('If checked, the URL will always render as an absolute URL.'),
  87. );
  88. $form['validate_url'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Validate URL'),
  91. '#default_value' => isset($instance['settings']['validate_url']) && ($instance['settings']['validate_url'] !== '') ? $instance['settings']['validate_url'] : TRUE,
  92. '#description' => t('If checked, the URL field will be verified as a valid URL during validation.'),
  93. );
  94. $form['url'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Optional URL'),
  97. '#default_value' => isset($instance['settings']['url']) ? $instance['settings']['url'] : '',
  98. '#return_value' => 'optional',
  99. '#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.'),
  100. );
  101. $title_options = array(
  102. 'optional' => t('Optional Title'),
  103. 'required' => t('Required Title'),
  104. 'value' => t('Static Title'),
  105. 'select' => t('Selected Title'),
  106. 'none' => t('No Title'),
  107. );
  108. $form['title'] = array(
  109. '#type' => 'radios',
  110. '#title' => t('Link Title'),
  111. '#default_value' => isset($instance['settings']['title']) ? $instance['settings']['title'] : 'optional',
  112. '#options' => $title_options,
  113. '#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.'),
  114. );
  115. $form['title_value'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Static or default title'),
  118. '#default_value' => isset($instance['settings']['title_value']) ? $instance['settings']['title_value'] : '',
  119. '#description' => t('This title will 1) always be used if "Static Title" is selected above, or 2) used if "Optional title" is selected above and no title is entered when creating content.'),
  120. '#states' => array(
  121. 'visible' => array(
  122. ':input[name="instance[settings][title]"]' => array('value' => 'value'),
  123. ),
  124. ),
  125. );
  126. $form['title_allowed_values'] = array(
  127. '#type' => 'textarea',
  128. '#title' => t('Title allowed values'),
  129. '#default_value' => isset($instance['settings']['title_allowed_values']) ? $instance['settings']['title_allowed_values'] : '',
  130. '#description' => t('When using "Selected Title", you can allow users to select the title from a limited set of values (eg. Home, Office, Other). Enter here all possible values that title can take, one value per line.'),
  131. '#states' => array(
  132. 'visible' => array(
  133. ':input[name="instance[settings][title]"]' => array('value' => 'select'),
  134. ),
  135. ),
  136. );
  137. $form['title_label_use_field_label'] = array(
  138. '#type' => 'checkbox',
  139. '#title' => t('Use field label as the label for the title field'),
  140. '#default_value' => isset($instance['settings']['title_label_use_field_label']) ? $instance['settings']['title_label_use_field_label'] : FALSE,
  141. '#description' => t('If this is checked the field label will be hidden.'),
  142. );
  143. $form['title_maxlength'] = array(
  144. '#type' => 'textfield',
  145. '#title' => t('Max length of title field'),
  146. '#default_value' => isset($instance['settings']['title_maxlength']) ? $instance['settings']['title_maxlength'] : '128',
  147. '#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.'),
  148. '#maxlength' => 3,
  149. '#size' => 3,
  150. );
  151. if (module_exists('token')) {
  152. // Add token module replacements fields.
  153. $form['enable_tokens'] = array(
  154. '#type' => 'checkbox',
  155. '#title' => t('Allow user-entered tokens'),
  156. '#default_value' => isset($instance['settings']['enable_tokens']) ? $instance['settings']['enable_tokens'] : 1,
  157. '#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.'),
  158. );
  159. $entity_info = entity_get_info($instance['entity_type']);
  160. $form['tokens_help'] = array(
  161. '#theme' => 'token_tree',
  162. '#token_types' => array($entity_info['token type']),
  163. '#global_types' => TRUE,
  164. '#click_insert' => TRUE,
  165. '#dialog' => TRUE,
  166. );
  167. }
  168. $form['display'] = array(
  169. '#tree' => TRUE,
  170. );
  171. $form['display']['url_cutoff'] = array(
  172. '#type' => 'textfield',
  173. '#title' => t('URL Display Cutoff'),
  174. '#default_value' => isset($instance['settings']['display']['url_cutoff']) ? $instance['settings']['display']['url_cutoff'] : '80',
  175. '#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.'),
  176. '#maxlength' => 3,
  177. '#size' => 3,
  178. );
  179. $target_options = array(
  180. LINK_TARGET_DEFAULT => t('Default (no target attribute)'),
  181. LINK_TARGET_TOP => t('Open link in window root'),
  182. LINK_TARGET_NEW_WINDOW => t('Open link in new window'),
  183. LINK_TARGET_USER => t('Allow the user to choose'),
  184. );
  185. $form['attributes'] = array(
  186. '#tree' => TRUE,
  187. );
  188. $form['attributes']['target'] = array(
  189. '#type' => 'radios',
  190. '#title' => t('Link Target'),
  191. '#default_value' => empty($instance['settings']['attributes']['target']) ? LINK_TARGET_DEFAULT : $instance['settings']['attributes']['target'],
  192. '#options' => $target_options,
  193. );
  194. $form['attributes']['rel'] = array(
  195. '#type' => 'textfield',
  196. '#title' => t('Rel Attribute'),
  197. '#description' => t('When output, this link will have this rel attribute. The most common usage is <a href="http://en.wikipedia.org/wiki/Nofollow" target="blank">rel=&quot;nofollow&quot;</a> which prevents some search engines from spidering entered links.'),
  198. '#default_value' => empty($instance['settings']['attributes']['rel']) ? '' : $instance['settings']['attributes']['rel'],
  199. '#field_prefix' => 'rel = "',
  200. '#field_suffix' => '"',
  201. '#size' => 20,
  202. );
  203. $rel_remove_options = array(
  204. 'default' => t('Keep rel as set up above (untouched/default)'),
  205. 'rel_remove_external' => t('Remove rel if given link is external'),
  206. 'rel_remove_internal' => t('Remove rel if given link is internal'),
  207. );
  208. $form['rel_remove'] = array(
  209. '#type' => 'radios',
  210. '#title' => t('Remove rel attribute automatically'),
  211. '#default_value' => !isset($instance['settings']['rel_remove']) ? 'default' : $instance['settings']['rel_remove'],
  212. '#description' => t('Turn on/off if rel attribute should be removed automatically, if user given link is internal/external'),
  213. '#options' => $rel_remove_options,
  214. );
  215. $form['attributes']['configurable_class'] = array(
  216. '#title' => t("Allow the user to enter a custom link class per link"),
  217. '#type' => 'checkbox',
  218. '#default_value' => empty($instance['settings']['attributes']['configurable_class']) ? '' : $instance['settings']['attributes']['configurable_class'],
  219. );
  220. $form['attributes']['class'] = array(
  221. '#type' => 'textfield',
  222. '#title' => t('Additional CSS Class'),
  223. '#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'),
  224. '#default_value' => empty($instance['settings']['attributes']['class']) ? '' : $instance['settings']['attributes']['class'],
  225. );
  226. $form['attributes']['configurable_title'] = array(
  227. '#title' => t("Allow the user to enter a link 'title' attribute"),
  228. '#type' => 'checkbox',
  229. '#default_value' => empty($instance['settings']['attributes']['configurable_title']) ? '' : $instance['settings']['attributes']['configurable_title'],
  230. );
  231. $form['attributes']['title'] = array(
  232. '#title' => t("Default link 'title' Attribute"),
  233. '#type' => 'textfield',
  234. '#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" target="blank">WCAG 1.0 Guidelines</a> for links comformances. Tokens values will be evaluated.'),
  235. '#default_value' => empty($instance['settings']['attributes']['title']) ? '' : $instance['settings']['attributes']['title'],
  236. '#field_prefix' => 'title = "',
  237. '#field_suffix' => '"',
  238. '#size' => 20,
  239. );
  240. return $form;
  241. }
  242. /**
  243. * Form validate.
  244. *
  245. * #element_validate handler for link_field_instance_settings_form().
  246. */
  247. function link_field_settings_form_validate($element, &$form_state, $complete_form) {
  248. if ($form_state['values']['instance']['settings']['title'] === 'value' && empty($form_state['values']['instance']['settings']['title_value'])) {
  249. form_set_error('instance][settings][title_value', t('A default title must be provided if the title is a static value.'));
  250. }
  251. if ($form_state['values']['instance']['settings']['title'] === 'select'
  252. && empty($form_state['values']['instance']['settings']['title_allowed_values'])) {
  253. form_set_error('instance][settings][title_allowed_values', t('You must enter one or more allowed values for link Title, the title is a selected value.'));
  254. }
  255. if (!empty($form_state['values']['instance']['settings']['display']['url_cutoff']) && !is_numeric($form_state['values']['instance']['settings']['display']['url_cutoff'])) {
  256. form_set_error('display', t('URL Display Cutoff value must be numeric.'));
  257. }
  258. if (empty($form_state['values']['instance']['settings']['title_maxlength'])) {
  259. form_set_value($element['title_maxlength'], '128', $form_state);
  260. }
  261. elseif (!is_numeric($form_state['values']['instance']['settings']['title_maxlength'])) {
  262. form_set_error('title_maxlength', t('The max length of the link title must be numeric.'));
  263. }
  264. elseif ($form_state['values']['instance']['settings']['title_maxlength'] > 255) {
  265. form_set_error('title_maxlength', t('The max length of the link title cannot be greater than 255 characters.'));
  266. }
  267. }
  268. /**
  269. * Implements hook_field_is_empty().
  270. */
  271. function link_field_is_empty($item, $field) {
  272. return empty($item['title']) && empty($item['url']);
  273. }
  274. /**
  275. * Implements hook_field_load().
  276. */
  277. function link_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  278. foreach ($entities as $id => $entity) {
  279. foreach ($items[$id] as $delta => $item) {
  280. $items[$id][$delta]['attributes'] = _link_load($field, $item, $instances[$id]);
  281. }
  282. }
  283. }
  284. /**
  285. * Implements hook_field_validate().
  286. */
  287. function link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  288. $optional_field_found = FALSE;
  289. if ($instance['settings']['validate_url'] !== 0 || is_null($instance['settings']['validate_url']) || !isset($instance['settings']['validate_url'])) {
  290. foreach ($items as $delta => $value) {
  291. _link_validate($items[$delta], $delta, $field, $entity, $instance, $langcode, $optional_field_found, $errors);
  292. }
  293. }
  294. foreach ($items as $delta => $value) {
  295. if (isset($value['attributes']) && is_string($value['attributes'])) {
  296. $errors[$field['field_name']][$langcode][$delta][] = array(
  297. 'error' => 'link_required',
  298. 'message' => t('String values are not acceptable for attributes.'),
  299. 'error_element' => array('url' => TRUE, 'title' => FALSE),
  300. );
  301. }
  302. }
  303. if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] && !$optional_field_found) {
  304. $errors[$field['field_name']][$langcode][0][] = array(
  305. 'error' => 'link_required',
  306. 'message' => t('At least one title or URL must be entered.'),
  307. 'error_element' => array('url' => FALSE, 'title' => TRUE),
  308. );
  309. }
  310. }
  311. /**
  312. * Implements hook_field_insert().
  313. */
  314. function link_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  315. foreach ($items as $delta => $value) {
  316. _link_process($items[$delta], $delta, $field, $entity, $instance);
  317. }
  318. }
  319. /**
  320. * Implements hook_field_update().
  321. */
  322. function link_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  323. foreach ($items as $delta => $value) {
  324. _link_process($items[$delta], $delta, $field, $entity, $instance);
  325. }
  326. }
  327. /**
  328. * Implements hook_field_prepare_view().
  329. */
  330. function link_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  331. foreach ($items as $entity_id => $entity_items) {
  332. foreach ($entity_items as $delta => $value) {
  333. _link_sanitize($items[$entity_id][$delta], $delta, $field, $instances[$entity_id], $entities[$entity_id]);
  334. }
  335. }
  336. }
  337. /**
  338. * Implements hook_field_widget_info().
  339. */
  340. function link_field_widget_info() {
  341. return array(
  342. 'link_field' => array(
  343. 'label' => 'Link',
  344. 'field types' => array('link_field'),
  345. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  346. ),
  347. );
  348. }
  349. /**
  350. * Implements hook_field_widget_form().
  351. */
  352. function link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  353. $element += array(
  354. '#type' => $instance['widget']['type'],
  355. '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  356. );
  357. return $element;
  358. }
  359. /**
  360. * Implements hook_field_widget_error().
  361. */
  362. function link_field_widget_error($element, $error, $form, &$form_state) {
  363. if (!empty($error['error_element']['title'])) {
  364. form_error($element['title'], $error['message']);
  365. }
  366. elseif (!empty($error['error_element']['url'])) {
  367. form_error($element['url'], $error['message']);
  368. }
  369. }
  370. /**
  371. * Unpacks the item attributes for use.
  372. */
  373. function _link_load($field, $item, $instance) {
  374. if (isset($item['attributes'])) {
  375. if (!is_array($item['attributes'])) {
  376. $item['attributes'] = unserialize($item['attributes']);
  377. }
  378. return $item['attributes'];
  379. }
  380. elseif (isset($instance['settings']['attributes'])) {
  381. return $instance['settings']['attributes'];
  382. }
  383. else {
  384. return $field['settings']['attributes'];
  385. }
  386. }
  387. /**
  388. * Prepares the item attributes and url for storage.
  389. *
  390. * @param array $item
  391. * Link field values.
  392. * @param array $delta
  393. * The sequence number for current values.
  394. * @param array $field
  395. * The field structure array.
  396. * @param object $entity
  397. * Entity object.
  398. * @param array $instance
  399. * The instance structure for $field on $entity's bundle.
  400. *
  401. * @codingStandardsIgnoreStart
  402. */
  403. function _link_process(&$item, $delta, $field, $entity, $instance) {
  404. // @codingStandardsIgnoreEnd
  405. // Trim whitespace from URL.
  406. if (!empty($item['url'])) {
  407. $item['url'] = trim($item['url']);
  408. }
  409. // If no attributes are set then make sure $item['attributes'] is an empty
  410. // array, so $field['attributes'] can override it.
  411. if (empty($item['attributes'])) {
  412. $item['attributes'] = array();
  413. }
  414. // Serialize the attributes array.
  415. if (!is_string($item['attributes'])) {
  416. $item['attributes'] = serialize($item['attributes']);
  417. }
  418. // Don't save an invalid default value (e.g. 'http://').
  419. if ((isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url']) && is_object($entity)) {
  420. $langcode = !empty($entity) ? field_language($instance['entity_type'], $entity, $instance['field_name']) : LANGUAGE_NONE;
  421. if (!link_validate_url($item['url'], $langcode)) {
  422. unset($item['url']);
  423. }
  424. }
  425. }
  426. /**
  427. * Validates that the link field has been entered properly.
  428. */
  429. function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) {
  430. if ($item['url'] && !(isset($instance['default_value'][$delta]['url']) && $item['url'] === $instance['default_value'][$delta]['url'] && !$instance['required'])) {
  431. // Validate the link.
  432. if (!link_validate_url(trim($item['url']), $langcode)) {
  433. $errors[$field['field_name']][$langcode][$delta][] = array(
  434. 'error' => 'link_required',
  435. 'message' => t('The value %value provided for %field is not a valid URL.', array(
  436. '%value' => trim($item['url']),
  437. '%field' => $instance['label'],
  438. )),
  439. 'error_element' => array('url' => TRUE, 'title' => FALSE),
  440. );
  441. }
  442. // Require a title for the link if necessary.
  443. if ($instance['settings']['title'] == 'required' && strlen(trim($item['title'])) == 0) {
  444. $errors[$field['field_name']][$langcode][$delta][] = array(
  445. 'error' => 'link_required',
  446. 'message' => t('Titles are required for all links.'),
  447. 'error_element' => array('url' => FALSE, 'title' => TRUE),
  448. );
  449. }
  450. }
  451. // Require a link if we have a title.
  452. if ($instance['settings']['url'] !== 'optional' && strlen(isset($item['title']) ? $item['title'] : NULL) > 0 && strlen(trim($item['url'])) == 0) {
  453. $errors[$field['field_name']][$langcode][$delta][] = array(
  454. 'error' => 'link_required',
  455. 'message' => t('You cannot enter a title without a link url.'),
  456. 'error_element' => array('url' => TRUE, 'title' => FALSE),
  457. );
  458. }
  459. // In a totally bizzaro case, where URLs and titles are optional but the field
  460. // is required, ensure there is at least one link.
  461. if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional'
  462. && (strlen(trim($item['url'])) !== 0 || strlen(trim($item['title'])) !== 0)) {
  463. $optional_field_found = TRUE;
  464. }
  465. // Require entire field.
  466. if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] == 1 && !$optional_field_found && isset($instance['id'])) {
  467. $errors[$field['field_name']][$langcode][$delta][] = array(
  468. 'error' => 'link_required',
  469. 'message' => t('At least one title or URL must be entered.'),
  470. 'error_element' => array('url' => FALSE, 'title' => TRUE),
  471. );
  472. }
  473. }
  474. /**
  475. * Clean up user-entered values for a link field according to field settings.
  476. *
  477. * @param array $item
  478. * A single link item, usually containing url, title, and attributes.
  479. * @param int $delta
  480. * The delta value if this field is one of multiple fields.
  481. * @param array $field
  482. * The CCK field definition.
  483. * @param object $entity
  484. * The entity containing this link.
  485. *
  486. * @codingStandardsIgnoreStart
  487. */
  488. function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
  489. // @codingStandardsIgnoreEnd
  490. // Don't try to process empty links.
  491. if (empty($item['url']) && empty($item['title'])) {
  492. return;
  493. }
  494. if (empty($item['html'])) {
  495. $item['html'] = FALSE;
  496. }
  497. // Replace URL tokens.
  498. $entity_type = $instance['entity_type'];
  499. $entity_info = entity_get_info($entity_type);
  500. $property_id = $entity_info['entity keys']['id'];
  501. $entity_token_type = isset($entity_info['token type']) ? $entity_info['token type'] : (
  502. $entity_type == 'taxonomy_term' || $entity_type == 'taxonomy_vocabulary' ? str_replace('taxonomy_', '', $entity_type) : $entity_type
  503. );
  504. if (isset($instance['settings']['enable_tokens']) && $instance['settings']['enable_tokens']) {
  505. $text_tokens = token_scan($item['url']);
  506. if (!empty($text_tokens)) {
  507. // Load the entity if necessary for entities in views.
  508. if (isset($entity->{$property_id})) {
  509. $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
  510. $entity_loaded = array_pop($entity_loaded);
  511. }
  512. else {
  513. $entity_loaded = $entity;
  514. }
  515. $item['url'] = token_replace($item['url'], array($entity_token_type => $entity_loaded));
  516. }
  517. }
  518. $type = link_url_type($item['url']);
  519. // If the type of the URL cannot be determined and URL validation is disabled,
  520. // then assume LINK_EXTERNAL for later processing.
  521. if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
  522. $type = LINK_EXTERNAL;
  523. }
  524. $url = link_cleanup_url($item['url']);
  525. $url_parts = _link_parse_url($url);
  526. if (!empty($url_parts['url'])) {
  527. $item['url'] = url($url_parts['url'],
  528. array(
  529. 'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
  530. 'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
  531. 'absolute' => !empty($instance['settings']['absolute_url']),
  532. 'html' => TRUE,
  533. )
  534. );
  535. }
  536. // Create a shortened URL for display.
  537. if ($type == LINK_EMAIL) {
  538. $display_url = str_replace('mailto:', '', $url);
  539. }
  540. else {
  541. $display_url = url($url_parts['url'],
  542. array(
  543. 'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
  544. 'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
  545. 'absolute' => !empty($instance['settings']['absolute_url']),
  546. )
  547. );
  548. }
  549. if ($instance['settings']['display']['url_cutoff'] && strlen($display_url) > $instance['settings']['display']['url_cutoff']) {
  550. $display_url = substr($display_url, 0, $instance['settings']['display']['url_cutoff']) . "...";
  551. }
  552. $item['display_url'] = $display_url;
  553. // Use the title defined at the instance level.
  554. if ($instance['settings']['title'] == 'value' && strlen(trim($instance['settings']['title_value']))) {
  555. $title = $instance['settings']['title_value'];
  556. if (function_exists('i18n_string_translate')) {
  557. $i18n_string_name = "field:{$instance['field_name']}:{$instance['bundle']}:title_value";
  558. $title = i18n_string_translate($i18n_string_name, $title);
  559. }
  560. }
  561. // Use the title defined by the user at the widget level.
  562. elseif (drupal_strlen(trim($item['title']))) {
  563. $title = $item['title'];
  564. }
  565. // Use the static title if a user-defined title is optional and a static title
  566. // has been defined.
  567. elseif ($instance['settings']['title'] == 'optional' && drupal_strlen(trim($instance['settings']['title_value']))) {
  568. $title = $instance['settings']['title_value'];
  569. }
  570. else {
  571. $title = '';
  572. }
  573. // Replace title tokens.
  574. if ($title && $instance['settings']['enable_tokens']) {
  575. $text_tokens = token_scan($title);
  576. if (!empty($text_tokens)) {
  577. // Load the entity if necessary for entities in views.
  578. if (isset($entity->{$property_id})) {
  579. $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
  580. $entity_loaded = array_pop($entity_loaded);
  581. }
  582. else {
  583. $entity_loaded = $entity;
  584. }
  585. $title = token_replace($title, array($entity_token_type => $entity_loaded));
  586. }
  587. }
  588. if ($title && ($instance['settings']['title'] == 'value' || $instance['settings']['enable_tokens'])) {
  589. $title = filter_xss($title, array(
  590. 'b',
  591. 'br',
  592. 'code',
  593. 'em',
  594. 'i',
  595. 'img',
  596. 'span',
  597. 'strong',
  598. 'sub',
  599. 'sup',
  600. 'tt',
  601. 'u',
  602. ));
  603. $item['html'] = TRUE;
  604. }
  605. $item['title'] = empty($title) && $title !== '0' ? $item['display_url'] : $title;
  606. if (!isset($item['attributes'])) {
  607. $item['attributes'] = array();
  608. }
  609. // Unserialize attributtes array if it has not been unserialized yet.
  610. if (!is_array($item['attributes'])) {
  611. $item['attributes'] = (array) unserialize($item['attributes']);
  612. }
  613. // Add default attributes.
  614. if (!is_array($instance['settings']['attributes'])) {
  615. $instance['settings']['attributes'] = _link_default_attributes();
  616. }
  617. else {
  618. $instance['settings']['attributes'] += _link_default_attributes();
  619. }
  620. // Merge item attributes with attributes defined at the field level.
  621. $item['attributes'] += $instance['settings']['attributes'];
  622. // If user is not allowed to choose target attribute, use default defined at
  623. // field level.
  624. if ($instance['settings']['attributes']['target'] != LINK_TARGET_USER) {
  625. $item['attributes']['target'] = $instance['settings']['attributes']['target'];
  626. }
  627. elseif ($item['attributes']['target'] == LINK_TARGET_USER) {
  628. $item['attributes']['target'] = LINK_TARGET_DEFAULT;
  629. }
  630. // Remove the target attribute if the default (no target) is selected.
  631. if (empty($item['attributes']) || (isset($item['attributes']['target']) && $item['attributes']['target'] == LINK_TARGET_DEFAULT)) {
  632. unset($item['attributes']['target']);
  633. }
  634. // Remove rel attribute for internal or external links if selected.
  635. if (isset($item['attributes']['rel']) && isset($instance['settings']['rel_remove']) && $instance['settings']['rel_remove'] != 'default') {
  636. if (($instance['settings']['rel_remove'] != 'rel_remove_internal' && $type != LINK_INTERNAL) ||
  637. ($instance['settings']['rel_remove'] != 'rel_remove_external' && $type != LINK_EXTERNAL)) {
  638. unset($item['attributes']['rel']);
  639. }
  640. }
  641. // Handle "title" link attribute.
  642. if (!empty($item['attributes']['title']) && module_exists('token')) {
  643. $text_tokens = token_scan($item['attributes']['title']);
  644. if (!empty($text_tokens)) {
  645. // Load the entity (necessary for entities in views).
  646. if (isset($entity->{$property_id})) {
  647. $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
  648. $entity_loaded = array_pop($entity_loaded);
  649. }
  650. else {
  651. $entity_loaded = $entity;
  652. }
  653. $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
  654. }
  655. $item['attributes']['title'] = filter_xss($item['attributes']['title'], array(
  656. 'b',
  657. 'br',
  658. 'code',
  659. 'em',
  660. 'i',
  661. 'img',
  662. 'span',
  663. 'strong',
  664. 'sub',
  665. 'sup',
  666. 'tt',
  667. 'u',
  668. ));
  669. }
  670. // Handle attribute classes.
  671. if (!empty($item['attributes']['class'])) {
  672. $classes = explode(' ', $item['attributes']['class']);
  673. foreach ($classes as &$class) {
  674. $class = drupal_clean_css_identifier($class);
  675. }
  676. $item['attributes']['class'] = implode(' ', $classes);
  677. }
  678. unset($item['attributes']['configurable_class']);
  679. // Remove title attribute if it's equal to link text.
  680. if (isset($item['attributes']['title']) && $item['attributes']['title'] == $item['title']) {
  681. unset($item['attributes']['title']);
  682. }
  683. unset($item['attributes']['configurable_title']);
  684. // Remove empty attributes.
  685. $item['attributes'] = array_filter($item['attributes']);
  686. }
  687. /**
  688. * Because parse_url doesn't work with relative urls.
  689. *
  690. * @param string $url
  691. * URL to parse.
  692. *
  693. * @return array
  694. * Array of url pieces - only 'url', 'query', and 'fragment'.
  695. */
  696. function _link_parse_url($url) {
  697. $url_parts = array();
  698. // Separate out the anchor, if any.
  699. if (strpos($url, '#') !== FALSE) {
  700. $url_parts['fragment'] = substr($url, strpos($url, '#') + 1);
  701. $url = substr($url, 0, strpos($url, '#'));
  702. }
  703. // Separate out the query string, if any.
  704. if (strpos($url, '?') !== FALSE) {
  705. $query = substr($url, strpos($url, '?') + 1);
  706. $url_parts['query'] = _link_parse_str($query);
  707. $url = substr($url, 0, strpos($url, '?'));
  708. }
  709. $url_parts['url'] = $url;
  710. return $url_parts;
  711. }
  712. /**
  713. * Replaces the PHP parse_str() function.
  714. *
  715. * Because parse_str replaces the following characters in query parameters name
  716. * in order to maintain compatibility with deprecated register_globals
  717. * directive:
  718. *
  719. * - chr(32) ( ) (space)
  720. * - chr(46) (.) (dot)
  721. * - chr(91) ([) (open square bracket)
  722. * - chr(128) - chr(159) (various)
  723. *
  724. * @param string $query
  725. * Query to parse.
  726. *
  727. * @return array
  728. * Array of query parameters.
  729. *
  730. * @see http://php.net/manual/en/language.variables.external.php#81080
  731. */
  732. function _link_parse_str($query) {
  733. $query_array = array();
  734. $pairs = explode('&', $query);
  735. foreach ($pairs as $pair) {
  736. $name_value = explode('=', $pair, 2);
  737. $name = urldecode($name_value[0]);
  738. $value = isset($name_value[1]) ? urldecode($name_value[1]) : NULL;
  739. $query_array[$name] = $value;
  740. }
  741. return $query_array;
  742. }
  743. /**
  744. * Implements hook_theme().
  745. */
  746. function link_theme() {
  747. return array(
  748. 'link_formatter_link_default' => array(
  749. 'variables' => array('element' => NULL, 'field' => NULL),
  750. ),
  751. 'link_formatter_link_plain' => array(
  752. 'variables' => array('element' => NULL, 'field' => NULL),
  753. ),
  754. 'link_formatter_link_host' => array(
  755. 'variables' => array('element' => NULL),
  756. ),
  757. 'link_formatter_link_absolute' => array(
  758. 'variables' => array('element' => NULL, 'field' => NULL),
  759. ),
  760. 'link_formatter_link_domain' => array(
  761. 'variables' => array(
  762. 'element' => NULL,
  763. 'display' => NULL,
  764. 'field' => NULL,
  765. ),
  766. ),
  767. 'link_formatter_link_no_protocol' => array(
  768. 'variables' => array('element' => NULL, 'field' => NULL),
  769. ),
  770. 'link_formatter_link_title_plain' => array(
  771. 'variables' => array('element' => NULL, 'field' => NULL),
  772. ),
  773. 'link_formatter_link_url' => array(
  774. 'variables' => array('element' => NULL, 'field' => NULL),
  775. ),
  776. 'link_formatter_link_short' => array(
  777. 'variables' => array('element' => NULL, 'field' => NULL),
  778. ),
  779. 'link_formatter_link_label' => array(
  780. 'variables' => array('element' => NULL, 'field' => NULL),
  781. ),
  782. 'link_formatter_link_separate' => array(
  783. 'variables' => array('element' => NULL, 'field' => NULL),
  784. ),
  785. 'link_field' => array(
  786. 'render element' => 'element',
  787. ),
  788. );
  789. }
  790. /**
  791. * Formats a link field widget.
  792. */
  793. function theme_link_field($vars) {
  794. drupal_add_css(drupal_get_path('module', 'link') . '/link.css');
  795. $element = $vars['element'];
  796. // Prefix single value link fields with the name of the field.
  797. if (empty($element['#field']['multiple'])) {
  798. if (isset($element['url']) && !isset($element['title'])) {
  799. $element['url']['#title_display'] = 'invisible';
  800. }
  801. }
  802. $output = '';
  803. $output .= '<div class="link-field-subrow clearfix">';
  804. if (isset($element['title'])) {
  805. $output .= '<div class="link-field-title link-field-column">' . drupal_render($element['title']) . '</div>';
  806. }
  807. $output .= '<div class="link-field-url' . (isset($element['title']) ? ' link-field-column' : '') . '">' . drupal_render($element['url']) . '</div>';
  808. $output .= '</div>';
  809. if (!empty($element['attributes']['target'])) {
  810. $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['target']) . '</div>';
  811. }
  812. if (!empty($element['attributes']['title'])) {
  813. $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['title']) . '</div>';
  814. }
  815. if (!empty($element['attributes']['class'])) {
  816. $output .= '<div class="link-attributes">' . drupal_render($element['attributes']['class']) . '</div>';
  817. }
  818. $output .= drupal_render_children($element);
  819. return $output;
  820. }
  821. /**
  822. * Implements hook_element_info().
  823. */
  824. function link_element_info() {
  825. $elements = array();
  826. $elements['link_field'] = array(
  827. '#input' => TRUE,
  828. '#process' => array('link_field_process'),
  829. '#theme' => 'link_field',
  830. '#theme_wrappers' => array('form_element'),
  831. );
  832. return $elements;
  833. }
  834. /**
  835. * Returns the default attributes and their values.
  836. */
  837. function _link_default_attributes() {
  838. return array(
  839. 'target' => LINK_TARGET_DEFAULT,
  840. 'class' => '',
  841. 'rel' => '',
  842. );
  843. }
  844. /**
  845. * Processes the link type element before displaying the field.
  846. *
  847. * Build the form element. When creating a form using FAPI #process,
  848. * note that $element['#value'] is already set.
  849. *
  850. * The $fields array is in
  851. * $complete_form['#field_info'][$element['#field_name']].
  852. */
  853. function link_field_process($element, $form_state, $complete_form) {
  854. $instance = field_widget_instance($element, $form_state);
  855. $settings = $instance['settings'];
  856. $element['url'] = array(
  857. '#type' => 'textfield',
  858. '#maxlength' => LINK_URL_MAX_LENGTH,
  859. '#title' => t('URL'),
  860. '#required' => ($element['#delta'] == 0 && $settings['url'] !== 'optional') ? $element['#required'] : FALSE,
  861. '#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
  862. );
  863. if (in_array($settings['title'], array('optional', 'required'))) {
  864. // Figure out the label of the title field.
  865. if (!empty($settings['title_label_use_field_label'])) {
  866. // Use the element label as the title field label.
  867. $title_label = $element['#title'];
  868. // Hide the field label because there is no need for the duplicate labels.
  869. $element['#title_display'] = 'invisible';
  870. }
  871. else {
  872. $title_label = t('Title');
  873. }
  874. // Default value.
  875. $title_maxlength = 128;
  876. if (!empty($settings['title_maxlength'])) {
  877. $title_maxlength = $settings['title_maxlength'];
  878. }
  879. $element['title'] = array(
  880. '#type' => 'textfield',
  881. '#maxlength' => $title_maxlength,
  882. '#title' => $title_label,
  883. '#description' => t('The link title is limited to @maxlength characters maximum.', array('@maxlength' => $title_maxlength)),
  884. '#required' => ($settings['title'] == 'required' && (($element['#delta'] == 0 && $element['#required']) || !empty($element['#value']['url']))) ? TRUE : FALSE,
  885. '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
  886. );
  887. }
  888. elseif ($settings['title'] == 'select') {
  889. $options = drupal_map_assoc(array_filter(explode("\n", str_replace("\r", "\n", trim($settings['title_allowed_values'])))));
  890. $element['title'] = array(
  891. '#type' => 'select',
  892. '#title' => t('Title'),
  893. '#description' => t('Select the a title for this link.'),
  894. '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
  895. '#options' => $options,
  896. );
  897. }
  898. // Initialize field attributes as an array if it is not an array yet.
  899. if (!is_array($settings['attributes'])) {
  900. $settings['attributes'] = array();
  901. }
  902. // Add default attributes.
  903. $settings['attributes'] += _link_default_attributes();
  904. $attributes = isset($element['#value']['attributes']) ? $element['#value']['attributes'] : $settings['attributes'];
  905. if (!empty($settings['attributes']['target']) && $settings['attributes']['target'] == LINK_TARGET_USER) {
  906. $element['attributes']['target'] = array(
  907. '#type' => 'checkbox',
  908. '#title' => t('Open URL in a New Window'),
  909. '#return_value' => LINK_TARGET_NEW_WINDOW,
  910. '#default_value' => isset($attributes['target']) ? $attributes['target'] : FALSE,
  911. );
  912. }
  913. if (!empty($settings['attributes']['configurable_title']) && $settings['attributes']['configurable_title'] == 1) {
  914. $element['attributes']['title'] = array(
  915. '#type' => 'textfield',
  916. '#title' => t('Link "title" attribute'),
  917. '#default_value' => isset($attributes['title']) ? $attributes['title'] : '',
  918. '#field_prefix' => 'title = "',
  919. '#field_suffix' => '"',
  920. );
  921. }
  922. if (!empty($settings['attributes']['configurable_class']) && $settings['attributes']['configurable_class'] == 1) {
  923. $element['attributes']['class'] = array(
  924. '#type' => 'textfield',
  925. '#title' => t('Custom link class'),
  926. '#default_value' => isset($attributes['class']) ? $attributes['class'] : '',
  927. '#field_prefix' => 'class = "',
  928. '#field_suffix' => '"',
  929. );
  930. }
  931. // If the title field is available or there are field accepts multiple values
  932. // then allow the individual field items display the required asterisk if
  933. // needed.
  934. if (isset($element['title']) || isset($element['_weight'])) {
  935. // To prevent an extra required indicator, disable the required flag on the
  936. // base element since all the sub-fields are already required if desired.
  937. $element['#required'] = FALSE;
  938. }
  939. return $element;
  940. }
  941. /**
  942. * Implements hook_field_formatter_info().
  943. */
  944. function link_field_formatter_info() {
  945. return array(
  946. 'link_default' => array(
  947. 'label' => t('Title, as link (default)'),
  948. 'field types' => array('link_field'),
  949. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  950. ),
  951. 'link_title_plain' => array(
  952. 'label' => t('Title, as plain text'),
  953. 'field types' => array('link_field'),
  954. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  955. ),
  956. 'link_host' => array(
  957. 'label' => t('Host, as plain text'),
  958. 'field types' => array('link_field'),
  959. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  960. ),
  961. 'link_url' => array(
  962. 'label' => t('URL, as link'),
  963. 'field types' => array('link_field'),
  964. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  965. ),
  966. 'link_plain' => array(
  967. 'label' => t('URL, as plain text'),
  968. 'field types' => array('link_field'),
  969. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  970. ),
  971. 'link_absolute' => array(
  972. 'label' => t('URL, absolute'),
  973. 'field types' => array('link_field'),
  974. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  975. ),
  976. 'link_domain' => array(
  977. 'label' => t('Domain, as link'),
  978. 'field types' => array('link_field'),
  979. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  980. 'settings' => array(
  981. 'strip_www' => FALSE,
  982. ),
  983. ),
  984. 'link_no_protocol' => array(
  985. 'label' => t('URL with the protocol removed'),
  986. 'field types' => array('link_field'),
  987. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  988. ),
  989. 'link_short' => array(
  990. 'label' => t('Short, as link with title "Link"'),
  991. 'field types' => array('link_field'),
  992. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  993. ),
  994. 'link_label' => array(
  995. 'label' => t('Label, as link with label as title'),
  996. 'field types' => array('link_field'),
  997. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  998. ),
  999. 'link_separate' => array(
  1000. 'label' => t('Separate title and URL'),
  1001. 'field types' => array('link_field'),
  1002. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  1003. ),
  1004. );
  1005. }
  1006. /**
  1007. * Implements hook_field_formatter_settings_form().
  1008. */
  1009. function link_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  1010. $display = $instance['display'][$view_mode];
  1011. $settings = $display['settings'];
  1012. $element = array();
  1013. if ($display['type'] == 'link_domain') {
  1014. $element['strip_www'] = array(
  1015. '#title' => t('Strip www. from domain'),
  1016. '#type' => 'checkbox',
  1017. '#default_value' => $settings['strip_www'],
  1018. );
  1019. }
  1020. return $element;
  1021. }
  1022. /**
  1023. * Implements hook_field_formatter_settings_summary().
  1024. */
  1025. function link_field_formatter_settings_summary($field, $instance, $view_mode) {
  1026. $display = $instance['display'][$view_mode];
  1027. if ($display['type'] == 'link_domain') {
  1028. if ($display['settings']['strip_www']) {
  1029. return t('Strip www. from domain');
  1030. }
  1031. else {
  1032. return t('Leave www. in domain');
  1033. }
  1034. }
  1035. return '';
  1036. }
  1037. /**
  1038. * Implements hook_field_formatter_view().
  1039. */
  1040. function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  1041. $elements = array();
  1042. foreach ($items as $delta => $item) {
  1043. $elements[$delta] = array(
  1044. '#theme' => 'link_formatter_' . $display['type'],
  1045. '#element' => $item,
  1046. '#field' => $instance,
  1047. '#display' => $display,
  1048. );
  1049. }
  1050. return $elements;
  1051. }
  1052. /**
  1053. * Formats a link.
  1054. */
  1055. function theme_link_formatter_link_default($vars) {
  1056. $link_options = $vars['element'];
  1057. unset($link_options['title']);
  1058. unset($link_options['url']);
  1059. if (isset($link_options['attributes']['class'])) {
  1060. $link_options['attributes']['class'] = array($link_options['attributes']['class']);
  1061. }
  1062. // Display a normal link if both title and URL are available.
  1063. if (!empty($vars['element']['title']) && !empty($vars['element']['url'])) {
  1064. return l($vars['element']['title'], $vars['element']['url'], $link_options);
  1065. }
  1066. // If only a title, display the title.
  1067. elseif (!empty($vars['element']['title'])) {
  1068. return !empty($link_options['html']) ? $vars['element']['title'] : check_plain($vars['element']['title']);
  1069. }
  1070. elseif (!empty($vars['element']['url'])) {
  1071. return l($vars['element']['title'], $vars['element']['url'], $link_options);
  1072. }
  1073. }
  1074. /**
  1075. * Formats a link (or its title) as plain text.
  1076. */
  1077. function theme_link_formatter_link_plain($vars) {
  1078. $link_options = $vars['element'];
  1079. if (isset($link_options['title'])) {
  1080. unset($link_options['title']);
  1081. }
  1082. else {
  1083. $vars['element']['title'] = '';
  1084. }
  1085. unset($link_options['url']);
  1086. return empty($vars['element']['url']) ? check_plain($vars['element']['title']) : url($vars['element']['url'], $link_options);
  1087. }
  1088. /**
  1089. * Theme function for 'host' text field formatter.
  1090. */
  1091. function theme_link_formatter_link_host($vars) {
  1092. $host = @parse_url($vars['element']['url']);
  1093. return isset($host['host']) ? check_plain($host['host']) : '';
  1094. }
  1095. /**
  1096. * Formats a link as an absolute URL.
  1097. */
  1098. function theme_link_formatter_link_absolute($vars) {
  1099. $absolute = array('absolute' => TRUE);
  1100. return empty($vars['element']['url']) ? '' : url($vars['element']['url'], $absolute + $vars['element']);
  1101. }
  1102. /**
  1103. * Formats a link using the URL's domain for it's link text.
  1104. */
  1105. function theme_link_formatter_link_domain($vars) {
  1106. $link_options = $vars['element'];
  1107. unset($link_options['title']);
  1108. unset($link_options['url']);
  1109. $domain = parse_url($vars['element']['display_url'], PHP_URL_HOST);
  1110. if (!empty($vars['display']['settings']['strip_www'])) {
  1111. $domain = str_replace('www.', '', $domain);
  1112. }
  1113. return $vars['element']['url'] ? l($domain, $vars['element']['url'], $link_options) : '';
  1114. }
  1115. /**
  1116. * Formats a link without the http:// or https://.
  1117. */
  1118. function theme_link_formatter_link_no_protocol($vars) {
  1119. $link_options = $vars['element'];
  1120. unset($link_options['title']);
  1121. unset($link_options['url']);
  1122. // We drop any scheme of the url.
  1123. $scheme = parse_url($vars['element']['url']);
  1124. $search = '/' . preg_quote($scheme['scheme'] . '://', '/') . '/';
  1125. $replace = '';
  1126. $display_url = preg_replace($search, $replace, $vars['element']['url'], 1);
  1127. return $vars['element']['url'] ? l($display_url, $vars['element']['url'], $link_options) : '';
  1128. }
  1129. /**
  1130. * Formats a link's title as plain text.
  1131. */
  1132. function theme_link_formatter_link_title_plain($vars) {
  1133. return empty($vars['element']['title']) ? '' : check_plain(decode_entities($vars['element']['title']));
  1134. }
  1135. /**
  1136. * Formats a link using an alternate display URL for its link text.
  1137. */
  1138. function theme_link_formatter_link_url($vars) {
  1139. $link_options = $vars['element'];
  1140. unset($link_options['title']);
  1141. unset($link_options['url']);
  1142. return $vars['element']['url'] ? l($vars['element']['display_url'], $vars['element']['url'], $link_options) : '';
  1143. }
  1144. /**
  1145. * Formats a link using "Link" as the link text.
  1146. */
  1147. function theme_link_formatter_link_short($vars) {
  1148. $link_options = $vars['element'];
  1149. unset($link_options['title']);
  1150. unset($link_options['url']);
  1151. return $vars['element']['url'] ? l(t('Link'), $vars['element']['url'], $link_options) : '';
  1152. }
  1153. /**
  1154. * Formats a link using the field's label as link text.
  1155. */
  1156. function theme_link_formatter_link_label($vars) {
  1157. $link_options = $vars['element'];
  1158. unset($link_options['title']);
  1159. unset($link_options['url']);
  1160. return $vars['element']['url'] ? l($vars['field']['label'], $vars['element']['url'], $link_options) : '';
  1161. }
  1162. /**
  1163. * Formats a link as separate title and URL elements.
  1164. */
  1165. function theme_link_formatter_link_separate($vars) {
  1166. $class = empty($vars['element']['attributes']['class']) ? '' : ' ' . $vars['element']['attributes']['class'];
  1167. unset($vars['element']['attributes']['class']);
  1168. $link_options = $vars['element'];
  1169. unset($link_options['title']);
  1170. unset($link_options['url']);
  1171. $title = empty($vars['element']['title']) ? '' : check_plain($vars['element']['title']);
  1172. // @TODO static html markup looks not very elegant
  1173. // needs smarter output solution and an optional title/url seperator
  1174. $url_parts = _link_parse_url($vars['element']['url']);
  1175. $output = '';
  1176. $output .= '<div class="link-item ' . $class . '">';
  1177. if (!empty($title)) {
  1178. $output .= '<div class="link-title">' . $title . '</div>';
  1179. }
  1180. $output .= '<div class="link-url">' . l($url_parts['url'], $vars['element']['url'], $link_options) . '</div>';
  1181. $output .= '</div>';
  1182. return $output;
  1183. }
  1184. /**
  1185. * Implements hook_token_list().
  1186. *
  1187. * @TODO: hook_token_list no longer exists - this should change to
  1188. * hook_token_info().
  1189. */
  1190. function link_token_list($type = 'all') {
  1191. if ($type === 'field' || $type === 'all') {
  1192. $tokens = array();
  1193. $tokens['link']['url'] = t("Link URL");
  1194. $tokens['link']['title'] = t("Link title");
  1195. $tokens['link']['view'] = t("Formatted html link");
  1196. return $tokens;
  1197. }
  1198. }
  1199. /**
  1200. * Implements hook_token_values().
  1201. *
  1202. * @TODO: hook_token_values no longer exists - this should change to
  1203. * hook_tokens().
  1204. */
  1205. function link_token_values($type, $object = NULL) {
  1206. if ($type === 'field') {
  1207. $item = $object[0];
  1208. $tokens['url'] = $item['url'];
  1209. $tokens['title'] = $item['title'];
  1210. $tokens['view'] = isset($item['view']) ? $item['view'] : '';
  1211. return $tokens;
  1212. }
  1213. }
  1214. /**
  1215. * Implements hook_views_api().
  1216. */
  1217. function link_views_api() {
  1218. return array(
  1219. 'api' => 2,
  1220. 'path' => drupal_get_path('module', 'link') . '/views',
  1221. );
  1222. }
  1223. /**
  1224. * Forms a valid URL if possible from an entered address.
  1225. *
  1226. * Trims whitespace and automatically adds an http:// to addresses without a
  1227. * protocol specified.
  1228. *
  1229. * @param string $url
  1230. * The url entered by the user.
  1231. * @param string $protocol
  1232. * The protocol to be prepended to the url if one is not specified.
  1233. */
  1234. function link_cleanup_url($url, $protocol = 'http') {
  1235. $url = trim($url);
  1236. $type = link_url_type($url);
  1237. if ($type === LINK_EXTERNAL) {
  1238. // Check if there is no protocol specified.
  1239. $protocol_match = preg_match("/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i", $url);
  1240. if (empty($protocol_match)) {
  1241. // But should there be? Add an automatic http:// if it starts with a
  1242. // domain name.
  1243. $link_domains = _link_domains();
  1244. $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\-_]*\.)+)(' . $link_domains . '|[a-z]{2}))/i', $url);
  1245. if (!empty($domain_match)) {
  1246. $url = $protocol . "://" . $url;
  1247. }
  1248. }
  1249. }
  1250. return $url;
  1251. }
  1252. /**
  1253. * Validates a URL.
  1254. *
  1255. * @param string $text
  1256. * Url to be validated.
  1257. * @param string $langcode
  1258. * An optional language code to look up the path in.
  1259. *
  1260. * @return bool
  1261. * True if a valid link, FALSE otherwise.
  1262. */
  1263. function link_validate_url($text, $langcode = NULL) {
  1264. $text = _link_clean_relative($text);
  1265. $text = link_cleanup_url($text);
  1266. $type = link_url_type($text);
  1267. if ($type && ($type == LINK_INTERNAL || $type == LINK_EXTERNAL)) {
  1268. $flag = valid_url($text, TRUE);
  1269. if (!$flag) {
  1270. $normal_path = drupal_get_normal_path($text, $langcode);
  1271. $parsed_link = parse_url($normal_path, PHP_URL_PATH);
  1272. if ($normal_path != $parsed_link) {
  1273. $normal_path = $parsed_link;
  1274. }
  1275. $flag = drupal_valid_path($normal_path);
  1276. }
  1277. if (!$flag) {
  1278. $flag = file_exists($normal_path);
  1279. }
  1280. if (!$flag) {
  1281. $uri = file_build_uri($normal_path);
  1282. $flag = file_exists($uri);
  1283. }
  1284. }
  1285. else {
  1286. $flag = (bool) $type;
  1287. }
  1288. return $flag;
  1289. }
  1290. /**
  1291. * Cleaner of relatives urls.
  1292. *
  1293. * @param string $url
  1294. * The url to clean up the relative protocol.
  1295. */
  1296. function _link_clean_relative($url) {
  1297. $check = substr($url, 0, 2);
  1298. if (isset($_SERVER['HTTPS']) &&
  1299. ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
  1300. isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
  1301. $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  1302. $protocol = 'https://';
  1303. }
  1304. else {
  1305. $protocol = 'http://';
  1306. }
  1307. if ($check == '//') {
  1308. $url = str_replace('//', $protocol, $url);
  1309. }
  1310. return $url;
  1311. }
  1312. /**
  1313. * Type check a URL.
  1314. *
  1315. * Accepts all URLs following RFC 1738 standard for URL formation and all e-mail
  1316. * addresses following the RFC 2368 standard for mailto address formation.
  1317. *
  1318. * @param string $text
  1319. * Url to be checked.
  1320. *
  1321. * @return mixed
  1322. * Returns boolean FALSE if the URL is not valid. On success, returns one of
  1323. * the LINK_(linktype) constants.
  1324. */
  1325. function link_url_type($text) {
  1326. // @TODO Complete letters.
  1327. // @codingStandardsIgnoreStart
  1328. $link_ichars_domain = (string) html_entity_decode(implode("", array(
  1329. "&#x00BF;", // ¿
  1330. "&#x00C0;", // À
  1331. "&#x00C1;", // Á
  1332. "&#x00C2;", // Â
  1333. "&#x00C3;", // Ã
  1334. "&#x00C4;", // Ä
  1335. "&#x00C5;", // Å
  1336. "&#x00C6;", // Æ
  1337. "&#x00C7;", // Ç
  1338. "&#x00C8;", // È
  1339. "&#x00C9;", // É
  1340. "&#x00CA;", // Ê
  1341. "&#x00CB;", // Ë
  1342. "&#x00CC;", // Ì
  1343. "&#x00CD;", // Í
  1344. "&#x00CE;", // Î
  1345. "&#x00CF;", // Ï
  1346. "&#x00D0;", // Ð
  1347. "&#x00D1;", // Ñ
  1348. "&#x00D2;", // Ò
  1349. "&#x00D3;", // Ó
  1350. "&#x00D4;", // Ô
  1351. "&#x00D5;", // Õ
  1352. "&#x00D6;", // Ö
  1353. // ×
  1354. "&#x00D8;", // Ø
  1355. "&#x00D9;", // Ù
  1356. "&#x00DA;", // Ú
  1357. "&#x00DB;", // Û
  1358. "&#x00DC;", // Ü
  1359. "&#x00DD;", // Ý
  1360. "&#x00DE;", // Þ
  1361. // ß (see LINK_ICHARS)
  1362. "&#x00E0;", // à
  1363. "&#x00E1;", // á
  1364. "&#x00E2;", // â
  1365. "&#x00E3;", // ã
  1366. "&#x00E4;", // ä
  1367. "&#x00E5;", // å
  1368. "&#x00E6;", // æ
  1369. "&#x00E7;", // ç
  1370. "&#x00E8;", // è
  1371. "&#x00E9;", // é
  1372. "&#x00EA;", // ê
  1373. "&#x00EB;", // ë
  1374. "&#x00EC;", // ì
  1375. "&#x00ED;", // í
  1376. "&#x00EE;", // î
  1377. "&#x00EF;", // ï
  1378. "&#x00F0;", // ð
  1379. "&#x00F1;", // ñ
  1380. "&#x00F2;", // ò
  1381. "&#x00F3;", // ó
  1382. "&#x00F4;", // ô
  1383. "&#x00F5;", // õ
  1384. "&#x00F6;", // ö
  1385. // ÷
  1386. "&#x00F8;", // ø
  1387. "&#x00F9;", // ù
  1388. "&#x00FA;", // ú
  1389. "&#x00FB;", // û
  1390. "&#x00FC;", // ü
  1391. "&#x00FD;", // ý
  1392. "&#x00FE;", // þ
  1393. "&#x00FF;", // ÿ
  1394. "&#x0152;", // Œ
  1395. "&#x0153;", // œ
  1396. "&#x0178;", // Ÿ
  1397. )), ENT_QUOTES, 'UTF-8');
  1398. // @codingStandardsIgnoreEnd
  1399. $link_ichars = $link_ichars_domain . (string) html_entity_decode(implode("", array(
  1400. // ß.
  1401. "&#x00DF;",
  1402. )), ENT_QUOTES, 'UTF-8');
  1403. $allowed_protocols = variable_get('filter_allowed_protocols', array(
  1404. 'http',
  1405. 'https',
  1406. 'ftp',
  1407. 'news',
  1408. 'nntp',
  1409. 'telnet',
  1410. 'mailto',
  1411. 'irc',
  1412. 'ssh',
  1413. 'sftp',
  1414. 'webcal',
  1415. ));
  1416. $link_domains = _link_domains();
  1417. // Starting a parenthesis group with (?: means that it is grouped, but is not
  1418. // captured.
  1419. $protocol = '((?:' . implode("|", $allowed_protocols) . '):\/\/)';
  1420. $authentication = "(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=" . $link_ichars . "]|%[0-9a-f]{2})+(?::(?:[\w" . $link_ichars . "\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})*)?)?@)";
  1421. $domain = '(?:(?:[a-z0-9' . $link_ichars_domain . ']([a-z0-9' . $link_ichars_domain . '\-_\[\]])*)(\.(([a-z0-9' . $link_ichars_domain . '\-_\[\]])+\.)*(' . $link_domains . '|[a-z]{2}))?)';
  1422. $ipv4 = '(?:[0-9]{1,3}(\.[0-9]{1,3}){3})';
  1423. $ipv6 = '(?:[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7})';
  1424. $port = '(?::([0-9]{1,5}))';
  1425. // Pattern specific to external links.
  1426. $external_pattern = '/^' . $protocol . '?' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
  1427. // Pattern specific to internal links.
  1428. $internal_pattern = "/^(?:[a-z0-9" . $link_ichars . "_\-+\[\] ]+)";
  1429. $internal_pattern_file = "/^(?:[a-z0-9" . $link_ichars . "_\-+\[\]\. \/\(\)][a-z0-9" . $link_ichars . "_\-+\[\]\. \(\)][a-z0-9" . $link_ichars . "_\-+\[\]\. \/\(\)]+)$/i";
  1430. $directories = "(?:\/[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'#!():;*@\[\]]*)*";
  1431. // Yes, four backslashes == a single backslash.
  1432. $query = "(?:\/?\?([?a-z0-9" . $link_ichars . "+_|\-\.~\/\\\\%=&,$'!():;*@\[\]{} ]*))";
  1433. $anchor = "(?:#[a-z0-9" . $link_ichars . "_\-\.~+%=&,$'():;*@\[\]\/\?]*)";
  1434. // The rest of the path for a standard URL.
  1435. // @codingStandardsIgnoreLine
  1436. $end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
  1437. $message_id = '[^@].*@' . $domain;
  1438. $newsgroup_name = '(?:[0-9a-z+-]*\.)*[0-9a-z+-]*';
  1439. $news_pattern = '/^news:(' . $newsgroup_name . '|' . $message_id . ')$/i';
  1440. $user = '[a-zA-Z0-9' . $link_ichars . '_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
  1441. $email_pattern = '/^mailto:' . $user . '@' . '(?:' . $domain . '|' . $ipv4 . '|' . $ipv6 . '|localhost)' . $query . '?$/';
  1442. if (strpos($text, '<front>') === 0) {
  1443. return LINK_FRONT;
  1444. }
  1445. if (in_array('mailto', $allowed_protocols) && preg_match($email_pattern, $text)) {
  1446. return LINK_EMAIL;
  1447. }
  1448. if (in_array('news', $allowed_protocols) && preg_match($news_pattern, $text)) {
  1449. return LINK_NEWS;
  1450. }
  1451. if (preg_match($internal_pattern . $end, $text)) {
  1452. return LINK_INTERNAL;
  1453. }
  1454. if (drupal_valid_path($text) && url_is_external($text) == FALSE) {
  1455. return LINK_INTERNAL;
  1456. }
  1457. if (preg_match($external_pattern . $end, $text)) {
  1458. return LINK_EXTERNAL;
  1459. }
  1460. if (preg_match($internal_pattern_file, $text)) {
  1461. return LINK_INTERNAL;
  1462. }
  1463. return FALSE;
  1464. }
  1465. /**
  1466. * Returns the list of allowed domains.
  1467. *
  1468. * If the variable link_allowed_domains is set, restrict allowed domains to the
  1469. * strings in that array. If the variable link_allowed_domains is not set, allow
  1470. * all domains between 2 and 63 characters in length.
  1471. * See https://tools.ietf.org/html/rfc1034.
  1472. */
  1473. function _link_domains() {
  1474. $link_allowed_domains = variable_get('link_allowed_domains', array());
  1475. return empty($link_allowed_domains) ? '[a-z][a-z0-9-]{1,62}' : implode('|', $link_allowed_domains);
  1476. }
  1477. /**
  1478. * Implements hook_migrate_field_alter().
  1479. */
  1480. function link_content_migrate_field_alter(&$field_value, $instance_value) {
  1481. if ($field_value['type'] == 'link') {
  1482. // Adjust the field type.
  1483. $field_value['type'] = 'link_field';
  1484. // Remove settings that are now on the instance.
  1485. foreach (array(
  1486. 'attributes',
  1487. 'display',
  1488. 'url',
  1489. 'title',
  1490. 'title_value',
  1491. 'enable_tokens',
  1492. 'validate_url',
  1493. ) as $setting) {
  1494. unset($field_value['settings'][$setting]);
  1495. }
  1496. }
  1497. }
  1498. /**
  1499. * Implements hook_migrate_instance_alter().
  1500. *
  1501. * Widget type also changed to link_field.
  1502. */
  1503. function link_content_migrate_instance_alter(&$instance_value, $field_value) {
  1504. if ($field_value['type'] == 'link') {
  1505. // Grab settings that were previously on the field.
  1506. foreach (array(
  1507. 'attributes',
  1508. 'display',
  1509. 'url',
  1510. 'title',
  1511. 'title_value',
  1512. 'enable_tokens',
  1513. 'validate_url',
  1514. ) as $setting) {
  1515. if (isset($field_value['settings'][$setting])) {
  1516. $instance_value['settings'][$setting] = $field_value['settings'][$setting];
  1517. }
  1518. }
  1519. // Adjust widget type.
  1520. if ($instance_value['widget']['type'] == 'link') {
  1521. $instance_value['widget']['type'] = 'link_field';
  1522. }
  1523. // Adjust formatter types.
  1524. foreach ($instance_value['display'] as $context => $settings) {
  1525. if (in_array($settings['type'], array(
  1526. 'default',
  1527. 'title_plain',
  1528. 'url',
  1529. 'plain',
  1530. 'short',
  1531. 'label',
  1532. 'separate',
  1533. ))) {
  1534. $instance_value['display'][$context]['type'] = 'link_' . $settings['type'];
  1535. }
  1536. }
  1537. }
  1538. }
  1539. /**
  1540. * Implements hook_field_settings_form().
  1541. */
  1542. function link_field_settings_form() {
  1543. return array();
  1544. }
  1545. /**
  1546. * Additional callback to adapt the property info of link fields.
  1547. *
  1548. * @see entity_metadata_field_entity_property_info()
  1549. */
  1550. function link_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  1551. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  1552. // Define a data structure so it's possible to deal with both the link title
  1553. // and URL.
  1554. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  1555. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  1556. // Auto-create the field item as soon as a property is set.
  1557. $property['auto creation'] = 'link_field_item_create';
  1558. $property['property info'] = link_field_item_property_info();
  1559. $property['property info']['url']['required'] = !$instance['settings']['url'];
  1560. $property['property info']['title']['required'] = ($instance['settings']['title'] == 'required');
  1561. if ($instance['settings']['title'] == 'none') {
  1562. unset($property['property info']['title']);
  1563. }
  1564. unset($property['query callback']);
  1565. }
  1566. /**
  1567. * Callback for creating a new, empty link field item.
  1568. *
  1569. * @see link_field_property_info_callback()
  1570. */
  1571. function link_field_item_create() {
  1572. return array('title' => NULL, 'url' => NULL, 'display_url' => NULL);
  1573. }
  1574. /**
  1575. * Defines info for the properties of the link-field item data structure.
  1576. */
  1577. function link_field_item_property_info() {
  1578. $properties['title'] = array(
  1579. 'type' => 'text',
  1580. 'label' => t('The title of the link.'),
  1581. 'setter callback' => 'entity_property_verbatim_set',
  1582. );
  1583. $properties['url'] = array(
  1584. 'type' => 'text',
  1585. 'label' => t('The URL of the link.'),
  1586. 'setter callback' => 'entity_property_verbatim_set',
  1587. );
  1588. $properties['attributes'] = array(
  1589. 'type' => 'struct',
  1590. 'label' => t('The attributes of the link.'),
  1591. 'setter callback' => 'entity_property_verbatim_set',
  1592. 'getter callback' => 'link_attribute_property_get',
  1593. );
  1594. $properties['display_url'] = array(
  1595. 'type' => 'uri',
  1596. 'label' => t('The full URL of the link.'),
  1597. 'setter callback' => 'entity_property_verbatim_set',
  1598. );
  1599. return $properties;
  1600. }
  1601. /**
  1602. * Entity property info getter callback for link attributes.
  1603. */
  1604. function link_attribute_property_get($data, array $options, $name, $type, $info) {
  1605. return isset($data[$name]) ? array_filter($data[$name]) : array();
  1606. }
  1607. /**
  1608. * Implements hook_field_update_instance().
  1609. */
  1610. function link_field_update_instance($instance, $prior_instance) {
  1611. if (function_exists('i18n_string_update') && isset($instance['widget']) && $instance['widget']['type'] == 'link_field' && $prior_instance['settings']['title_value'] != $instance['settings']['title_value']) {
  1612. $i18n_string_name = "field:{$instance['field_name']}:{$instance['bundle']}:title_value";
  1613. i18n_string_update($i18n_string_name, $instance['settings']['title_value']);
  1614. }
  1615. }
  1616. /**
  1617. * Implements hook_i18n_string_list_TEXTGROUP_alter().
  1618. */
  1619. function link_i18n_string_list_field_alter(&$strings, $type = NULL, $object = NULL) {
  1620. if ($type != 'field_instance' || !is_array($object) || !isset($object['widget']['type'])) {
  1621. return;
  1622. }
  1623. if ($object['widget']['type'] == 'link_field' && isset($object['settings']['title_value'])) {
  1624. $strings['field'][$object['field_name']][$object['bundle']]['title_value']['string'] = $object['settings']['title_value'];
  1625. }
  1626. }