tripal.entity.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. * Implements hook_entity_property_info_alter().
  223. *
  224. * For some reason not all our TripalFields end up in the properties field for
  225. * each bundle. This becomes a problem with Search API integration because only
  226. * fields listed in the properties for a bundle are available to be indexed.
  227. * Thus we are altering the property info to add any fields attached to
  228. * TripalEntities which may have been missed.
  229. *
  230. * Furthermore, there are some pecularities with how TripalFields store their
  231. * value that causes the default getter callback difficulties in some edge cases.
  232. * Thus we are overriding that function below.
  233. */
  234. function tripal_entity_property_info_alter(&$info) {
  235. // For each Tripal Content Type, we want to ensure all attached fields
  236. // are added to the bundle properties.
  237. foreach ($info['TripalEntity']['bundles'] as $bundle_name => $bundle) {
  238. // Retrieve information for all fields attached to this Tripal Content Type.
  239. $fields = field_info_instances('TripalEntity', $bundle_name);
  240. foreach ($fields as $field_name => $field_info) {
  241. // If there is a field attached to the current Tripal Content Type that
  242. // is not listed in properties, then add it. We use the full defaults here
  243. // just in case it's not a TripalField or ChadoField.
  244. if (!isset($info['TripalEntity']['bundles'][$bundle_name]['properties'][$field_name])) {
  245. $info['TripalEntity']['bundles'][$bundle_name]['properties'][$field_name] = array(
  246. 'label' => $field_info['label'],
  247. 'type' => 'text',
  248. 'description' => $field_info['description'],
  249. 'getter callback' => 'entity_metadata_field_property_get',
  250. 'setter callback' => 'entity_metadata_field_property_set',
  251. 'access callback' => 'entity_metadata_field_access_callback',
  252. 'query callback' => 'entity_metadata_field_query',
  253. 'translatable' => FALSE,
  254. 'field' => TRUE,
  255. 'required' => $field_info['required'],
  256. );
  257. }
  258. // Now, if it's a TripalField or a ChadoField, then we want to use a custom
  259. // getter callback in order to ensure values are retrieved properly.
  260. // ASSUMPTION: All TripalFields and ChadoFields have an ontology term
  261. // defining them.
  262. if (isset($field_info['settings']['term_accession'])) {
  263. $info['TripalEntity']['bundles'][$bundle_name]['properties'][$field_name]['getter callback'] = 'tripal_field_property_get';
  264. }
  265. }
  266. }
  267. }
  268. /**
  269. * Callback for getting TripalField and ChadoField property values.
  270. *
  271. * This function retrieves the value from a field. Since the value has already
  272. * been set by the Tripal/ChadoField class at this point, it should just be a
  273. * matter of grabbing the value.
  274. *
  275. * @param $entity
  276. * The fully-loaded entity object to be indexed.
  277. * @param $options
  278. * Options that can be ued when retrieving the value.
  279. * @param $field_name
  280. * The machine name of the field we want to retrieve.
  281. * @param $entity_type
  282. * The type of entity (ie: TripalEntity).
  283. *
  284. * @return
  285. * The rendered value of the field specified by $field_name.
  286. */
  287. function tripal_field_property_get($entity, array $options, $field_name, $entity_type, $info) {
  288. // Retrieve information for the field.
  289. $field = field_info_field($field_name);
  290. // Retrieve the language code.
  291. $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE;
  292. $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode, TRUE);
  293. $values = array();
  294. if (isset($entity->{$field_name}[$langcode])) {
  295. // For each value of the field... (this will be multiple if cardinality is > 1).
  296. foreach ($entity->{$field_name}[$langcode] as $delta => $data) {
  297. // All Tripal/ChadoFields should have a value key. Only the information
  298. // stored in this value key should be displayed on the page, available
  299. // via web services or indexed for searching. This is there is no value
  300. // key, we will not index anything.
  301. if (!isset($data['value'])) {
  302. return NULL;
  303. }
  304. // Sometimes TripalFields may return multiple pieces of information in the
  305. // value field. In this case, the key should be an ontology term describing
  306. // what each piece of data is and the value should be the data.
  307. if (is_array($data['value'])) {
  308. // Just include all the pieces of information seperated by spaces
  309. // so they are tokenized out later on.
  310. $tmp = $data['value'];
  311. if (isset($tmp['entity'])) { unset($tmp['entity']); }
  312. foreach ($tmp as $k => $v) { $tmp[$k] = strip_tags($v); }
  313. $curr_val = implode(' ', $tmp);
  314. }
  315. else {
  316. // Otherwise, assume the value is a single piece of information
  317. // and add that directly to be indexed.
  318. $curr_val = strip_tags($data['value']);
  319. // Ensure that we have a clean boolean data type.
  320. if ($info['type'] == 'boolean' || $info['type'] == 'list<boolean>') {
  321. $curr_val = (boolean) $curr_val;
  322. }
  323. }
  324. // Only add the current value if it's not empty.
  325. if (!empty(trim($curr_val))) {
  326. $values[$delta] = $curr_val;
  327. }
  328. }
  329. }
  330. // For an empty single-valued field, we have to return NULL.
  331. return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values;
  332. }
  333. /**
  334. * Checks access permissions for a given entity.
  335. *
  336. * This function is set for TripalEntity access checking in the
  337. * tripal_entity_info() under the 'access callback' element.
  338. *
  339. * @param $op
  340. * The operation. One of: create, view, edit, delete.
  341. * @param $entity
  342. * The entity to check access for.
  343. * @param $account
  344. * The user account.
  345. * @param $entity_type
  346. * The type of entity (will always be TripalEntity).
  347. */
  348. function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
  349. global $user;
  350. if ($entity) {
  351. $bundle_name = $entity->bundle;
  352. }
  353. else {
  354. return FALSE;
  355. }
  356. if (!isset($account)) {
  357. $account = $user;
  358. }
  359. switch ($op) {
  360. case 'create':
  361. return user_access('create ' . $bundle_name, $account);
  362. case 'view':
  363. return user_access('view ' . $bundle_name, $account);
  364. case 'edit':
  365. return user_access('edit ' . $bundle_name, $account);
  366. case 'delete':
  367. return user_access('delete ' . $bundle_name, $account);
  368. }
  369. return FALSE;
  370. }
  371. /**
  372. * Implements hook_entity_view.
  373. *
  374. * Here we want to overwite unattached fields with a div box that will be
  375. * recognized by JavaScript that will then use AJAX to load the field.
  376. *
  377. * The tripal_ajax_attach_field() function is called by an AJAX call to
  378. * retrieve the field.
  379. */
  380. function tripal_entity_view($entity, $type, $view_mode, $langcode) {
  381. if ($type == 'TripalEntity') {
  382. foreach (element_children($entity->content) as $child_name) {
  383. // Initailize the prefix and suffix for this field.
  384. if (!array_key_exists('#prefix', $entity->content[$child_name])) {
  385. $entity->content[$child_name]['#prefix'] = '';
  386. }
  387. if (!array_key_exists('#suffix', $entity->content[$child_name])) {
  388. $entity->content[$child_name]['#suffix'] = '';
  389. }
  390. // Surround the field with a <div> box for AJAX loading if this
  391. // field is unattached. this will allow JS code to automatically load
  392. // the field.
  393. $instance = field_info_instance('TripalEntity', $child_name, $entity->bundle);
  394. if ($instance and array_key_exists('settings', $instance)) {
  395. $class = '';
  396. if (array_key_exists('auto_attach', $instance['settings']) and
  397. $instance['settings']['auto_attach'] == FALSE and
  398. $entity->{$child_name}['#processed'] == FALSE) {
  399. // If the field is empty then try to use ajax to load it.
  400. $items = field_get_items('TripalEntity', $entity, $child_name);
  401. if (count($items) == 0 or empty($items[0]['value'])) {
  402. $class = 'class="tripal-entity-unattached"';
  403. }
  404. }
  405. $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" ' . $class . '>';
  406. $entity->content[$child_name]['#suffix'] .= '</div>';
  407. }
  408. }
  409. }
  410. }
  411. /**
  412. * Responds to an AJAX call for populating a field.
  413. *
  414. * @param $id
  415. * The ID of the HTML div box where the field is housed. The ID contains the
  416. * entity ID and field name.
  417. */
  418. function tripal_ajax_attach_field($id) {
  419. $matches = array();
  420. if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
  421. $entity_id = $matches[1];
  422. $field_name = $matches[2];
  423. $field = field_info_field($field_name);
  424. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
  425. reset($result);
  426. $entity = $result[$entity_id];
  427. // Get the element render array for this field and turn off the label
  428. // display. It's already on the page. We need to get the display from the
  429. // instance and pass it into the field_view_field. Otherwise it uses the
  430. // instance default display settings. Not sure why it does this. It needs
  431. // more investigation.
  432. $instance = field_info_instance('TripalEntity', $field_name, $entity->bundle);
  433. $element = field_view_field('TripalEntity', $entity, $field_name, $instance['display']['default']);
  434. $element['#label_display'] = 'hidden';
  435. $content = drupal_render($element);
  436. return drupal_json_output(array(
  437. 'id' => $id,
  438. 'content' => $content
  439. ));
  440. }
  441. }