tripal_ds.field_formatter.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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' => ''),
  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' => ''),
  19. ),
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_field_group_format_settings().
  25. * If the group has no format settings, default ones will be added.
  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. $form = array();
  31. // Add instance_settings.
  32. switch ($group->format_type) {
  33. case 'tripalpane':
  34. $form['label']['#description'] = t('Please enter a label for collapsible elements');
  35. break;
  36. }
  37. return $form;
  38. }
  39. /*
  40. * Implements field_group_pre_render_<format-type>.
  41. * Format type: Tripalpane.
  42. *
  43. * @param $element The field group form element.
  44. * @param $group The Field group object prepared for pre_render.
  45. * @param $form The root element or form.
  46. */
  47. function tripal_ds_field_group_pre_render(&$element, $group, &$form) {
  48. $group_name = $group->group_name;
  49. switch ($group->format_type) {
  50. case 'tripalpane':
  51. $element['#prefix'] = '<div class="tripal_pane-fieldset-'.$group_name.' '.$group_name.'">';
  52. $element['#suffix'] = '</div>';
  53. break;
  54. }
  55. }
  56. /*
  57. * Implements hook_field_group_create_field_group
  58. *
  59. */
  60. function tripal_ds_field_group_create_field_group($group) {
  61. if($group->format_type == 'tripalpane'){
  62. //write to the tripal_ds table to record the new tripal pane
  63. $field_for_table = new stdClass();
  64. $field_for_table->tripal_ds_field_name = $group->group_name;
  65. $field_for_table->tripal_ds_field_label = $group->label;
  66. $field_for_table->entity_type = 'TripalEntity';
  67. $field_for_table->bundle = $group->bundle;
  68. drupal_write_record('tripal_ds', $field_for_table);
  69. }
  70. }
  71. /*
  72. * Implements hook_field_group_delete_field_group
  73. *
  74. */
  75. function tripal_ds_field_group_delete_field_group($group) {
  76. if($group->format_type == 'tripalpane'){
  77. db_delete('tripal_ds')
  78. ->condition('bundle', $group->bundle, '=')
  79. ->condition('tripal_ds_field_name', $group->group_name, '=')
  80. ->execute();
  81. }
  82. }
  83. /*
  84. * Implements hook_field_group_update_field_group
  85. * checks for changes from tripalpane to another type
  86. */
  87. function tripal_ds_field_group_update_field_group($group) {
  88. //change from tripal pane to another type
  89. if(!empty($group->bundle)){
  90. $result = db_select('tripal_ds', 't')
  91. ->fields('t')
  92. ->condition('bundle', $group->bundle,'=')
  93. ->condition('tripal_ds_field_name', $group->group_name,'=')
  94. ->execute()
  95. ->fetchAssoc();
  96. //change from tripal pane to another type
  97. if($group->format_type !== 'tripalpane' && !empty($result)){
  98. db_delete('tripal_ds')
  99. ->condition('bundle', $result['bundle'], '=')
  100. ->condition('tripal_ds_field_name', $result['tripal_ds_field_name'], '=')
  101. ->execute();
  102. }
  103. //change from other type to tripalpane
  104. if($group->format_type == 'tripalpane' && empty($result)){
  105. if(strpos($group->group_name, 'Tripal Pane')){
  106. $group_name = str_replace('Tripal Pane', "", $group->group_name);
  107. }
  108. $field_for_table = new stdClass();
  109. $field_for_table->tripal_ds_field_name = $group_name;
  110. $field_for_table->tripal_ds_field_label = $group->label;
  111. $field_for_table->entity_type = 'TripalEntity';
  112. $field_for_table->bundle = $group->bundle;
  113. drupal_write_record('tripal_ds', $field_for_table);
  114. }
  115. }
  116. }