tripal_ds.field_formatter.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Implements hook_field_group_formatter_info().
  4. */
  5. function tripal_ds_field_group_formatter_info() {
  6. return array(
  7. 'form' => array(
  8. 'tripalpane' => array(
  9. 'label' => t('Tripal Pane'),
  10. 'description' => t('This fieldgroup renders the inner content in a Tripal Pane with the title as legend.'),
  11. 'instance_settings' => array('description' => '', 'classes' => '', 'id' => '', 'hide' => 1),
  12. ),
  13. ),
  14. 'display' => array(
  15. 'tripalpane' => array(
  16. 'label' => t('Tripal Pane'),
  17. 'description' => t('This fieldgroup renders the inner content in a Tripal Pane with the title as legend.'),
  18. 'instance_settings' => array('description' => '', 'classes' => '', 'id' => '', 'hide' => 1),
  19. ),
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_field_group_format_settings().
  25. *
  26. * @params Object $group The group object.
  27. * @return Array $form The form element for the format settings.
  28. */
  29. function tripal_ds_field_group_format_settings($group) {
  30. // Add a wrapper for extra settings to use by others.
  31. $form = array(
  32. 'instance_settings' => array(
  33. '#tree' => TRUE,
  34. '#weight' => 2,
  35. ),
  36. );
  37. $field_group_types = field_group_formatter_info();
  38. $mode = $group->mode == 'form' ? 'form' : 'display';
  39. $formatter = $field_group_types [$mode][$group->format_type];
  40. // Add optional instance_settings.
  41. switch ($group->format_type) {
  42. case 'tripalpane':
  43. $form['instance_settings']['hide'] = array(
  44. '#title' => t('Hide panel on page load'),
  45. '#type' => 'checkbox',
  46. '#default_value' => isset($group->format_settings['instance_settings']['hide']) ? $group->format_settings['instance_settings']['hide'] : $formatter['instance_settings']['hide'],
  47. '#weight' => 2,
  48. );
  49. break;
  50. }
  51. return $form;
  52. }
  53. /**
  54. * Implements field_group_pre_render_<format-type>.
  55. * Format type: Tripalpane.
  56. *
  57. * @param $element The field group form element.
  58. * @param $group The Field group object prepared for pre_render.
  59. * @param $form The root element or form.
  60. */
  61. function tripal_ds_field_group_pre_render(&$element, $group, &$form) {
  62. switch ($group->format_type) {
  63. case 'tripalpane':
  64. $group_name = $group->group_name;
  65. //Hide the tripal panes here if there are no children.
  66. $description = $group->format_settings['instance_settings']['description'];
  67. $hide = isset($group->format_settings['instance_settings']['hide']) ? $group->format_settings['instance_settings']['hide'] : 'none';
  68. $classes = $group->format_settings['instance_settings']['classes'];
  69. if ($hide == '1') {
  70. $element['#prefix'] = '<div class="tripal_pane-fieldset-'.$group_name.' '.$group_name.' tripal_pane '.$classes.' hideTripalPane"> <span class="field-group-format-title">' . check_plain(t($group->label)) . '</span>';
  71. }
  72. else {
  73. $element['#prefix'] = '<div class="tripal_pane-fieldset-'.$group_name.' '.$group_name.' tripal_pane '.$classes.'"> <span class="field-group-format-title">' . check_plain(t($group->label)) . '</span>';
  74. }
  75. if (!empty($description)) {
  76. $element['#prefix'] .= '<div class="description">' . $description . '</div>';
  77. }
  78. $element['#suffix'] = '</div>';
  79. break;
  80. }
  81. }
  82. /**
  83. * Updated the weight of field groups to reflect their order in the layout array.
  84. *
  85. * @param $field_name
  86. * Machine readable name of the field.
  87. * @param $bundle
  88. * Machine name of bundle, example bio_data_1
  89. * @param $weight
  90. */
  91. function tripal_ds_field_group_update_weight($field_name, $bundle, $weight) {
  92. // Pull the group data from field_group table.
  93. $result = db_select('field_group', 'fg')
  94. ->fields('fg')
  95. ->condition('bundle', $bundle,'=')
  96. ->condition('group_name', $field_name,'=')
  97. ->execute()
  98. ->fetchAssoc();
  99. // If result found update the weight.
  100. if(!empty($result)){
  101. $data = unserialize($result['data']);
  102. $data['weight'] = $weight;
  103. $data = serialize($data);
  104. // Write the new weight to the field_group entry.
  105. db_update('field_group')
  106. ->fields(array(
  107. 'data' => $data
  108. ))
  109. ->condition('bundle', $bundle,'=')
  110. ->condition('group_name', $field_name,'=')
  111. ->execute();
  112. }
  113. }