tripal.entity.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. 'views controller class' => 'TripalBundleViewsController',
  167. 'base table' => 'tripal_bundle',
  168. 'fieldable' => FALSE,
  169. 'bundle of' => 'TripalEntity',
  170. 'exportable' => FALSE,
  171. 'entity keys' => array (
  172. 'id' => 'id',
  173. 'name' => 'name',
  174. 'label' => 'label'
  175. ),
  176. 'access callback' => 'tripal_bundle_access',
  177. 'module' => 'tripal',
  178. // Enable the entity API's admin UI.
  179. 'admin ui' => array (
  180. 'path' => 'admin/structure/bio_data',
  181. 'controller class' => 'TripalBundleUIController',
  182. 'file' => 'includes/TripalBundleUIController.inc',
  183. 'menu wildcard' => '%TripalBundle',
  184. )
  185. );
  186. return $entities;
  187. }
  188. /**
  189. * Implements hook_entities_info_alter().
  190. *
  191. * Add in the bundles (entity types) to the TripalEntity entity.
  192. */
  193. function tripal_entity_info_alter(&$entity_info){
  194. if (array_key_exists('TripalEntity', $entity_info)) {
  195. // Dynamically add in the bundles. Bundles are alternative groups of fields
  196. // or configuration associated with an entity type .We want to dynamically
  197. // add the bundles to the entity.
  198. $bundles = db_select('tripal_bundle', 'tb')
  199. ->fields('tb')
  200. ->execute();
  201. while ($bundle = $bundles->fetchObject()) {
  202. $bundle_name = $bundle->name;
  203. $term_id = $bundle->term_id;
  204. $term = entity_load('TripalTerm', array('id' => $term_id));
  205. $term = reset($term);
  206. $label = preg_replace('/_/', ' ', ucwords($term->name));
  207. $entity_info['TripalEntity']['bundles'][$bundle_name] = array (
  208. 'label' => $label,
  209. 'admin' => array (
  210. 'path' => 'admin/structure/bio_data/manage/%TripalBundle',
  211. 'real path' => 'admin/structure/bio_data/manage/' . $bundle_name,
  212. 'bundle argument' => 4,
  213. 'access arguments' => array (
  214. 'manage tripal content types'
  215. )
  216. )
  217. );
  218. }
  219. }
  220. }
  221. /**
  222. * Checks access permissions for a given entity.
  223. *
  224. * This function is set for TripalEntity access checking in the
  225. * tripal_entity_info() under the 'access callback' element.
  226. *
  227. * @param $op
  228. * The operation. One of: create, view, edit, delete.
  229. * @param $entity
  230. * The entity to check access for.
  231. * @param $account
  232. * The user account.
  233. * @param $entity_type
  234. * The type of entity (will always be TripalEntity).
  235. */
  236. function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
  237. global $user;
  238. if ($entity) {
  239. $bundle_name = $entity->bundle;
  240. }
  241. else {
  242. return FALSE;
  243. }
  244. if (!isset($account)) {
  245. $account = $user;
  246. }
  247. switch ($op) {
  248. case 'create':
  249. return user_access('create ' . $bundle_name, $account);
  250. case 'view':
  251. return user_access('view ' . $bundle_name, $account);
  252. case 'edit':
  253. return user_access('edit ' . $bundle_name, $account);
  254. case 'delete':
  255. return user_access('delete ' . $bundle_name, $account);
  256. }
  257. return FALSE;
  258. }
  259. /**
  260. * Implements hook_entity_view.
  261. *
  262. * Here we want to overwite unattached fields with a div box that will be
  263. * recognized by JavaScript that will then use AJAX to load the field.
  264. *
  265. * The tripal_ajax_attach_field() function is called by an AJAX call to
  266. * retrieve the field.
  267. */
  268. function tripal_entity_view($entity, $type, $view_mode, $langcode) {
  269. if ($type == 'TripalEntity') {
  270. foreach (element_children($entity->content) as $child_name) {
  271. // Initailize the prefix and suffix for this field.
  272. if (!array_key_exists('#prefix', $entity->content[$child_name])) {
  273. $entity->content[$child_name]['#prefix'] = '';
  274. }
  275. if (!array_key_exists('#suffix', $entity->content[$child_name])) {
  276. $entity->content[$child_name]['#suffix'] = '';
  277. }
  278. // Surround the field with a <div> box for AJAX loading if this
  279. // field is unattached. this will allow JS code to automatically load
  280. // the field.
  281. $instance = field_info_instance('TripalEntity', $child_name, $entity->bundle);
  282. if ($instance and array_key_exists('settings', $instance)) {
  283. $class = '';
  284. if (array_key_exists('auto_attach', $instance['settings']) and
  285. $instance['settings']['auto_attach'] == FALSE and
  286. $entity->$child_name['#processed'] == FALSE) {
  287. // If the field is empty then try to use ajax to load it.
  288. $items = field_get_items('TripalEntity', $entity, $child_name);
  289. if (count($items) == 0 or empty($items[0]['value'])) {
  290. $class = 'class="tripal-entity-unattached"';
  291. }
  292. }
  293. $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" ' . $class . '>';
  294. $entity->content[$child_name]['#suffix'] .= '</div>';
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * Responds to an AJAX call for populating a field.
  301. *
  302. * @param $id
  303. * The ID of the HTML div box where the field is housed. The ID contains the
  304. * entity ID and field name.
  305. */
  306. function tripal_ajax_attach_field($id) {
  307. $matches = array();
  308. if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
  309. $entity_id = $matches[1];
  310. $field_name = $matches[2];
  311. $field = field_info_field($field_name);
  312. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
  313. reset($result);
  314. $entity = $result[$entity_id];
  315. // Get the element render array for this field and turn off the label
  316. // display. It's already on the page. We need to get the display from the
  317. // instance and pass it into the field_view_field. Otherwise it uses the
  318. // instance default display settings. Not sure why it does this. It needs
  319. // more investigation.
  320. $instance = field_info_instance('TripalEntity', $field_name, $entity->bundle);
  321. $element = field_view_field('TripalEntity', $entity, $field_name, $instance['display']['default']);
  322. $element['#label_display'] = 'hidden';
  323. $content = drupal_render($element);
  324. return drupal_json_output(array(
  325. 'id' => $id,
  326. 'content' => $content
  327. ));
  328. }
  329. }