tripal_fields_layout.module 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. *
  4. * Implements hook_form_FORM_ID_alter().
  5. *
  6. * The field_ui_field_edit_form is used for customizing the settings of
  7. * a field attached to an entity.
  8. */
  9. function tripal_fields_layout_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  10. // For entity fields added by Tripal Entities we don't want the
  11. // the end-user to change the cardinality and the required fields
  12. // such that record can't be saved in Chado.
  13. $dbs = tripal_fields_layout_get_db_names_for_published_vocabularies();
  14. if (in_array($form['#instance']['entity_type'], $dbs)) {
  15. $form['field']['cardinality']['#access'] = FALSE;
  16. $form['instance']['required']['#access'] = FALSE;
  17. }
  18. // TODO: don't the the maximum length be larger than the field size.
  19. }
  20. /**
  21. * Implements hook_form_FORM_ID_alter().
  22. *
  23. * The field_ui_display_overview_form is used for formatting the display
  24. * or layout of fields attached to an entity.
  25. */
  26. function tripal_fields_layout_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  27. $entity_type = $form['#entity_type'];
  28. $bundle_name = $form['#bundle'];
  29. // Get the bundle record.
  30. $bundle = db_select('tripal_bundle', 'tb')
  31. ->fields('tb')
  32. ->condition('bundle', $bundle_name)
  33. ->execute()
  34. ->fetchObject();
  35. if (module_exists('ds')) {
  36. drupal_set_message('Tripal is not compatible with the Display Suite (ds)
  37. module. If you would like to use Tripal-style panels for the layout
  38. of your pages please disable the Display Suite module. If you
  39. prefer to use the Display Suite module then disable the Tripal
  40. Fields Layout (tripal_fields_layout) module.', 'warning');
  41. }
  42. // Add a vertical tab fieldset at the bottom of the
  43. $form['overview_vert_tabs'] = array(
  44. '#type' => 'vertical_tabs'
  45. );
  46. $form['modes']['#group'] = 'overview_vert_tabs';
  47. $form['modes']['#weight'] = 1000;
  48. $form['te_add_panels'] = array(
  49. '#type' => 'fieldset',
  50. '#title' => 'Add a Panel',
  51. '#collapsible' => TRUE,
  52. '#collapsed' => TRUE,
  53. '#group' => 'overview_vert_tabs'
  54. );
  55. $form['te_add_panels']['instructions'] = array(
  56. '#type' => 'item',
  57. '#markup' => t('You may add as many panels to your page layout as
  58. desired. Panels can be used to organize fields into categories....')
  59. );
  60. $form['te_add_panels']['panel_name'] = array(
  61. '#type' => 'textfield',
  62. '#title' => 'Panel Name',
  63. '#description' => t('Please provide a computer readable name for this
  64. panel. The name should only contain alphanumeric values and
  65. underscores. It must not begin with a number.')
  66. );
  67. $form['te_add_panels']['panel_label'] = array(
  68. '#type' => 'textfield',
  69. '#title' => 'Panel Label',
  70. '#description' => t('Please provide a human readable label for this
  71. panel. This is the name that will appear to site visitors.')
  72. );
  73. $form['te_add_panels']['message'] = array(
  74. '#type' => 'textarea',
  75. '#title' => 'Empty Message',
  76. '#rows' => 2,
  77. '#description' => t('When the panel has no fields the following
  78. message will be shown in the form above.')
  79. );
  80. $form['te_add_panels']['add_button'] = array(
  81. '#type' => 'submit',
  82. '#value' => 'Add Panel',
  83. '#name' => 'add-panel-submit'
  84. );
  85. $form['te_layout_panels'] = array(
  86. '#type' => 'fieldset',
  87. '#title' => 'Order Panels',
  88. '#collapsible' => TRUE,
  89. '#collapsed' => TRUE,
  90. '#group' => 'overview_vert_tabs'
  91. );
  92. $form['te_configure_panels'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => 'Configure Panels',
  95. '#collapsible' => TRUE,
  96. '#collapsed' => TRUE,
  97. '#group' => 'overview_vert_tabs'
  98. );
  99. // Make sure our default panels are in the database.
  100. _tripal_fields_layout_check_default_field_panels($bundle);
  101. // Now add each panel as a region.
  102. $form['fields']['#regions'] = array();
  103. $panels = db_select('tripal_panels', 'tp')
  104. ->fields('tp')
  105. ->condition('bundle_id', $bundle->id)
  106. ->orderBy('weight', 'ASC')
  107. ->orderBy('label', 'ASC')
  108. ->execute();
  109. $panel_options = array();
  110. while ($panel = $panels->fetchObject()) {
  111. $settings = unserialize($panel->settings);
  112. $form['fields']['#regions'][$panel->name] = array(
  113. 'title' => t($panel->label),
  114. 'message' => t($settings['message']),
  115. );
  116. $panel_options[$panel->name] = $panel->label;
  117. }
  118. // Set the table headers to add a new 'region' column.
  119. $form['fields']['#header'] = array(
  120. t('Field'),
  121. t('Weight'),
  122. t('Parent'),
  123. t('Label'),
  124. array('data' => t('Format'), 'colspan' => 3),
  125. t('Region'),
  126. );
  127. // Change the region callback for each field to place each field in the
  128. // proper "panel" region. Also, set the default region to be the base region.
  129. $fields = $form['fields'];
  130. foreach (element_children($fields) as $field_name) {
  131. $form['fields'][$field_name]['#region_callback'] = 'tripal_fields_layout_field_ui_row_region';
  132. $form['fields'][$field_name]['region'] = array(
  133. '#type' => 'select',
  134. '#options' => $panel_options,
  135. '#default_value' => 'te_base',
  136. '#attributes' => array(
  137. 'class' => array('te-field-region'),
  138. )
  139. );
  140. }
  141. // Add validate and submit handlers. Rearrange the submit callbacks
  142. // so ours is first.
  143. $form['#validate'][] = 'tripal_fields_layout_field_ui_validate';
  144. $submit = $form['#submit'];
  145. $form['#submit'] = array('tripal_fields_layout_field_ui_submit');
  146. $form['#submit'] = array_merge($form['#submit'], $submit);
  147. }
  148. /**
  149. * A helper function for checking if the default panels are in the database.
  150. */
  151. function _tripal_fields_layout_check_default_field_panels($bundle) {
  152. // Make sure we have records for our default regions: te_base and te_hidden.
  153. // First check if the base region is in the database. If not, add it.
  154. $te_base = db_select('tripal_panels', 'tp')
  155. ->fields('tp')
  156. ->condition('name', 'te_base')
  157. ->execute()
  158. ->fetchObject();
  159. if (!$te_base) {
  160. $settings = array(
  161. 'message' => 'Fields that should appear at the top of each page should be placed in the base panel. These fields are always present and help orient the user by at least indicating the necessary information to uniquely identiy the content.',
  162. );
  163. db_insert('tripal_panels')
  164. ->fields(array(
  165. 'bundle_id' => $bundle->id,
  166. 'name' => 'te_base',
  167. 'label' => 'Base Content',
  168. 'settings' => serialize($settings),
  169. 'weight' => -9999999
  170. ))
  171. ->execute();
  172. }
  173. // Next check if the hidden region is in the database. If not, add it.
  174. $te_base = db_select('tripal_panels', 'tp')
  175. ->fields('tp')
  176. ->condition('name', 'te_disabled')
  177. ->execute()
  178. ->fetchObject();
  179. if (!$te_base) {
  180. $settings = array(
  181. 'message' => 'Place field here to hide them from display.',
  182. );
  183. db_insert('tripal_panels')
  184. ->fields(array(
  185. 'bundle_id' => $bundle->id,
  186. 'name' => 'te_disabled',
  187. 'label' => 'Disabled',
  188. 'settings' => serialize($settings),
  189. 'weight' => 9999999
  190. ))
  191. ->execute();
  192. }
  193. }
  194. /**
  195. * Returns the region to which a row on the Field UI page belongs.
  196. *
  197. * @param $row
  198. * The current row that is being rendered in the Field UI page.
  199. */
  200. function tripal_fields_layout_field_ui_row_region($row) {
  201. $default_panel = 'te_base';
  202. $panel = '';
  203. $field_name = $row['#parents'][1];
  204. $field = field_info_field($field_name);
  205. // First get the panel
  206. if (!$panel) {
  207. $panel = $default_panel;
  208. }
  209. // See if there is a record in the tripal_panel_fields for this field.
  210. return $default_panel;
  211. }
  212. /**
  213. * Validates the field UI form to ensure proper panel assignments.
  214. *
  215. * @param $form
  216. * @param $form_state
  217. */
  218. function tripal_fields_layout_field_ui_validate($form, &$form_state) {
  219. }
  220. /**
  221. * Responds to a submit from the field UI form for saving panel assignments.
  222. *
  223. * @param $form
  224. * @param $form_state
  225. */
  226. function tripal_fields_layout_field_ui_submit($form, &$form_state) {
  227. }