linkit.field.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * Process callback.
  155. */
  156. function linkit_process_field_element($element, &$form_state, &$complete_form) {
  157. // Only proceed if the field is attached to an entity.
  158. if (!isset($element['#entity_type'])) {
  159. return $element;
  160. }
  161. $field = field_info_field($element['#field_name']);
  162. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  163. if (isset($instance['settings']['linkit']['enable']) && $instance['settings']['linkit']['enable']) {
  164. // Load the profile.
  165. $profile = linkit_profile_load($instance['settings']['linkit']['profile']);
  166. if (!$profile) {
  167. return $element;
  168. }
  169. // Load the insert plugin for the profile.
  170. $insert_plugin = linkit_insert_plugin_load($profile->data['insert_plugin']['plugin']);
  171. if ($insert_plugin === NULL) {
  172. return $element;
  173. }
  174. // Set the field ID.
  175. $field_id = $element['#id'];
  176. // Special treatment for link fields.
  177. if ($element['#type'] == 'link_field') {
  178. $field_id = $element['#id'] . '-url';
  179. }
  180. $field_js = array(
  181. 'data' => array(
  182. 'linkit' => array(
  183. 'fields' => array(
  184. $field_id => array(
  185. 'profile' => $instance['settings']['linkit']['profile'],
  186. 'insert_plugin' => $profile->data['insert_plugin']['plugin'],
  187. 'url_method' => $profile->data['insert_plugin']['url_method'],
  188. // @TODO: Add autocomplete settings.
  189. ),
  190. ),
  191. ),
  192. ),
  193. 'type' => 'setting',
  194. );
  195. // Link fields can have a title field.
  196. if ($element['#type'] == 'link_field') {
  197. if (isset($instance['settings']['title']) && in_array($instance['settings']['title'], array('optional', 'required'))) {
  198. $field_js['data']['linkit']['fields'][$field_id]['title_field'] = $element['#id'] . '-title';
  199. }
  200. }
  201. // Attach js files and settings Linkit needs.
  202. $element['#attached']['library'][] = array('linkit', 'base');
  203. $element['#attached']['library'][] = array('linkit', 'field');
  204. $element['#attached']['js'][] = $insert_plugin['javascript'];
  205. $element['#attached']['js'][] = $field_js;
  206. $button_text = !empty($instance['settings']['linkit']['button_text']) ? $instance['settings']['linkit']['button_text'] : t('Search');
  207. // Add Linkit dialog button to the element suffix.
  208. $element['#field_suffix'] = '<a class="button linkit-field-button linkit-field-' . $field_id . '" href="#">' . $button_text . '</a>';
  209. }
  210. return $element;
  211. }