linkit.field.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @file
  4. * Implementation for Fields and Linkit.
  5. */
  6. /**
  7. * Implements hook_linkit_allowed_field_types().
  8. */
  9. function linkit_linkit_allowed_field_types() {
  10. $allowed_field_types = array(
  11. 'text',
  12. 'text_long',
  13. 'text_with_summary',
  14. 'link_field',
  15. );
  16. return $allowed_field_types;
  17. }
  18. /**
  19. * Get all allowed filed types.
  20. * @return
  21. * An array of allowed filed types.
  22. */
  23. function linkit_get_allowed_field_types() {
  24. // Get allowed field and widget types.
  25. $allowed_field_types = module_invoke_all('linkit_allowed_field_types');
  26. drupal_alter('linkit_allowed_field_types', $allowed_field_types);
  27. return $allowed_field_types;
  28. }
  29. /**
  30. * Implements hook_linkit_allowed_elements().
  31. */
  32. function linkit_linkit_allowed_field_elements() {
  33. $allowed_elements = array(
  34. 'textfield',
  35. 'textarea',
  36. 'textarea_with_summary',
  37. 'link_field',
  38. );
  39. return $allowed_elements;
  40. }
  41. /**
  42. * Get all allowed filed elements.
  43. * @return
  44. * An array of allowed filed elements.
  45. */
  46. function linkit_get_allowed_field_elements() {
  47. // Get allowed field and widget types.
  48. $allowed_field_elements = module_invoke_all('linkit_allowed_field_elements');
  49. drupal_alter('linkit_allowed_field_elements', $allowed_field_elements);
  50. return $allowed_field_elements;
  51. }
  52. /**
  53. * Implements hook_linkit_allowed_field_widget_types().
  54. */
  55. function linkit_linkit_allowed_field_widget_types() {
  56. $allowed_field_widget_types = array(
  57. 'text_textfield',
  58. 'text_textarea',
  59. 'text_textarea_with_summary',
  60. 'link_field',
  61. );
  62. return $allowed_field_widget_types;
  63. }
  64. /**
  65. * Get all allowed filed widget types.
  66. * @return
  67. * An array of allowed filed widget types.
  68. */
  69. function linkit_get_allowed_field_widget_types() {
  70. // Get allowed field and widget types.
  71. $allowed_field_widget_types = module_invoke_all('linkit_allowed_field_widget_types');
  72. drupal_alter('linkit_allowed_field_widget_types', $allowed_field_widget_types);
  73. return $allowed_field_widget_types;
  74. }
  75. /**
  76. * Implements hook_form_FIELD_UI_FIELD_EDIT_FORM_alter().
  77. */
  78. function linkit_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  79. $instance = $form['#instance'];
  80. if (isset($form['locked']) && $form['locked']) {
  81. return;
  82. }
  83. // Get allowed field types.
  84. $allowed_field_types = linkit_get_allowed_field_types();
  85. // Get allowed field widget types.
  86. $allowed_field_widget_types = linkit_get_allowed_field_widget_types();
  87. $allowed_field = in_array($form['#field']['type'], $allowed_field_types);
  88. $allowed_widget = in_array($form['instance']['widget']['type']['#value'], $allowed_field_widget_types);
  89. // Add the linkit settings to the field instance form if it's valid.
  90. if ($allowed_field && $allowed_widget) {
  91. // Fieldset for Linkit settings on this field instance.
  92. $form['instance']['settings']['linkit'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => t('Linkit field settings'),
  95. '#collapsible' => FALSE,
  96. '#collapsed' => FALSE,
  97. );
  98. // Enable Linkit on this field instance.
  99. $form['instance']['settings']['linkit']['enable'] = array(
  100. '#type' => 'checkbox',
  101. '#title' => t('Enable Linkit for this field.'),
  102. '#default_value' => isset($instance['settings']['linkit']['enable'])
  103. ? $instance['settings']['linkit']['enable'] : 0,
  104. '#description' => t('Do not use this for CKeditor and TinyMCE fields. You will have to configure this on the wysiwyg/ckeditor profile.'),
  105. );
  106. $profiles = linkit_profile_field_load_all();
  107. $options = array();
  108. foreach ($profiles as $profile) {
  109. $options[$profile->name] = $profile->admin_title;
  110. }
  111. // Sort the options.
  112. natsort($options);
  113. $form['instance']['settings']['linkit']['profile'] = array(
  114. '#type' => 'select',
  115. '#title' => t('Profile'),
  116. '#options' => $options,
  117. '#empty_option' => t('- Select a profile -'),
  118. '#default_value' => isset($instance['settings']['linkit']['profile'])
  119. ? $instance['settings']['linkit']['profile'] : '',
  120. '#states' => array(
  121. 'invisible' => array(
  122. 'input[name="instance[settings][linkit][enable]"]' => array('checked' => FALSE),
  123. ),
  124. 'required' => array(
  125. 'input[name="instance[settings][linkit][enable]"]' => array('checked' => TRUE),
  126. ),
  127. ),
  128. '#element_validate' => array('linkit_field_profile_validate'),
  129. );
  130. // Enable Linkit on this field instance.
  131. $form['instance']['settings']['linkit']['button_text'] = array(
  132. '#type' => 'textfield',
  133. '#title' => t('Button text that activates linkit modal.'),
  134. '#default_value' => isset($instance['settings']['linkit']['button_text'])
  135. ? $instance['settings']['linkit']['button_text'] : t('Search'),
  136. );
  137. }
  138. }
  139. /**
  140. * Validation callback; Only validate the profile field if linkit is enabled on
  141. * the instance.
  142. *
  143. * @see linkit_form_field_ui_field_edit_form_alter()
  144. */
  145. function linkit_field_profile_validate($element, &$form_state, $form) {
  146. if (isset($form_state['values']['instance']['settings']['linkit']['enable'])
  147. && $form_state['values']['instance']['settings']['linkit']['enable']) {
  148. if (empty($element['#value'])) {
  149. form_error($element, t('You must select an profile.'));
  150. }
  151. }
  152. }
  153. /**
  154. * After build callback.
  155. *
  156. * @param array $element
  157. * Form API element.
  158. * @param array $form_state
  159. * State of form the element belongs to.
  160. *
  161. * @return array
  162. * Form API element with attached Linkit functionality.
  163. */
  164. function linkit_field_element_after_build(array $element, array &$form_state) {
  165. // Only proceed if the field is attached to an entity.
  166. if (!isset($element['#entity_type'])) {
  167. return $element;
  168. }
  169. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  170. if (empty($instance['settings']['linkit']['enable'])) {
  171. return $element;
  172. }
  173. // Load the profile.
  174. /* @var \LinkitProfile $profile */
  175. $profile = linkit_profile_load($instance['settings']['linkit']['profile']);
  176. if (!$profile || !isset($profile->data['insert_plugin']['plugin'])) {
  177. return $element;
  178. }
  179. // Load the insert plugin for the profile.
  180. $insert_plugin = linkit_insert_plugin_load($profile->data['insert_plugin']['plugin']);
  181. $js_settings = array(
  182. 'helper' => 'field',
  183. 'source' => $element['#id'],
  184. 'profile' => $instance['settings']['linkit']['profile'],
  185. 'insertPlugin' => $profile->data['insert_plugin']['plugin'],
  186. );
  187. // Special treatment for link fields.
  188. if ('link_field' == $element['#type']) {
  189. $js_settings['source'] = $element['url']['#id'];
  190. // @see link_field_info()
  191. // @see link_field_instance_settings_form()
  192. //
  193. // Link fields have a title field, but value could
  194. // be changed only for those options.
  195. if (in_array($instance['settings']['title'], array('optional', 'required'))) {
  196. $js_settings['titleField'] = $element['title']['#id'];
  197. }
  198. }
  199. // Add Linkit dialog button to the element suffix.
  200. $element['#field_suffix'] = l(empty($instance['settings']['linkit']['button_text']) ? t('Search') : $instance['settings']['linkit']['button_text'], '', array(
  201. 'attributes' => array(
  202. 'class' => array(
  203. "button",
  204. "linkit-field-button",
  205. "linkit-field-{$js_settings['source']}",
  206. ),
  207. ),
  208. ));
  209. // Attach js files and settings Linkit needs.
  210. $element['#attached']['library'][] = array('linkit', 'base');
  211. $element['#attached']['library'][] = array('linkit', 'field');
  212. $element['#attached']['js'][] = $insert_plugin['javascript'];
  213. $element['#attached']['js'][] = array(
  214. 'type' => 'setting',
  215. 'data' => array(
  216. 'linkit' => array('fields' => array($js_settings)),
  217. ),
  218. );
  219. return $element;
  220. }