tripal_ds.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. require_once "includes/tripal_ds.inc";
  3. require_once "includes/tripal_ds.ds.inc";
  4. require_once "includes/tripal_ds.field_group.inc";
  5. require_once "includes/tripal_ds.field_formatter.inc";
  6. function tripal_ds_init() {
  7. drupal_add_css(drupal_get_path('module', 'tripal_ds') . '/theme/css/tripaldsfeature.css');
  8. drupal_add_js(drupal_get_path('module', 'tripal_ds') . '/theme/js/tripal_ds.js');
  9. $theme_dir = url(drupal_get_path('module', 'tripal_ds') . '/theme');
  10. drupal_add_js("var ds_theme_dir = '$theme_dir';", 'inline', 'header');
  11. }
  12. /**
  13. * Implements hook_views_api().
  14. */
  15. function tripal_ds_views_api() {
  16. return array(
  17. 'api' => 3,
  18. 'path' => drupal_get_path('module', 'tripal_ds') . '/includes/views',
  19. );
  20. }
  21. /**
  22. * Implements hook_menu().
  23. * Defines all menu items needed by Tripal DS
  24. *
  25. */
  26. function tripal_ds_menu() {
  27. $items = array();
  28. // Adds a +Apply Tripal Display Suite option to 'Tripal Content Types' page.
  29. $items['admin/structure/bio_data/manage/%/display/apply'] = array(
  30. 'title' => 'Apply Default Tripal Layout (will reset current layout)',
  31. 'description' => t('Apply the Tripal Display Suite settings to this content type.'),
  32. 'page callback' => 'drupal_get_form',
  33. 'access arguments' => array('administer tripal'),
  34. 'page arguments' => array('tripal_ds_update_layout_form', 4),
  35. 'type' => MENU_LOCAL_ACTION,
  36. );
  37. // Adds a +Add Tripal Pane button to 'Tripal Content Types' page.
  38. $items['admin/structure/bio_data/manage/%/display/create'] = array(
  39. 'title' => 'Create an empty Tripal Pane',
  40. 'description' => t('Create a new empty tripal pane.'),
  41. 'page callback' => 'drupal_get_form',
  42. 'access arguments' => array('administer tripal'),
  43. 'page arguments' => array('tripal_ds_pane_addition_button_form', 4),
  44. 'type' => MENU_LOCAL_ACTION,
  45. );
  46. return $items;
  47. }
  48. /**
  49. * Implements hook_bundle_postcreate().
  50. *
  51. * This is a Triapl defined hook and is called in the TripalBundle::create()
  52. * function to allow modules to perform tasks when a bundle is created.
  53. */
  54. function tripal_ds_bundle_postcreate($bundle) {
  55. $bundle_name = $bundle->name;
  56. $bundle_data_table = $bundle->data_table;
  57. $instances = field_info_instances('TripalEntity', $bundle_name);
  58. if($bundle_data_table == 'pub'){
  59. _ds_layout_pub_settings_info($bundle_name, $instances);
  60. }
  61. else {
  62. _ds_layout_settings_info($bundle_name, $instances);
  63. }
  64. }
  65. /**
  66. * Update the tripal_ds table when a tripal pane is deleted.
  67. */
  68. function tripal_ds_table_column_delete($bundle){
  69. $bundle_name = $bundle->name;
  70. db_delete('tripal_ds')
  71. ->condition('bundle', $bundle_name, '=')
  72. ->execute();
  73. }
  74. /**
  75. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  76. */
  77. function tripal_ds_bundle_delete($bundle){
  78. tripal_ds_table_column_delete($bundle);
  79. }
  80. /*
  81. * Implements hook_ds_field_settings_alter()
  82. */
  83. function tripal_ds_ds_field_settings_alter(&$field_settings, $form, $form_state){
  84. // Get the form info from the bundle about to be saved.
  85. $tripal_entity_object = $form_state['build_info']['args']['1'];
  86. // Grab the bundle.
  87. $bundle_id = $tripal_entity_object->name;
  88. // Grab the field groups from the bundle.
  89. $updated_field_groups = $form_state['field_group'];
  90. // Grab the fields from the bundle.
  91. $fields = $form_state['values']['fields'];
  92. // Delete the menu items associated with the bundle id.
  93. db_delete('tripal_ds')
  94. ->condition('bundle', $bundle_id, '=')
  95. ->execute();
  96. // Traverse the updated field_groups grabbing the tripal pane items.
  97. $tripal_pane_field_groups = array();
  98. $i = 0;
  99. foreach($updated_field_groups as $updated_field_group){
  100. if($updated_field_group->format_type == 'tripalpane'){
  101. $tripal_pane_field_groups += [ $i => $updated_field_group->group_name];
  102. $i++;
  103. }
  104. }
  105. // Now grab the labels of the tripalpane.
  106. foreach($updated_field_groups as $updated_field_group){
  107. foreach($tripal_pane_field_groups as $tripal_pane_field_group){
  108. if($updated_field_group->group_name == $tripal_pane_field_group){
  109. if($fields[$tripal_pane_field_group]['region'] !== 'hidden'){
  110. tripal_ds_bundle_menu_item($bundle_id, $updated_field_group->label, $tripal_pane_field_group, 'tripalentity');
  111. }
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Implements hook_field_group_pre_render().
  118. *
  119. * This function gives you the oppertunity to create the given
  120. * wrapper element that can contain the fields.
  121. * In the example beneath, some variables are prepared and used when building the
  122. * actual wrapper element. All elements in drupal fapi can be used.
  123. *
  124. * Note that at this point, the field group has no notion of the fields in it.
  125. *
  126. * There is also an alternative way of handling this. The default implementation
  127. * within field_group calls "field_group_pre_render_<format_type>".
  128. * @see field_group_pre_render_fieldset.
  129. *
  130. * @param Array $elements by address.
  131. * @param Object $group The Field group info.
  132. function tripal_ds_field_group_pre_render(& $element, $group, & $form) {
  133. watchdog('debug', '<pre>tripal_ds_preprocess_TripalEntity $group: '. print_r($group, TRUE) .'</pre>');
  134. if ($group->format_settings['formatter'] != 'open') {
  135. $add['#prefix'] = '<div class="field-group-format ' . $classes . '">
  136. <span class="field-group-format-toggler">' . check_plain(t($group->label)) . '</span>
  137. <div class="field-group-format-wrapper" style="display: none;">';
  138. $add['#suffix'] = '</div></div>';
  139. }
  140. }
  141. */
  142. /**
  143. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  144. */
  145. function tripal_ds_bundle_menu_item($bundle_name, $field_label, $field_name, $entity_type){
  146. //Check the record does not already exist
  147. $tripal_ds_rows = db_select('tripal_ds', 'ds')
  148. ->fields('ds', array('tripal_ds_field_name', 'tripal_ds_field_label'))
  149. ->condition('bundle', $bundle_name, '=')
  150. ->condition('tripal_ds_field_label', $field_label, '=')
  151. ->condition('tripal_ds_field_name', $field_name, '=')
  152. ->execute()->fetchAll();
  153. if(!empty($tripal_ds_rows)){
  154. foreach ($tripal_ds_rows as $tripal_ds_row){
  155. if(($field_label == $tripal_ds_row->tripal_ds_field_label) && ($field_name == $tripal_ds_row->tripal_ds_field_name) && ($bundle_name == $tripal_ds_rows->bundle)) {
  156. // Do not write the field to the table
  157. drupal_set_message("Could not update the bundle menu because that field already exists.", 'error');
  158. }
  159. }
  160. }
  161. else {
  162. //Write to the tripal_ds table to record the new tripal pane.
  163. $field_for_table = new stdClass();
  164. $field_for_table->tripal_ds_field_name = $field_name;
  165. $field_for_table->tripal_ds_field_label = $field_label;
  166. $field_for_table->entity_type = $entity_type;
  167. $field_for_table->bundle = $bundle_name;
  168. drupal_write_record('tripal_ds', $field_for_table);
  169. }
  170. }
  171. /*
  172. * Implements hook_ds_layout_info() to define layouts from code in a module for
  173. * display suite
  174. */
  175. function tripal_ds_ds_layout_info() {
  176. $path = drupal_get_path('module', 'tripal_ds');
  177. $layouts = array(
  178. 'tripal_ds_feature' => array(
  179. 'label' => t('Tripal Feature Layout'),
  180. 'path' => $path . '/theme/templates',
  181. 'regions' => array(
  182. 'left' => t('Left'),
  183. 'right' => t('Right'),
  184. ),
  185. 'css' => TRUE,
  186. ),
  187. );
  188. return $layouts;
  189. }
  190. function tripal_ds_update_layout_form($form, &$form_state, $bundle_name) {
  191. $form = array();
  192. $form['bundle_name'] = array(
  193. '#type' => 'value',
  194. '#value' => $bundle_name,
  195. );
  196. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  197. $bundle_label = $bundle->label;
  198. return confirm_form($form,
  199. t('Please confirm you would like to apply this layout: ' . $bundle_label),
  200. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  201. t('This action cannot be undone.'),
  202. t('Yes, apply layout'),
  203. t('No, cancel')
  204. );
  205. }
  206. /**
  207. *
  208. * @param $form_state
  209. * @param $form
  210. */
  211. function tripal_ds_update_layout_form_submit($form, &$form_state) {
  212. $bundle_name = $form_state['build_info']['args'][0];
  213. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  214. //Build the identifier to check against ds_layout_settings.
  215. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  216. //Check to see if the layout already exists.
  217. $result = db_select('ds_layout_settings', 'ds')
  218. ->fields('ds')
  219. ->condition('id', $ds_identifier, '=')
  220. ->execute()
  221. ->fetchField();
  222. //Check to see if there are any field groups associated with the bundle.
  223. $result_fg = db_select('field_group', 'fg')
  224. ->fields('fg')
  225. ->condition('bundle', $bundle_name, '=')
  226. ->execute()
  227. ->fetchField();
  228. //Check to see if there are any tripal ds fields associated with the bundle.
  229. $result_tds = db_select('tripal_ds', 'tds')
  230. ->fields('tds')
  231. ->condition('bundle', $bundle_name, '=')
  232. ->execute();
  233. //Check to see if there are any field settings associated with the bundle.
  234. $result_fs = db_select('ds_field_settings', 'fs')
  235. ->fields('fs')
  236. ->condition('bundle', $bundle_name, '=')
  237. ->execute();
  238. //If the layout exists, delete it.
  239. if(!empty($result)) {
  240. db_delete('ds_layout_settings')
  241. ->condition('id', $ds_identifier, '=')
  242. ->execute();
  243. }
  244. //Then delete the field_group_fields associated with the identifier.
  245. if(!empty($result_fg)) {
  246. db_delete('field_group')
  247. ->condition('bundle', $bundle_name, '=')
  248. ->execute();
  249. }
  250. //Then delete the ds_field_settings associated with the identifier.
  251. if(!empty($result_tds)) {
  252. db_delete('ds_field_settings')
  253. ->condition('bundle', $bundle_name, '=')
  254. ->execute();
  255. }
  256. //Then delete the tripal_ds menu item.
  257. if(!empty($result_fs)) {
  258. db_delete('tripal_ds')
  259. ->condition('bundle', $bundle_name, '=')
  260. ->execute();
  261. }
  262. //Now you can build the layout fresh.
  263. $instances = field_info_instances('TripalEntity', $bundle_name);
  264. $bundle_data_table = $bundle->data_table;
  265. if($bundle_data_table == 'pub'){
  266. $success = _ds_layout_pub_settings_info($bundle_name, $instances);
  267. }
  268. else {
  269. $success = _ds_layout_settings_info($bundle_name, $instances);
  270. }
  271. if ($success) {
  272. drupal_set_message("Layout applied successfully and saved.");
  273. }
  274. else {
  275. drupal_set_message("Could not apply layout.", 'error');
  276. }
  277. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  278. }
  279. /**
  280. * @param $field
  281. * @param $bundle_name
  282. function tripal_ds_field_create_field($field, $bundle_name) {
  283. //Build the rest of the passes parameters.
  284. $field_name = str_replace('field_', '', $field['field_name']);
  285. $group_field_name = 'gp_'.$field['field_name'];
  286. //Create the field groups.
  287. _additional_fields_field_group_info($bundle_name, $field_name, $group_field_name, $field_name);
  288. //Place the field groups in the layout.
  289. tripal_ds_update_ds_layout($bundle_name, $field_name, $group_field_name);
  290. }
  291. */
  292. /**
  293. * @param $form
  294. * @param $form_state
  295. * @param $bundle_name
  296. * @return mixed
  297. */
  298. function tripal_ds_pane_addition_button_form($form, &$form_state, $bundle_name) {
  299. $form = array();
  300. $form['bundle_name'] = array(
  301. '#type' => 'value',
  302. '#value' => $bundle_name,
  303. );
  304. $form['field_name'] = array(
  305. '#type' => 'textfield',
  306. '#title' => t('Tripal Panel Label'),
  307. '#required' => TRUE,
  308. '#description' => "Please enter the label for the new Tripal Pane",
  309. '#size' => 20,
  310. '#maxlength' => 50,
  311. );
  312. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  313. $bundle_label = $bundle->label;
  314. return confirm_form($form,
  315. t('Please confirm you would like to create a new field for: ' . $bundle_label),
  316. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  317. t('Create new Tripal Pane'),
  318. t('Yes'),
  319. t('No, cancel')
  320. );
  321. }
  322. /**
  323. *
  324. * @param $form_state
  325. * @param $form
  326. */
  327. function tripal_ds_pane_addition_button_form_submit($form, &$form_state) {
  328. $bundle_name = $form_state['build_info']['args'][0];
  329. //Build the rest of the passed variables.
  330. $field_name = $form_state['input']['field_name'];
  331. $group_field_name = 'gp_'.$form_state['input']['field_name'];
  332. //Create the field groups, last passed parameter is NULL because there are no
  333. //children.
  334. _additional_fields_field_group_info($bundle_name, $field_name, $group_field_name, NULL);
  335. //Place the field groups in the layout.
  336. tripal_ds_update_ds_layout($bundle_name, NULL, $group_field_name);
  337. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  338. }
  339. /**
  340. * @param $bundle_name
  341. */
  342. function tripal_ds_update_ds_layout($bundle_name, $field_name, $tripal_pane_name) {
  343. //Build the identifier to check against ds_layout_settings.
  344. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  345. //Check to see if the layout already exists.
  346. $result = db_select('ds_layout_settings', 'ds')
  347. ->fields('ds', array('settings'))
  348. ->condition('ds.id', $ds_identifier, '=')
  349. ->execute()
  350. ->fetchObject();
  351. //If the layout exists unserialize it.
  352. if(!empty($result)) {
  353. $layout_info = $result->settings;
  354. $layout_info = unserialize($layout_info);
  355. //Count the number of rows in the region and add the field to the region.
  356. $index = count($layout_info['regions']['right']);
  357. //Now add the tripal pane and field to the right region and field array.
  358. if(!empty($field_name)){
  359. $layout_info['regions']['right'][$index] = $field_name;
  360. $incremented_index = $index++;
  361. $layout_info['fields'][$field_name] = 'right';
  362. }
  363. if(!empty($tripal_pane_name)){
  364. if(!empty($incremented_index)){
  365. $layout_info['regions']['right'][$incremented_index] = $tripal_pane_name;
  366. $layout_info['fields'][$tripal_pane_name] = 'right';
  367. }
  368. else {
  369. $layout_info['regions']['right'][$index] = $tripal_pane_name;
  370. $layout_info['fields'][$tripal_pane_name] = 'right';
  371. }
  372. }
  373. //Update the ds_layout_settings table with the new layout info.
  374. drupal_write_record('ds_layout_settings', $layout_info);
  375. }
  376. }
  377. /*
  378. * Code for the view of the menu items
  379. //get tripal entity id from url then run it against tripal entity db
  380. //and grab the bundle id, then pass bundle id to view
  381. $url = current_path();
  382. $url_exploded = explode("/", $url);
  383. $tripal_entity_id = (int)$url_exploded[1];
  384. $result = db_select('tripal_entity', 'te')
  385. ->fields('te', array('bundle'))
  386. ->condition('id', $tripal_entity_id, '=')
  387. ->execute()
  388. ->fetchField();
  389. */
  390. /**
  391. * Implements hook_field_display_alter().
  392. * @param $display
  393. * @param $context
  394. */
  395. function tripal_ds_field_display_alter(&$display, $context){
  396. $field_name = $context['field']['field_name'];
  397. $bundle = $context['entity']->bundle;
  398. $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
  399. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, '');
  400. if($field_name && ($hide_variable == 'hide')){
  401. $field = field_get_items('TripalEntity', $context['entity'], $field_name);
  402. if($field) {
  403. if(tripal_field_is_empty($field, $field)) {
  404. $parent_field_info = tripal_ds_find_field_group_parent($field_name, 'TripalEntity', $bundle, $context);
  405. if(!empty($parent_field_info)){
  406. $increment = 0;
  407. foreach($parent_field_info as $parent_fields => $parent_field){
  408. // Stop the right rail element from rendering.
  409. drupal_add_css('.'.$parent_field_info[$increment].' {display:none;}', 'inline');
  410. // Hide any associated menu links.
  411. drupal_add_css('#'.$parent_field_info[$increment].' {display:none;}', 'inline');
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }
  418. /*
  419. */
  420. /**
  421. * @param $field_name
  422. * @param $entity_type
  423. * @param $bundle
  424. *
  425. * @return array
  426. */
  427. function tripal_ds_find_field_group_parent($field_name, $entity_type, $bundle, $context){
  428. $field_groups_to_hide = array();
  429. $increment = 0;
  430. // Get the field groups associated with this bundle.
  431. $fg_for_bundle = db_select('field_group', 'fg')
  432. ->fields('fg')
  433. ->condition('bundle', $bundle, '=')
  434. ->condition('entity_type', $entity_type, '=')
  435. ->execute()->fetchAll();
  436. // Run through the field groups looking for the provided $field_name
  437. foreach ($fg_for_bundle as $field_groups => $field_group) {
  438. $field_group_data = unserialize($field_group->data);
  439. // There is a separate function to deal with tables, so disregard.
  440. if ($field_group_data['format_type'] == 'table'){
  441. // Do nothing
  442. }
  443. elseif (!empty($field_group_data['children'][0])) {
  444. $children = $field_group_data['children'];
  445. //If there is more than one child all need to be checked.
  446. if (count($children) > 1) {
  447. foreach ($children as $kids => $child) {
  448. // Now check if each child if empty.
  449. $field = field_get_items('TripalEntity', $context['entity'], $child);
  450. if(!tripal_field_is_empty($field, $field)){
  451. //If any of the fields are not empty do not add the parent.
  452. break 2;
  453. }
  454. else {
  455. continue;
  456. }
  457. }
  458. $field_groups_to_hide[$increment] = $field_group->group_name;
  459. }
  460. elseif($children[0] == $field_name) {
  461. $field_groups_to_hide[$increment] = $field_group->group_name;
  462. }
  463. }
  464. $increment++;
  465. }
  466. // Remove duplicate values.
  467. $field_groups_to_hide = array_unique($field_groups_to_hide);
  468. return $field_groups_to_hide;
  469. }