tripal.entity.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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' => TRUE,
  40. // Caching of fields
  41. 'field cache' => TRUE,
  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' => TRUE,
  131. // Caching of fields
  132. 'field cache' => TRUE,
  133. // Bundles are added dynamically below.
  134. 'bundles' => array (),
  135. 'label callback' => 'tripal_entity_label',
  136. // The information below is used by the TripalEntityUIController
  137. // (which extends the EntityDefaultUIController). The admin_ui
  138. // key here is mean to appear on the 'Find Content' page of the
  139. // administrative menu.
  140. 'admin ui' => array (
  141. 'path' => 'admin/content/bio_data',
  142. 'controller class' => 'TripalEntityUIController',
  143. 'menu wildcard' => '%TripalEntity',
  144. 'file' => 'includes/TripalEntityUIController.inc'
  145. ),
  146. 'view modes' => array (
  147. 'full' => array (
  148. 'label' => t ('Full content'),
  149. 'custom settings' => FALSE
  150. ),
  151. 'teaser' => array (
  152. 'label' => t ('Teaser'),
  153. 'custom settings' => TRUE
  154. )
  155. )
  156. );
  157. //
  158. // The TripalBundle entity is used manage the bundle types. The 'bundle of'
  159. // attribute links this to the TripalEntity and allows the UI provided
  160. // by the entity module to work for each TripalEntity bundle.
  161. //
  162. $entities['TripalBundle'] = array (
  163. 'label' => 'Tripal Content Type',
  164. 'entity class' => 'TripalBundle',
  165. 'controller class' => 'TripalBundleController',
  166. 'base table' => 'tripal_bundle',
  167. 'fieldable' => FALSE,
  168. 'bundle of' => 'TripalEntity',
  169. 'exportable' => FALSE,
  170. 'entity keys' => array (
  171. 'id' => 'id',
  172. 'name' => 'name',
  173. 'label' => 'label'
  174. ),
  175. 'access callback' => 'tripal_bundle_access',
  176. 'module' => 'tripal',
  177. // Enable the entity API's admin UI.
  178. 'admin ui' => array (
  179. 'path' => 'admin/structure/bio_data',
  180. 'controller class' => 'TripalBundleUIController',
  181. 'file' => 'includes/TripalBundleUIController.inc',
  182. 'menu wildcard' => '%TripalBundle',
  183. )
  184. );
  185. return $entities;
  186. }
  187. /**
  188. * Implements hook_entities_info_alter().
  189. *
  190. * Add in the bundles (entity types) to the TripalEntity entity.
  191. */
  192. function tripal_entity_info_alter(&$entity_info){
  193. if (array_key_exists('TripalEntity', $entity_info)) {
  194. // Dynamically add in the bundles. Bundles are alternative groups of fields
  195. // or configuration associated with an entity type .We want to dynamically
  196. // add the bundles to the entity.
  197. $bundles = db_select('tripal_bundle', 'tb')
  198. ->fields('tb')
  199. ->execute();
  200. while ($bundle = $bundles->fetchObject()) {
  201. $bundle_name = $bundle->name;
  202. $term_id = $bundle->term_id;
  203. $term = entity_load('TripalTerm', array('id' => $term_id));
  204. $term = reset($term);
  205. $label = preg_replace('/_/', ' ', ucwords($term->name));
  206. $entity_info['TripalEntity']['bundles'][$bundle_name] = array (
  207. 'label' => $label,
  208. 'admin' => array (
  209. 'path' => 'admin/structure/bio_data/manage/%TripalBundle',
  210. 'real path' => 'admin/structure/bio_data/manage/' . $bundle_name,
  211. 'bundle argument' => 4,
  212. 'access arguments' => array (
  213. 'administer tripal data types'
  214. )
  215. )
  216. );
  217. }
  218. }
  219. }
  220. /**
  221. * Checks access permissions for a given entity.
  222. */
  223. function tripal_entity_access($entity) {
  224. // TODO: need to implement this function.
  225. return TRUE;
  226. }
  227. function tripal_form_tripal_entity_form_alter(&$form, &$form_state, $form_id) {
  228. }
  229. /**
  230. * Implements hook_entity_view.
  231. *
  232. * Here we want to overwite unattached fields with a div box that will be
  233. * recognized by JavaScript that will then use AJAX to load the field.
  234. *
  235. * We also want to add tooltips to each field that describes to the user
  236. * the meaning of the field.
  237. *
  238. * The tripal_ajax_attach_field() function is called by an AJAX call to
  239. * retrieve the field.
  240. */
  241. function tripal_entity_view($entity, $type, $view_mode, $langcode) {
  242. if ($type == 'TripalEntity') {
  243. foreach (element_children($entity->content) as $child_name) {
  244. // Initailize the prefix and suffix for this field.
  245. if (!array_key_exists('#prefix', $entity->content[$child_name])) {
  246. $entity->content[$child_name]['#prefix'] = '';
  247. }
  248. if (!array_key_exists('#suffix', $entity->content[$child_name])) {
  249. $entity->content[$child_name]['#suffix'] = '';
  250. }
  251. // Surround the field with a <div> box for AJAX loading if this
  252. // field is unattached. this will allow JS code to automatically load
  253. // the field.
  254. $child = $entity->content[$child_name];
  255. if (array_key_exists('#items', $child) and
  256. array_key_exists('0', $child['#items']) and
  257. is_array($child['#items'][0]) and
  258. array_key_exists('#unattached', $child['#items'][0])
  259. and $child['#items'][0]['#unattached'] === TRUE) {
  260. $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" class="tripal-entity-unattached">';
  261. $entity->content[$child_name]['#suffix'] .= '</div>';
  262. }
  263. // Add a tooltip to the field for the term.
  264. $field = field_info_field($child_name);
  265. if (array_key_exists('settings', $field) and
  266. array_key_exists('semantic_web', $field['settings'])) {
  267. $matches = array();
  268. if (preg_match('/^(.+):(.+)$/', $field['settings']['semantic_web'], $matches)) {
  269. $vocabulary = $matches[1];
  270. $accession = $matches[2];
  271. $term = tripal_get_term_details($vocabulary, $accession);
  272. if ($term) {
  273. $tooltip_img = url(drupal_get_path('module', 'tripal') . '/theme/images/info.gif');
  274. $tooltip_text = $term['definition'] ? $term['definition'] : $term['name'];
  275. $entity->content[$child_name]['#prefix'] .= '' .
  276. '<div class="tripal-tooltip">' .
  277. '<img src="' . $tooltip_img . '">' .
  278. '<div class="tripal-tooltiptext">' . $tooltip_text . '</div>' .
  279. '</div>';
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * Responds to an AJAX call for populating a field.
  288. *
  289. * @param $id
  290. * The ID of the HTML div box where the field is housed. The ID contains the
  291. * entity ID and field name.
  292. */
  293. function tripal_ajax_attach_field($id) {
  294. $matches = array();
  295. if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
  296. $entity_id = $matches[1];
  297. $field_name = $matches[2];
  298. $field = field_info_fields($field_name);
  299. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
  300. reset($result);
  301. $entity = $result[$entity_id];
  302. $content = drupal_render(field_view_field('TripalEntity', $entity, $field_name));
  303. return drupal_json_output(array(
  304. 'id' => $id,
  305. 'content' => $content
  306. ));
  307. }
  308. }