tripal_ds.field_formatter.inc 4.2 KB

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