tripal.entity.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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_entity_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. // Search integration
  158. if (module_exists('search')) {
  159. $entities['TripalEntity']['view modes'] += array(
  160. 'search_index' => array(
  161. 'label' => t('Search index'),
  162. 'custom settings' => FALSE,
  163. ),
  164. 'search_result' => array(
  165. 'label' => t('Search result highlighting input'),
  166. 'custom settings' => FALSE,
  167. ),
  168. );
  169. }
  170. // The TripalBundle entity is used manage the bundle types. The 'bundle of'
  171. // attribute links this to the TripalEntity and allows the UI provided
  172. // by the entity module to work for each TripalEntity bundle.
  173. //
  174. $entities['TripalBundle'] = array (
  175. 'label' => 'Tripal Content Type',
  176. 'entity class' => 'TripalBundle',
  177. 'controller class' => 'TripalBundleController',
  178. 'views controller class' => 'TripalBundleViewsController',
  179. 'base table' => 'tripal_bundle',
  180. 'fieldable' => FALSE,
  181. 'bundle of' => 'TripalEntity',
  182. 'exportable' => FALSE,
  183. 'entity keys' => array (
  184. 'id' => 'id',
  185. 'name' => 'name',
  186. 'label' => 'label'
  187. ),
  188. 'access callback' => 'tripal_bundle_access',
  189. 'module' => 'tripal',
  190. // Enable the entity API's admin UI.
  191. 'admin ui' => array (
  192. 'path' => 'admin/structure/bio_data',
  193. 'controller class' => 'TripalBundleUIController',
  194. 'file' => 'includes/TripalBundleUIController.inc',
  195. 'menu wildcard' => '%TripalBundle',
  196. )
  197. );
  198. return $entities;
  199. }
  200. /**
  201. * Implements the Entity URI callback function.
  202. */
  203. function tripal_entity_uri($entity) {
  204. return array(
  205. 'path' => 'bio-data/' . $entity->id,
  206. 'options' => array(),
  207. );
  208. }
  209. /**
  210. * Implements hook_entities_info_alter().
  211. *
  212. * Add in the bundles (entity types) to the TripalEntity entity.
  213. */
  214. function tripal_entity_info_alter(&$entity_info){
  215. if (array_key_exists('TripalEntity', $entity_info)) {
  216. // Dynamically add in the bundles. Bundles are alternative groups of fields
  217. // or configuration associated with an entity type .We want to dynamically
  218. // add the bundles to the entity.
  219. $bundles = db_select('tripal_bundle', 'tb')
  220. ->fields('tb')
  221. ->execute();
  222. while ($bundle = $bundles->fetchObject()) {
  223. $bundle_name = $bundle->name;
  224. $term_id = $bundle->term_id;
  225. $term = entity_load('TripalTerm', array('id' => $term_id));
  226. $term = reset($term);
  227. $label = preg_replace('/_/', ' ', ucwords($term->name));
  228. $entity_info['TripalEntity']['bundles'][$bundle_name] = array (
  229. 'label' => $label,
  230. 'admin' => array (
  231. 'path' => 'admin/structure/bio_data/manage/%TripalBundle',
  232. 'real path' => 'admin/structure/bio_data/manage/' . $bundle_name,
  233. 'bundle argument' => 4,
  234. 'access arguments' => array (
  235. 'manage tripal content types'
  236. )
  237. )
  238. );
  239. }
  240. }
  241. }
  242. /**
  243. * Implements hook_entity_property_info_alter().
  244. *
  245. * For some reason not all our TripalFields end up in the properties field for
  246. * each bundle. This becomes a problem with Search API integration because only
  247. * fields listed in the properties for a bundle are available to be indexed.
  248. * Thus we are altering the property info to add any fields attached to
  249. * TripalEntities which may have been missed.
  250. *
  251. * Furthermore, there are some pecularities with how TripalFields store their
  252. * value that causes the default getter callback difficulties in some edge cases.
  253. * Thus we are overriding that function below.
  254. */
  255. function tripal_entity_property_info_alter(&$info) {
  256. // Sometimes this function is called when there are no Tripal Entities.
  257. // Don't bother to do anything in this case.
  258. if (!isset($info['TripalEntity']['bundles'])) { return TRUE; }
  259. // For each Tripal Content Type, we want to ensure all attached fields
  260. // are added to the bundle properties.
  261. foreach ($info['TripalEntity']['bundles'] as $bundle_name => $bundle) {
  262. // Retrieve information for all fields attached to this Tripal Content Type.
  263. $fields = field_info_instances('TripalEntity', $bundle_name);
  264. foreach ($fields as $field_name => $field_info) {
  265. // If there is a field attached to the current Tripal Content Type that
  266. // is not listed in properties, then add it. We use the full defaults here
  267. // just in case it's not a TripalField or ChadoField.
  268. if (!isset($info['TripalEntity']['bundles'][$bundle_name]['properties'][$field_name])) {
  269. $info['TripalEntity']['bundles'][$bundle_name]['properties'][$field_name] = array(
  270. 'label' => $field_info['label'],
  271. 'type' => 'text',
  272. 'description' => $field_info['description'],
  273. 'getter callback' => 'entity_metadata_field_property_get',
  274. 'setter callback' => 'entity_metadata_field_property_set',
  275. 'access callback' => 'entity_metadata_field_access_callback',
  276. 'query callback' => 'entity_metadata_field_query',
  277. 'translatable' => FALSE,
  278. 'field' => TRUE,
  279. 'required' => $field_info['required'],
  280. );
  281. }
  282. // Now, if it's a TripalField or a ChadoField, then we want to use a custom
  283. // getter callback in order to ensure values are retrieved properly.
  284. // ASSUMPTION: All TripalFields and ChadoFields have an ontology term
  285. // defining them.
  286. if (isset($field_info['settings']['term_accession'])) {
  287. $info['TripalEntity']['bundles'][$bundle_name]['properties'][$field_name]['getter callback'] = 'tripal_field_property_get';
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Callback for getting TripalField and ChadoField property values.
  294. *
  295. * This function retrieves the value from a field. Since the value has already
  296. * been set by the Tripal/ChadoField class at this point, it should just be a
  297. * matter of grabbing the value.
  298. *
  299. * @param $entity
  300. * The fully-loaded entity object to be indexed.
  301. * @param $options
  302. * Options that can be ued when retrieving the value.
  303. * @param $field_name
  304. * The machine name of the field we want to retrieve.
  305. * @param $entity_type
  306. * The type of entity (ie: TripalEntity).
  307. *
  308. * @return
  309. * The rendered value of the field specified by $field_name.
  310. */
  311. function tripal_field_property_get($entity, array $options, $field_name, $entity_type, $info) {
  312. // Retrieve information for the field.
  313. $field = field_info_field($field_name);
  314. // Retrieve the language code.
  315. $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE;
  316. $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode, TRUE);
  317. $values = array();
  318. if (isset($entity->{$field_name}[$langcode])) {
  319. // For each value of the field... (this will be multiple if cardinality is > 1).
  320. foreach ($entity->{$field_name}[$langcode] as $delta => $data) {
  321. // All Tripal/ChadoFields should have a value key. Only the information
  322. // stored in this value key should be displayed on the page, available
  323. // via web services or indexed for searching. This is there is no value
  324. // key, we will not index anything.
  325. if (!isset($data['value'])) {
  326. return NULL;
  327. }
  328. // Sometimes TripalFields may return multiple pieces of information in the
  329. // value field. In this case, the key should be an ontology term describing
  330. // what each piece of data is and the value should be the data.
  331. if (is_array($data['value'])) {
  332. // Just include all the pieces of information seperated by spaces
  333. // so they are tokenized out later on.
  334. $tmp = $data['value'];
  335. if (isset($tmp['entity'])) { unset($tmp['entity']); }
  336. foreach ($tmp as $k => $v) { $tmp[$k] = strip_tags($v); }
  337. $curr_val = implode(' ', $tmp);
  338. }
  339. else {
  340. // Otherwise, assume the value is a single piece of information
  341. // and add that directly to be indexed.
  342. $curr_val = strip_tags($data['value']);
  343. // Ensure that we have a clean boolean data type.
  344. if ($info['type'] == 'boolean' || $info['type'] == 'list<boolean>') {
  345. $curr_val = (boolean) $curr_val;
  346. }
  347. }
  348. // Only add the current value if it's not empty.
  349. if (!empty(trim($curr_val))) {
  350. $values[$delta] = $curr_val;
  351. }
  352. }
  353. }
  354. // For an empty single-valued field, we have to return NULL.
  355. return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values;
  356. }
  357. /**
  358. * Checks access permissions for a given entity.
  359. *
  360. * This function is set for TripalEntity access checking in the
  361. * tripal_entity_info() under the 'access callback' element.
  362. *
  363. * @param $op
  364. * The operation. One of: create, view, edit, delete.
  365. * @param $entity
  366. * The entity to check access for.
  367. * @param $account
  368. * The user account.
  369. * @param $entity_type
  370. * The type of entity (will always be TripalEntity).
  371. */
  372. function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
  373. global $user;
  374. if ($entity) {
  375. $bundle_name = $entity->bundle;
  376. }
  377. else {
  378. return FALSE;
  379. }
  380. if (!isset($account)) {
  381. $account = $user;
  382. }
  383. if (!$entity_type) {
  384. $entity_type = $entity->type;
  385. }
  386. // See if other modules want to adust permissions.
  387. $results = module_invoke_all($entity_type . '_access', $entity, $op, $account);
  388. if (in_array(TRUE, $results)) {
  389. return TRUE;
  390. }
  391. switch ($op) {
  392. case 'create':
  393. return user_access('create ' . $bundle_name, $account);
  394. case 'view':
  395. return user_access('view ' . $bundle_name, $account);
  396. case 'edit':
  397. return user_access('edit ' . $bundle_name, $account);
  398. case 'delete':
  399. return user_access('delete ' . $bundle_name, $account);
  400. }
  401. return FALSE;
  402. }
  403. /**
  404. * Implements hook_entity_view.
  405. *
  406. * Here we want to overwite unattached fields with a div box that will be
  407. * recognized by JavaScript that will then use AJAX to load the field.
  408. *
  409. * The tripal_ajax_attach_field() function is called by an AJAX call to
  410. * retrieve the field.
  411. */
  412. function tripal_entity_view($entity, $type, $view_mode, $langcode) {
  413. if ($type == 'TripalEntity') {
  414. foreach (element_children($entity->content) as $child_name) {
  415. // Initailize the prefix and suffix for this field.
  416. if (!array_key_exists('#prefix', $entity->content[$child_name])) {
  417. $entity->content[$child_name]['#prefix'] = '';
  418. }
  419. if (!array_key_exists('#suffix', $entity->content[$child_name])) {
  420. $entity->content[$child_name]['#suffix'] = '';
  421. }
  422. // Surround the field with a <div> box for AJAX loading if this
  423. // field is unattached. this will allow JS code to automatically load
  424. // the field.
  425. $instance = field_info_instance('TripalEntity', $child_name, $entity->bundle);
  426. if ($instance and array_key_exists('settings', $instance)) {
  427. $class = '';
  428. if (array_key_exists('auto_attach', $instance['settings']) and
  429. $instance['settings']['auto_attach'] == FALSE and
  430. $entity->{$child_name}['#processed'] == FALSE) {
  431. // If the field is empty then try to use ajax to load it.
  432. $items = field_get_items('TripalEntity', $entity, $child_name);
  433. if (count($items) == 0 or empty($items[0]['value'])) {
  434. $class = 'class="tripal-entity-unattached"';
  435. }
  436. }
  437. $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" ' . $class . '>';
  438. $entity->content[$child_name]['#suffix'] .= '</div>';
  439. }
  440. }
  441. }
  442. }
  443. /**
  444. * Responds to an AJAX call for populating a field.
  445. *
  446. * @param $id
  447. * The ID of the HTML div box where the field is housed. The ID contains the
  448. * entity ID and field name.
  449. */
  450. function tripal_ajax_attach_field($id) {
  451. $matches = array();
  452. if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
  453. $entity_id = $matches[1];
  454. $field_name = $matches[2];
  455. $field = field_info_field($field_name);
  456. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
  457. reset($result);
  458. $entity = $result[$entity_id];
  459. // Get the element render array for this field and turn off the label
  460. // display. It's already on the page. We need to get the display from the
  461. // instance and pass it into the field_view_field. Otherwise it uses the
  462. // instance default display settings. Not sure why it does this. It needs
  463. // more investigation.
  464. $instance = field_info_instance('TripalEntity', $field_name, $entity->bundle);
  465. $element = field_view_field('TripalEntity', $entity, $field_name, $instance['display']['default']);
  466. $element['#label_display'] = 'hidden';
  467. $content = drupal_render($element);
  468. return drupal_json_output(array(
  469. 'id' => $id,
  470. 'content' => $content
  471. ));
  472. }
  473. }