tripal_fields_layout.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. // Layout Panels
  86. $form['te_layout_panels'] = array(
  87. '#type' => 'fieldset',
  88. '#title' => 'Order Panels',
  89. '#collapsible' => TRUE,
  90. '#collapsed' => TRUE,
  91. '#group' => 'overview_vert_tabs'
  92. );
  93. $form['te_layout_panels']['instructions'] = array(
  94. '#type' => 'item',
  95. '#markup' => t('Drag and drop the panel to change its order.')
  96. );
  97. $form['te_layout_panels']['panel_items']['#tree'] = TRUE;
  98. // Get available panels
  99. $result = db_query('SELECT panel_id, name, label, weight FROM {tripal_panels} ORDER BY weight ASC');
  100. foreach ($result as $item) {
  101. $form['te_layout_panels']['panel_items'][$item->panel_id] = array(
  102. 'name' => array(
  103. '#markup' => check_plain($item->name),
  104. ),
  105. 'label' => array(
  106. '#markup' => check_plain($item->label),
  107. ),
  108. 'weight' => array(
  109. '#type' => 'weight',
  110. '#title' => t('Weight'),
  111. '#default_value' => $item->weight,
  112. '#delta' => 50,
  113. '#title_display' => 'invisible',
  114. ),
  115. );
  116. }
  117. $form['te_layout_panels']['panel_items']['#theme_wrappers'] = array('tripal_fields_layout_form_draggable_panel_table');
  118. // Now we add our submit button, for submitting the form results.
  119. //
  120. // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
  121. // but is included as a Form API recommended practice.
  122. $form['te_layout_panels']['add_button'] = array(
  123. '#type' => 'submit',
  124. '#value' => 'Save Panel Order',
  125. '#name' => 'order-panel-submit'
  126. );
  127. // Configure Panels
  128. $form['te_configure_panels'] = array(
  129. '#type' => 'fieldset',
  130. '#title' => 'Configure Panels',
  131. '#collapsible' => TRUE,
  132. '#collapsed' => TRUE,
  133. '#group' => 'overview_vert_tabs'
  134. );
  135. // Make sure our default panels are in the database.
  136. _tripal_fields_layout_check_default_field_panels($bundle);
  137. // Now add each panel as a region.
  138. $form['fields']['#regions'] = array();
  139. $panels = db_select('tripal_panels', 'tp')
  140. ->fields('tp')
  141. ->condition('bundle_id', $bundle->id)
  142. ->orderBy('weight', 'ASC')
  143. ->orderBy('label', 'ASC')
  144. ->execute();
  145. $panel_options = array();
  146. while ($panel = $panels->fetchObject()) {
  147. $settings = unserialize($panel->settings);
  148. $form['fields']['#regions'][$panel->name] = array(
  149. 'title' => t($panel->label),
  150. 'message' => t($settings['message']),
  151. );
  152. $panel_options[$panel->name] = $panel->label;
  153. }
  154. // Set the table headers to add a new 'region' column.
  155. $form['fields']['#header'] = array(
  156. t('Field'),
  157. t('Weight'),
  158. t('Parent'),
  159. t('Label'),
  160. array('data' => t('Format'), 'colspan' => 3),
  161. t('Region'),
  162. );
  163. // Change the region callback for each field to place each field in the
  164. // proper "panel" region. Also, set the default region to be the base region.
  165. $fields = $form['fields'];
  166. foreach (element_children($fields) as $field_name) {
  167. $form['fields'][$field_name]['#region_callback'] = 'tripal_fields_layout_field_ui_row_region';
  168. $form['fields'][$field_name]['region'] = array(
  169. '#type' => 'select',
  170. '#options' => $panel_options,
  171. '#default_value' => 'te_base',
  172. '#attributes' => array(
  173. 'class' => array('te-field-region'),
  174. )
  175. );
  176. }
  177. // Add validate and submit handlers. Rearrange the submit callbacks
  178. // so ours is first.
  179. $form['#validate'][] = 'tripal_fields_layout_field_ui_validate';
  180. $submit = $form['#submit'];
  181. $form['#submit'] = array('tripal_fields_layout_field_ui_submit');
  182. $form['#submit'] = array_merge($form['#submit'], $submit);
  183. }
  184. /**
  185. * A helper function for checking if the default panels are in the database.
  186. */
  187. function _tripal_fields_layout_check_default_field_panels($bundle) {
  188. // Make sure we have records for our default regions: te_base and te_hidden.
  189. // First check if the base region is in the database. If not, add it.
  190. $te_base = db_select('tripal_panels', 'tp')
  191. ->fields('tp')
  192. ->condition('name', 'te_base')
  193. ->execute()
  194. ->fetchObject();
  195. if (!$te_base) {
  196. $settings = array(
  197. '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.',
  198. );
  199. db_insert('tripal_panels')
  200. ->fields(array(
  201. 'bundle_id' => $bundle->id,
  202. 'name' => 'te_base',
  203. 'label' => 'Base Content',
  204. 'settings' => serialize($settings),
  205. 'weight' => -9999999
  206. ))
  207. ->execute();
  208. }
  209. // Next check if the hidden region is in the database. If not, add it.
  210. $te_base = db_select('tripal_panels', 'tp')
  211. ->fields('tp')
  212. ->condition('name', 'te_disabled')
  213. ->execute()
  214. ->fetchObject();
  215. if (!$te_base) {
  216. $settings = array(
  217. 'message' => 'Place field here to hide them from display.',
  218. );
  219. db_insert('tripal_panels')
  220. ->fields(array(
  221. 'bundle_id' => $bundle->id,
  222. 'name' => 'te_disabled',
  223. 'label' => 'Disabled',
  224. 'settings' => serialize($settings),
  225. 'weight' => 9999999
  226. ))
  227. ->execute();
  228. }
  229. }
  230. /**
  231. * Returns the region to which a row on the Field UI page belongs.
  232. *
  233. * @param $row
  234. * The current row that is being rendered in the Field UI page.
  235. */
  236. function tripal_fields_layout_field_ui_row_region($row) {
  237. $default_panel = 'te_base';
  238. $panel = '';
  239. // Get field
  240. $field_name = $row['#parents'][1];
  241. $field = field_info_field($field_name);
  242. // Get field instance
  243. $field_id = $field['id'];
  244. $bundle_keys = array_keys($field['bundles']);
  245. $entity_type = $bundle_keys[0];
  246. $bundle = $field['bundles'][$entity_type][0];
  247. $field_instance = field_info_instance($entity_type, $field_name, $bundle);
  248. // Get panel_id
  249. $panel_id = db_select('tripal_panel_fields', 'tpf')
  250. ->fields('tpf', array('panel_id'))
  251. ->condition('field_id', $field_instance['id'])
  252. ->execute()
  253. ->fetchField();
  254. // Get panel name
  255. if ($panel_id) {
  256. $panel = db_select('tripal_panels', 'tp')
  257. ->fields('tp', array('name'))
  258. ->condition('panel_id', $panel_id)
  259. ->execute()
  260. ->fetchField();
  261. }
  262. // First get the panel
  263. if (!$panel) {
  264. $panel = $default_panel;
  265. }
  266. // See if there is a record in the tripal_panel_fields for this field.
  267. return $panel;
  268. }
  269. /**
  270. * Validates the field UI form to ensure proper panel assignments.
  271. *
  272. * @param $form
  273. * @param $form_state
  274. */
  275. function tripal_fields_layout_field_ui_validate($form, &$form_state) {
  276. if ($form_state ['clicked_button'] ['#name'] == 'add-panel-submit') {
  277. // Check if a valide panel name is provided
  278. $name = $form_state ['values'] ['panel_name'];
  279. if (! $name) {
  280. form_set_error ( 'panel_name', t ( "Please provide a name for the new panel." ) );
  281. }
  282. else if (preg_match ( '/^\d+/', $name )) {
  283. form_set_error ( 'panel_name', t ( "Panel name must not begin with a number." ) );
  284. }
  285. else if (preg_match ( '/\s+/', $name )) {
  286. form_set_error ( 'panel_name', t ( "Panel name should only contain alphanumeric values and underscores." ) );
  287. }
  288. // Check if a panel label is provided
  289. $label = $form_state ['values'] ['panel_label'];
  290. if (! $label) {
  291. form_set_error ( 'panel_label', t ( "Please provide a label for the new panel." ) );
  292. }
  293. }
  294. else if ($form_state ['clicked_button'] ['#name'] == 'order-panel-submit') {
  295. }
  296. else if ($form_state ['clicked_button'] ['#name'] == 'op') {
  297. }
  298. }
  299. /**
  300. * Responds to a submit from the field UI form for saving panel assignments.
  301. *
  302. * @param $form
  303. * @param $form_state
  304. */
  305. function tripal_fields_layout_field_ui_submit($form, &$form_state) {
  306. // Add a new panel
  307. if ($form_state ['clicked_button'] ['#name'] == 'add-panel-submit') {
  308. $bundle_id = $form_state['build_info']['args'][1]->id;
  309. $name = $form_state['values']['panel_name'];
  310. $label = $form_state['values']['panel_label'];
  311. $message = $form_state['values']['message'];
  312. $settings = array(
  313. 'message' => $message,
  314. );
  315. db_insert('tripal_panels')
  316. ->fields(array(
  317. 'bundle_id' => $bundle_id,
  318. 'name' => $name,
  319. 'label' => $label,
  320. 'settings' => serialize($settings),
  321. 'weight' => 0
  322. ))
  323. ->execute();
  324. }
  325. // Order panel
  326. else if ($form_state ['clicked_button'] ['#name'] == 'order-panel-submit') {
  327. $panels = $form_state['values']['panel_items'];
  328. foreach ($panels AS $id => $panel) {
  329. db_query(
  330. 'UPDATE {tripal_panels} SET weight = :weight WHERE panel_id = :id',
  331. array(
  332. ':weight' => $panel['weight'],
  333. ':id' => $id
  334. )
  335. );
  336. }
  337. }
  338. // Save field regions
  339. else if ($form_state ['clicked_button'] ['#name'] == 'op') {
  340. $fields = $form_state['values']['fields'];
  341. foreach($fields AS $field_name => $field_data){
  342. // Get field instance id
  343. $field = field_info_field($field_name);
  344. $field_id = $field['id'];
  345. $bundle_keys = array_keys($field['bundles']);
  346. $entity_type = $bundle_keys[0];
  347. $bundle = $field['bundles'][$entity_type][0];
  348. $field_instance = field_info_instance($entity_type, $field_name, $bundle);
  349. $field_instance_id = $field_instance['id'];
  350. // Get region panel_id
  351. $region = $field_data['region'];
  352. $panel_id = db_select('tripal_panels', 'tp')
  353. ->fields('tp', array('panel_id'))
  354. ->condition('name', $region)
  355. ->execute()
  356. ->fetchField();
  357. // Save
  358. $penal_field_id = db_select('tripal_panel_fields', 'tpf')
  359. ->fields('tpf', array('panel_field_id'))
  360. ->condition('field_id', $field_instance_id)
  361. ->condition('panel_id', $panel_id)
  362. ->execute()
  363. ->fetchField();
  364. if ($penal_field_id) {
  365. db_query(
  366. 'UPDATE tripal_panel_fields SET panel_id = :panel_id WHERE panel_field_id = :panel_field_id',
  367. array(
  368. ':panel_id' => $panel_id,
  369. ':panel_field_id' => $penal_field_id
  370. )
  371. )
  372. ->execute();
  373. }
  374. else {
  375. db_insert('tripal_panel_fields')
  376. ->fields(array(
  377. 'panel_id' => $panel_id,
  378. 'field_id' => $field_instance_id
  379. ))
  380. ->execute();
  381. }
  382. }
  383. }
  384. }
  385. /**
  386. * Theme the Panel Order Table as a draggable table
  387. *
  388. * @param unknown $variables
  389. * @return unknown
  390. */
  391. function theme_tripal_fields_layout_form_draggable_panel_table ($variables) {
  392. $element = $variables['element'];
  393. $rows = array();
  394. foreach (element_children($element) as $id) {
  395. // Before we add our 'weight' column to the row, we need to give the
  396. // element a custom class so that it can be identified in the
  397. // drupal_add_tabledrag call.
  398. //
  399. // This could also have been done during the form declaration by adding
  400. // '#attributes' => array('class' => 'example-item-weight'),
  401. // directy to the 'weight' element in tabledrag_example_simple_form().
  402. $element[$id]['weight']['#attributes']['class'] = array('panel-item-weight');
  403. $element[$id]['name']['#printed'] = FALSE;
  404. $element[$id]['label']['#printed'] = FALSE;
  405. $element[$id]['weight']['#printed'] = FALSE;
  406. // We are now ready to add each element of our $form data to the $rows
  407. // array, so that they end up as individual table cells when rendered
  408. // in the final table. We run each element through the drupal_render()
  409. // function to generate the final html markup for that element.
  410. $rows[] = array(
  411. 'data' => array(
  412. // Add our 'name' column.
  413. drupal_render($element[$id]['name']),
  414. // Add our 'description' column.
  415. drupal_render($element[$id]['label']),
  416. // Add our 'weight' column.
  417. drupal_render($element[$id]['weight']),
  418. ),
  419. // To support the tabledrag behaviour, we need to assign each row of the
  420. // table a class attribute of 'draggable'. This will add the 'draggable'
  421. // class to the <tr> element for that row when the final table is
  422. // rendered.
  423. 'class' => array('draggable'),
  424. );
  425. }
  426. // We now define the table header values. Ensure that the 'header' count
  427. // matches the final column count for your table.
  428. $header = array(t('Name'), t('Label'), t('Weight'));
  429. // We also need to pass the drupal_add_tabledrag() function an id which will
  430. // be used to identify the <table> element containing our tabledrag form.
  431. // Because an element's 'id' should be unique on a page, make sure the value
  432. // you select is NOT the same as the form ID used in your form declaration.
  433. $table_id = 'panel-items-table';
  434. // We can render our tabledrag table for output.
  435. $output = theme('table', array(
  436. 'header' => $header,
  437. 'rows' => $rows,
  438. 'attributes' => array('id' => $table_id),
  439. ));
  440. // And then render any remaining form elements (such as our submit button).
  441. //$output .= drupal_render_children($element);
  442. // We now call the drupal_add_tabledrag() function in order to add the
  443. // tabledrag.js goodness onto our page.
  444. //
  445. // For a basic sortable table, we need to pass it:
  446. // - the $table_id of our <table> element,
  447. // - the $action to be performed on our form items ('order'),
  448. // - a string describing where $action should be applied ('siblings'),
  449. // - and the class of the element containing our 'weight' element.
  450. drupal_add_tabledrag($table_id, 'order', 'sibling', 'panel-item-weight');
  451. return $output;
  452. }
  453. /**
  454. * Implements hook_theme().
  455. */
  456. function tripal_fields_layout_theme($existing, $type, $theme, $path) {
  457. return array(
  458. 'tripal_fields_layout_form_draggable_panel_table' => array(
  459. 'render element' => 'element',
  460. ),
  461. );
  462. }