tripal_entities.entity_form.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_entities_add_page() {
  6. $item = menu_get_item();
  7. $content = system_admin_menu_block($item);
  8. // Bypass the node/add listing if only one content type is available.
  9. if (count($content) == 1) {
  10. $item = array_shift($content);
  11. drupal_goto($item['href']);
  12. }
  13. return theme('tripal_entities_add_list', array('content' => $content));
  14. }
  15. /**
  16. * Returns HTML for a list of available node types for node creation.
  17. *
  18. * @param $variables
  19. * An associative array containing:
  20. * - content: An array of content types.
  21. *
  22. * @ingroup themeable
  23. */
  24. function theme_tripal_entities_add_list($variables) {
  25. $content = $variables['content'];
  26. $output = '';
  27. if ($content) {
  28. $output = '<dl class="node-type-list">';
  29. foreach ($content as $item) {
  30. $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
  31. $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
  32. }
  33. $output .= '</dl>';
  34. }
  35. else {
  36. $output = '<p>' . t('You have not created any biological types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.', array('@create-content' => url('admin/structure/BioData/add'))) . '</p>';
  37. }
  38. return $output;
  39. }
  40. /**
  41. *
  42. */
  43. function tripal_entities_entity_form($form, &$form_state, $dbxref_id = '', $entity = NULL) {
  44. $bundle_id = 'dbxref_' . $dbxref_id;
  45. // Add a vertical tabs element
  46. $form['entity_form_vtabs'] = array(
  47. '#type' => 'vertical_tabs',
  48. '#weight' => 999,
  49. );
  50. // If the entity doesn't exist then create one.
  51. if (!$entity) {
  52. $entity = entity_get_controller('BioData')->create(array('bundle' => $bundle_id));
  53. field_attach_form('BioData', $entity, $form, $form_state);
  54. $form['add_button'] = array(
  55. '#type' => 'submit',
  56. '#value' => t('Save'),
  57. '#name' => 'add_data',
  58. '#weight' => 1000
  59. );
  60. }
  61. else {
  62. field_attach_form('BioData', $entity, $form, $form_state);
  63. $form['update_button'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Update'),
  66. '#name' => 'update_data',
  67. '#weight' => 1000
  68. );
  69. $form['delete_button'] = array(
  70. '#type' => 'submit',
  71. '#value' => t('Delete'),
  72. '#name' => 'delete_data',
  73. '#weight' => 1001
  74. );
  75. }
  76. // The entity object must be added to the $form_state in order for
  77. // the Entity API to work. It must have a key of the entity name.
  78. $form_state['BioData'] = $entity;
  79. $form['#prefix'] = "<div id='$bundle_id-entity-form'>";
  80. $form['#suffix'] = "</div>";
  81. return $form;
  82. }
  83. /**
  84. * An Ajax callback for the tripal_entities_entity_form.
  85. */
  86. function tripal_entities_entity_form_ajax_callback($form, $form_state) {
  87. // return the form so Drupal can update the content on the page
  88. return $form;
  89. }
  90. /**
  91. * Implements hook_validate() for the tripal_entities_entity_form.
  92. */
  93. function tripal_entities_entity_form_validate($form, &$form_state) {
  94. if (array_key_exists('clicked_button', $form_state) and
  95. $form_state['clicked_button']['#name'] =='add_data') {
  96. $entity = $form_state['BioData'];
  97. field_attach_form_validate('BioData', $entity, $form, $form_state);
  98. }
  99. }
  100. /**
  101. * Implements hook_submit() for the tripal_entities_entity_form.
  102. */
  103. function tripal_entities_entity_form_submit($form, &$form_state) {
  104. $entity = $form_state['BioData'];
  105. if ($form_state['clicked_button']['#name'] =='cancel') {
  106. $form_state['redirect'] = "BioData/" . $entity->id;
  107. }
  108. if ($form_state['clicked_button']['#name'] =='update_data' or
  109. $form_state['clicked_button']['#name'] =='add_data') {
  110. $entityform = entity_ui_controller('BioData')->entityFormSubmitBuildEntity($form, $form_state);
  111. if ($entityform->save()) {
  112. $form_state['redirect'] = "BioData/" . $entity->id;
  113. }
  114. else {
  115. drupal_set_message('Cannot save entity', 'error');
  116. }
  117. }
  118. if ($form_state['clicked_button']['#name'] =='delete_data') {
  119. $form_state['redirect'] = 'BioData/' . $entity->id .'/delete';
  120. }
  121. }
  122. /**
  123. * Form callback: confirmation form for deleting a tripal_entity.
  124. *
  125. * @param $tripal_entity The
  126. * tripal_entity to delete
  127. *
  128. * @see confirm_form()
  129. */
  130. function tripal_entities_entity_delete_form($form, &$form_state, $entity) {
  131. $form_state['entity'] = $entity;
  132. $form['#submit'][] = 'tripal_entities_entity_delete_form_submit';
  133. $form = confirm_form($form,
  134. t('Click the delete button below to confirm deletion of the record titled: %title',
  135. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  136. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  137. return $form;
  138. }
  139. /**
  140. * Submit callback for tripal_entity_delete_form
  141. */
  142. function tripal_entities_entity_delete_form_submit($form, &$form_state) {
  143. $entity = $form_state['entity'];
  144. $entity_controller = new TripalEntityController($entity->type);
  145. if ($entity_controller->delete($entity)) {
  146. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  147. $form_state['redirect'] = 'admin/content/tripal_entitys';
  148. }
  149. else {
  150. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  151. }
  152. }