tripal.entity.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * Implement hook_entity_info().
  4. *
  5. * See the following for documentaiton of this type setup for Entities:
  6. *
  7. * http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  8. * http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  9. */
  10. function tripal_entity_info() {
  11. $entities = array();
  12. // The TripalVocab entity is meant to house vocabularies. It is these
  13. // vocabs that are used by the TripalTerm entities. The storage backend
  14. // is responsible for setting the values of this entity.
  15. //
  16. $entities['TripalVocab'] = array(
  17. // A human readable label to identify our entity.
  18. 'label' => 'Controlled Vocabulary',
  19. 'plural label' => 'Controlled Vocabularies',
  20. // The entity class and controller class extend the classes provided by the
  21. // Entity API.
  22. 'entity class' => 'TripalVocab',
  23. 'controller class' => 'TripalVocabController',
  24. // Adds Views integration for this entity.
  25. 'views controller class' => 'TripalVocabViewsController',
  26. // The table for this entity defined in hook_schema()
  27. 'base table' => 'tripal_vocab',
  28. // If fieldable == FALSE, we can't attach fields.
  29. 'fieldable' => TRUE,
  30. // entity_keys tells the controller what database fields are used for key
  31. // functions. It is not required if we don't have bundles or revisions.
  32. // Here we do not support a revision, so that entity key is omitted.
  33. 'entity keys' => array (
  34. 'id' => 'id',
  35. ),
  36. // Callback function for access to this entity.
  37. 'access callback' => 'tripal_entity_access',
  38. // FALSE disables caching. Caching functionality is handled by Drupal core.
  39. // 'static cache' => FALSE,
  40. // Disable caching of fields
  41. // 'field cache' => FALSE,
  42. // This entity doesn't support bundles.
  43. 'bundles' => array (),
  44. 'view modes' => array (
  45. 'full' => array (
  46. 'label' => t ('Full content'),
  47. 'custom settings' => FALSE
  48. ),
  49. 'teaser' => array (
  50. 'label' => t ('Teaser'),
  51. 'custom settings' => TRUE
  52. ),
  53. ),
  54. );
  55. //
  56. // The TripalTerm entity is meant to house vocabulary terms. It is these
  57. // terms that are used by the TripalEntity entities. The storage backend
  58. // is responsible for setting the values of this entity.
  59. //
  60. $entities['TripalTerm'] = array(
  61. // A human readable label to identify our entity.
  62. 'label' => 'Controlled Vocabulary Term',
  63. 'plural label' => 'Controlled Vocabulary Terms',
  64. // The entity class and controller class extend the classes provided by the
  65. // Entity API.
  66. 'entity class' => 'TripalTerm',
  67. 'controller class' => 'TripalTermController',
  68. // Adds Views integration for this entity.
  69. 'views controller class' => 'TripalTermViewsController',
  70. // The table for this entity defined in hook_schema()
  71. 'base table' => 'tripal_term',
  72. // If fieldable == FALSE, we can't attach fields.
  73. 'fieldable' => TRUE,
  74. // entity_keys tells the controller what database fields are used for key
  75. // functions. It is not required if we don't have bundles or revisions.
  76. // Here we do not support a revision, so that entity key is omitted.
  77. 'entity keys' => array (
  78. 'id' => 'id',
  79. ),
  80. // Callback function for access to this entity.
  81. 'access callback' => 'tripal_entity_access',
  82. // FALSE disables caching. Caching functionality is handled by Drupal core.
  83. 'static cache' => FALSE,
  84. // This entity doesn't support bundles.
  85. 'bundles' => array (),
  86. 'view modes' => array (
  87. 'full' => array (
  88. 'label' => t ('Full content'),
  89. 'custom settings' => FALSE
  90. ),
  91. 'teaser' => array (
  92. 'label' => t ('Teaser'),
  93. 'custom settings' => TRUE
  94. ),
  95. ),
  96. );
  97. //
  98. // The TripalEntity is used for all data. It links data from a storage
  99. // back-end to a TripalTerm entity.
  100. //
  101. $entities['TripalEntity'] = array (
  102. // A human readable label to identify our entity.
  103. 'label' => 'Tripal Content',
  104. 'plural label' => 'Tripal Content',
  105. // The entity class and controller class extend the classes provided by the
  106. // Entity API.
  107. 'entity class' => 'TripalEntity',
  108. 'controller class' => 'TripalEntityController',
  109. // Adds Views integration for this entity.
  110. 'views controller class' => 'TripalEntityViewsController',
  111. // The table for this entity defined in hook_schema()
  112. 'base table' => 'tripal_entity',
  113. // Returns the uri elements of an entity.
  114. 'uri callback' => 'tripal_vocbulary_term_uri',
  115. // IF fieldable == FALSE, we can't attach fields.
  116. 'fieldable' => TRUE,
  117. // entity_keys tells the controller what database fields are used for key
  118. // functions. It is not required if we don't have bundles or revisions.
  119. // Here we do not support a revision, so that entity key is omitted.
  120. 'entity keys' => array (
  121. 'id' => 'id',
  122. 'bundle' => 'bundle'
  123. ),
  124. 'bundle keys' => array (
  125. 'bundle' => 'name'
  126. ),
  127. // Callback function for access to this entity.
  128. 'access callback' => 'tripal_entity_access',
  129. // FALSE disables caching. Caching functionality is handled by Drupal core.
  130. 'static cache' => FALSE,
  131. // Bundles are added dynamically below.
  132. 'bundles' => array (),
  133. 'label callback' => 'tripal_entity_label',
  134. // The information below is used by the TripalEntityUIController
  135. // (which extends the EntityDefaultUIController). The admin_ui
  136. // key here is mean to appear on the 'Find Content' page of the
  137. // administrative menu.
  138. 'admin ui' => array (
  139. 'path' => 'admin/content/bio_data',
  140. 'controller class' => 'TripalEntityUIController',
  141. 'menu wildcard' => '%TripalEntity',
  142. 'file' => 'includes/TripalEntityUIController.inc'
  143. ),
  144. 'view modes' => array (
  145. 'full' => array (
  146. 'label' => t ('Full content'),
  147. 'custom settings' => FALSE
  148. ),
  149. 'teaser' => array (
  150. 'label' => t ('Teaser'),
  151. 'custom settings' => TRUE
  152. )
  153. )
  154. );
  155. //
  156. // The TripalBundle entity is used manage the bundle types. The 'bundle of'
  157. // attribute links this to the TripalEntity and allows the UI provided
  158. // by the entity module to work for each TripalEntity bundle.
  159. //
  160. $entities['TripalBundle'] = array (
  161. 'label' => 'Tripal Content Type',
  162. 'entity class' => 'TripalBundle',
  163. 'controller class' => 'TripalBundleController',
  164. 'base table' => 'tripal_bundle',
  165. 'fieldable' => FALSE,
  166. 'bundle of' => 'TripalEntity',
  167. 'exportable' => FALSE,
  168. 'entity keys' => array (
  169. 'id' => 'id',
  170. 'name' => 'name',
  171. 'label' => 'label'
  172. ),
  173. 'access callback' => 'tripal_bundle_access',
  174. 'module' => 'tripal',
  175. // Enable the entity API's admin UI.
  176. 'admin ui' => array (
  177. 'path' => 'admin/structure/bio_data',
  178. 'controller class' => 'TripalBundleUIController',
  179. 'file' => 'includes/TripalBundleUIController.inc',
  180. 'menu wildcard' => '%TripalBundle',
  181. )
  182. );
  183. return $entities;
  184. }
  185. /**
  186. * Implements hook_entities_info_alter().
  187. *
  188. * Add in the bundles (entity types) to the TripalEntity entity.
  189. */
  190. function tripal_entity_info_alter(&$entity_info){
  191. if (array_key_exists('TripalEntity', $entity_info)) {
  192. // Dynamically add in the bundles. Bundles are alternative groups of fields
  193. // or configuration associated with an entity type .We want to dynamically
  194. // add the bundles to the entity.
  195. $bundles = db_select('tripal_bundle', 'tb')
  196. ->fields('tb')
  197. ->execute();
  198. while ($bundle = $bundles->fetchObject()) {
  199. $bundle_name = $bundle->name;
  200. $term_id = $bundle->term_id;
  201. $term = entity_load('TripalTerm', array('id' => $term_id));
  202. $term = reset($term);
  203. $label = preg_replace('/_/', ' ', ucwords($term->name));
  204. $entity_info['TripalEntity']['bundles'][$bundle_name] = array (
  205. 'label' => $label,
  206. 'admin' => array (
  207. 'path' => 'admin/structure/bio_data/manage/%TripalBundle',
  208. 'real path' => 'admin/structure/bio_data/manage/' . $bundle_name,
  209. 'bundle argument' => 4,
  210. 'access arguments' => array (
  211. 'administer tripal data types'
  212. )
  213. )
  214. );
  215. }
  216. }
  217. }
  218. /**
  219. * Checks access permissions for a given entity.
  220. */
  221. function tripal_entity_access($entity) {
  222. // TODO: need to implement this function.
  223. return TRUE;
  224. }
  225. function tripal_form_tripal_entity_form_alter(&$form, &$form_state, $form_id) {
  226. //dpm($form);
  227. }
  228. /**
  229. * Implements hook_entity_view.
  230. *
  231. * Here we want to overwite unattached fields with a div box that will be
  232. * recognized by JavaScript that will then use AJAX to load the field.
  233. *
  234. * We also want to add tooltips to each field that describes to the user
  235. * the meaning of the field.
  236. *
  237. * The tripal_ajax_attach_field() function is called by an AJAX call to
  238. * retrieve the field.
  239. */
  240. function tripal_entity_view($entity, $type, $view_mode, $langcode) {
  241. if ($type == 'TripalEntity') {
  242. foreach (element_children($entity->content) as $child_name) {
  243. // Initailize the prefix and suffix for this field.
  244. if (!array_key_exists('#prefix', $entity->content[$child_name])) {
  245. $entity->content[$child_name]['#prefix'] = '';
  246. }
  247. if (!array_key_exists('#suffix', $entity->content[$child_name])) {
  248. $entity->content[$child_name]['#suffix'] = '';
  249. }
  250. // Surround the field with a <div> box for AJAX loading if this
  251. // field is unattached. this will allow JS code to automatically load
  252. // the field.
  253. $child = $entity->content[$child_name];
  254. if (array_key_exists('#items', $child) and
  255. array_key_exists('0', $child['#items']) and
  256. is_array($child['#items'][0]) and
  257. array_key_exists('#unattached', $child['#items'][0])
  258. and $child['#items'][0]['#unattached'] === TRUE) {
  259. $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" class="tripal-entity-unattached">';
  260. $entity->content[$child_name]['#suffix'] .= '</div>';
  261. }
  262. // Add a tooltip to the field for the term.
  263. $field = field_info_field($child_name);
  264. if (array_key_exists('settings', $field) and
  265. array_key_exists('semantic_web', $field['settings'])) {
  266. $matches = array();
  267. if (preg_match('/^(.+):(.+)$/', $field['settings']['semantic_web'], $matches)) {
  268. $vocabulary = $matches[1];
  269. $accession = $matches[2];
  270. $term = tripal_get_term_details($vocabulary, $accession);
  271. if ($term) {
  272. $tooltip_img = url(drupal_get_path('module', 'tripal') . '/theme/images/info.gif');
  273. $tooltip_text = $term['definition'] ? $term['definition'] : $term['name'];
  274. $entity->content[$child_name]['#prefix'] .= '' .
  275. '<div class="tripal-tooltip">' .
  276. '<img src="' . $tooltip_img . '">' .
  277. '<div class="tripal-tooltiptext">' . $tooltip_text . '</div>' .
  278. '</div>';
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Responds to an AJAX call for populating a field.
  287. *
  288. * @param $id
  289. * The ID of the HTML div box where the field is housed. The ID contains the
  290. * entity ID and field name.
  291. */
  292. function tripal_ajax_attach_field($id) {
  293. $matches = array();
  294. if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
  295. $entity_id = $matches[1];
  296. $field_name = $matches[2];
  297. $field = field_info_fields($field_name);
  298. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
  299. reset($result);
  300. $entity = $result[$entity_id];
  301. $content = drupal_render(field_view_field('TripalEntity', $entity, $field_name));
  302. return drupal_json_output(array(
  303. 'id' => $id,
  304. 'content' => $content
  305. ));
  306. }
  307. }