TripalEntityController.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. /**
  3. * TripalEntityController extends DrupalDefaultEntityController.
  4. *
  5. * Our subclass of DrupalDefaultEntityController lets us add a few
  6. * important create, update, and delete methods.
  7. */
  8. class TripalEntityController extends EntityAPIController {
  9. public function __construct($entityType) {
  10. parent::__construct($entityType);
  11. }
  12. /**
  13. * Create a Tripal data entity
  14. *
  15. * We first set up the values that are specific
  16. * to our data schema but then also go through the EntityAPIController
  17. * function.
  18. *
  19. * @param $type
  20. * The machine-readable type of the entity.
  21. *
  22. * @return
  23. * An object with all default fields initialized.
  24. */
  25. public function create(array $values = array()) {
  26. // Add some items to the values array passed to the constructor
  27. global $user;
  28. $values['uid'] = $user->uid;
  29. $values['created'] = time();
  30. $values['changed'] = time();
  31. $values['title'] = '';
  32. $values['type'] = 'TripalEntity';
  33. $values['nid'] = '';
  34. $values['status'] = 1;
  35. // Call the parent constructor.
  36. $entity = parent::create($values);
  37. // Allow modules to make additions to the entity when it's created.
  38. $modules = module_implements('entity_create');
  39. foreach ($modules as $module) {
  40. $function = $module . '_entity_create';
  41. $function($entity, $values['type']);
  42. }
  43. return $entity;
  44. }
  45. /**
  46. * Overrides EntityAPIController::delete().
  47. *
  48. * @param array $ids
  49. * An array of the ids denoting which entities to delete.
  50. * @param DatabaseTransaction $transaction
  51. * Optionally a DatabaseTransaction object to use. Allows overrides to pass in
  52. * their transaction object.
  53. */
  54. public function delete($ids, $transaction = NULL) {
  55. if (!$transaction) {
  56. $transaction = db_transaction();
  57. }
  58. try {
  59. // First load the entity.
  60. $entities = entity_load('TripalEntity', $ids);
  61. // Then properly delete each one.
  62. foreach ($entities as $entity) {
  63. // Invoke hook_entity_delete().
  64. module_invoke_all('entity_delete', $entity, $entity->type);
  65. // Delete any field data for this entity.
  66. field_attach_delete('TripalEntity', $entity);
  67. // Delete the entity record from our base table.
  68. db_delete('tripal_entity')
  69. ->condition('id', $entity->id)
  70. ->execute();
  71. }
  72. }
  73. catch (Exception $e) {
  74. $transaction->rollback();
  75. watchdog_exception('tripal', $e);
  76. throw $e;
  77. return FALSE;
  78. }
  79. return TRUE;
  80. }
  81. /**
  82. * Sets the title for an entity.
  83. *
  84. * @param $entity
  85. * The entity whose title should be changed.
  86. * @param $title
  87. * The title to use. It can contain tokens the correspond to field values.
  88. * Token should be be compatible with those returned by
  89. * tripal_get_entity_tokens().
  90. */
  91. public function setTitle($entity, $title = NULL) {
  92. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  93. // If no title was supplied then we should try to generate one using the
  94. // default format set by admins.
  95. if (!$title) {
  96. $title = tripal_get_title_format($bundle_entity);
  97. }
  98. $title = tripal_replace_entity_tokens($title, $entity, $bundle_entity);
  99. if ($title) {
  100. db_update('tripal_entity')
  101. ->fields(array(
  102. 'title' => $title
  103. ))
  104. ->condition('id', $entity->id)
  105. ->execute();
  106. }
  107. }
  108. /**
  109. * Sets the URL alias for an entity.
  110. *
  111. * @param $entity
  112. * The entity whose URL alias should be changed.
  113. * @param $alias
  114. * The alias to use. It can contain tokens the correspond to field values.
  115. * Token should be be compatible with those returned by
  116. * tripal_get_entity_tokens().
  117. */
  118. public function setAlias($entity, $alias = NULL) {
  119. $source_url = "bio_data/$entity->id";
  120. // If no alias was supplied then we should try to generate one using the
  121. // default format set by admins.
  122. if (!$alias) {
  123. // Load the TripalBundle entity for this TripalEntity.
  124. // First get the format for the url alias based on the bundle of the entity.
  125. // Then replace all the tokens with values from the entity fields.
  126. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  127. $alias = tripal_get_bundle_variable('url_format', $bundle_entity->id);
  128. $alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
  129. }
  130. // If there is still no alias supplied then we should generate one using
  131. // the term name and entity id.
  132. if (!$alias) {
  133. // Load the term for this TripalEntity. Set a default based on the term
  134. // name and entity id. Then replace all the tokens with values from
  135. // the entity fields.
  136. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  137. $term = reset($term);
  138. $alias = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
  139. $alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
  140. }
  141. // Check if the passed alias has tokens. Load the TripalBundle entity for
  142. // this TripalEntity. Then replace all the tokens with values from the
  143. // entity fields.
  144. if($alias && (preg_match_all("/\[[^\]]*\]/", $alias, $bundle_tokens))) {
  145. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  146. $alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
  147. }
  148. // Make sure the alias doesn't contain spaces.
  149. //$alias = preg_replace('/\s+/','-',$alias);
  150. // Or any non alpha numeric characters.
  151. //$alias = preg_replace('/[^a-zA-Z0-9\-\/]/','',$alias);
  152. //$alias = preg_replace('/_/','-',$alias);
  153. if ($alias) {
  154. // Determine if this alias has already been used.
  155. $sql ='
  156. SELECT count(*) as num_alias
  157. FROM {url_alias}
  158. WHERE alias=:alias
  159. ';
  160. $num_aliases = db_query($sql, array(':alias' => $alias))->fetchField();
  161. // Either there isn't an alias yet so we just create one.
  162. // OR an Alias already exists but we would like to add a new one.
  163. if ($num_aliases == 0) {
  164. // First delete any previous alias' for this entity.
  165. // Then save the new one.
  166. // TODO: publishing an entity can be very slow if there are lots of
  167. // entries in the url_alias table, due to this type of
  168. // SQL statement that gets called somewhere by Drupal:
  169. // SELECT DISTINCT SUBSTRING_INDEX(source, '/', 1) AS path FROM url_alias.
  170. // Perhaps we should write our own SQL to avoid this issue.
  171. $values = array(
  172. 'source' => $source_url,
  173. 'alias' => $alias,
  174. 'language' => 'und',
  175. );
  176. // Now check if an entry with the source url for this entity already
  177. // exists. This is an issue when updating existing url aliases. To avoid
  178. // creating 404s existing aliases need to be updated and a redirect
  179. // created to handle the old alias.
  180. $existing_aliases = db_select('url_alias', 'ua')
  181. ->fields('ua')
  182. ->condition('source', $source_url, '=')
  183. ->execute()->fetchAll();
  184. $num_aliases = count($existing_aliases);
  185. if($num_aliases) {
  186. // For each existing entry create a redirect.
  187. foreach ($existing_aliases as $ea) {
  188. $path = [
  189. 'source' => $ea->source,
  190. 'alias' => $alias,
  191. 'pid' => $ea->pid,
  192. 'original' => [
  193. 'alias' => $ea->alias,
  194. 'pid' => $ea->pid,
  195. 'language' => $ea->language,
  196. ]
  197. ];
  198. module_load_include('module', 'redirect', 'redirect');
  199. redirect_path_update($path);
  200. //After redirects created now update the url_aliases table.
  201. db_update('url_alias')
  202. ->fields([
  203. 'alias' => $alias,
  204. ])
  205. ->condition('source', $source_url, '=')
  206. ->condition('pid', $ea->pid, '=')
  207. ->execute();
  208. }
  209. }
  210. else {
  211. drupal_write_record('url_alias', $values);
  212. }
  213. }
  214. // If there is only one alias matching then it might just be that we
  215. // already assigned this alias to this entity in a previous save.
  216. elseif ($num_aliases == 1) {
  217. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  218. // Check to see if the single alias is for the same entity and if not
  219. // warn the admin that the alias is already used (ie: not unique?)
  220. $sql = "
  221. SELECT count(*) as num_alias
  222. FROM {url_alias}
  223. WHERE alias=:alias AND source=:source
  224. ";
  225. $replace = array(':alias' => $alias, ':source' => $source_url);
  226. $same_alias = db_query($sql, $replace)->fetchField();
  227. if (!$same_alias) {
  228. $msg = 'The URL alias, %alias, already exists for another page. ' .
  229. 'Please ensure the pattern supplied on the <a href="!link" ' .
  230. 'target="_blank">%type Edit Page</a> under URL Path options is ' .
  231. 'unique.';
  232. $msg_var = array(
  233. '%alias' => $alias,
  234. '!link' => url("admin/structure/bio_data/manage/$entity->bundle"),
  235. '%type' => $bundle_entity->label
  236. );
  237. tripal_report_error('trpentity', TRIPAL_WARNING, $msg, $msg_var);
  238. drupal_set_message(t($msg, $msg_var), 'warning');
  239. }
  240. }
  241. // If there are more then one alias' matching what we generated then there's
  242. // a real problem and we need to warn the administrator.
  243. else {
  244. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  245. $aliases = db_query('SELECT source FROM {url_alias} WHERE alias=:alias',
  246. array(':alias' => $alias))->fetchAll();
  247. $pages = array();
  248. foreach($aliases as $a) {
  249. $pages[] = $a->source;
  250. }
  251. $msg = 'The URL alias, %alias, already exists for multiple pages! '.
  252. 'Please ensure the pattern supplied on the <a href="!link" ' .
  253. 'target="_blank">%type Edit Page</a> under URL Path options is ' .
  254. 'unique.';
  255. $msg_var = array(
  256. '%alias' => $alias,
  257. '!link' => url("admin/structure/bio_data/manage/$entity->bundle"),
  258. '%type' => $bundle_entity->label
  259. );
  260. drupal_set_message(t($msg, $msg_var), 'error');
  261. $msg .= ' This url alias has already been used for the following pages: %pages.
  262. You can manually delete alias\' using a combination of path_load() and path_delete().';
  263. $msg_var['%pages'] = implode(', ', $pages);
  264. tripal_report_error('trpentity', TRIPAL_ERROR, $msg, $msg_var);
  265. }
  266. }
  267. }
  268. /**
  269. * Saves a new entity.
  270. *
  271. * @param $entity
  272. * A TripalEntity object to save.
  273. *
  274. * @return
  275. * The saved entity object with updated properties.
  276. */
  277. public function save($entity, DatabaseTransaction $transaction = NULL) {
  278. global $user;
  279. $pkeys = array();
  280. // Get the author information.
  281. $author = $user;
  282. if (property_exists($entity, 'uid')) {
  283. $author = user_load($entity->uid);
  284. }
  285. $changed_date = time();
  286. $create_date = $changed_date;
  287. if (property_exists($entity, 'created')) {
  288. if (!is_numeric($entity->created)) {
  289. $temp = new DateTime($entity->created);
  290. $create_date = $temp->getTimestamp();
  291. }
  292. }
  293. $status = 1;
  294. if (property_exists($entity, 'status')) {
  295. if ($entity->status === 0 or $entity->status === 1) {
  296. $status = $entity->status;
  297. }
  298. }
  299. $transaction = isset($transaction) ? $transaction : db_transaction();
  300. try {
  301. // If our entity has no id, then we need to give it a
  302. // time of creation.
  303. if (empty($entity->id)) {
  304. $entity->created = time();
  305. $invocation = 'entity_insert';
  306. }
  307. else {
  308. $invocation = 'entity_update';
  309. $pkeys = array('id');
  310. }
  311. if (property_exists($entity, 'publish') and $entity->publish == TRUE) {
  312. $invocation = 'entity_publish';
  313. }
  314. // Invoke hook_entity_presave().
  315. module_invoke_all('entity_presave', $entity, $entity->type);
  316. // Write out the entity record.
  317. $record = array(
  318. 'term_id' => $entity->term_id,
  319. 'type' => $entity->type,
  320. 'bundle' => $entity->bundle,
  321. 'title' => $entity->title,
  322. 'uid' => $author->uid,
  323. 'created' => $create_date,
  324. 'changed' => $changed_date,
  325. 'status' => $status,
  326. );
  327. if (property_exists($entity, 'nid') and $entity->nid) {
  328. $record['nid'] = $entity->nid;
  329. }
  330. if ($invocation == 'entity_update') {
  331. $record['id'] = $entity->id;
  332. }
  333. $success = drupal_write_record('tripal_entity', $record, $pkeys);
  334. if ($success == SAVED_NEW) {
  335. $entity->id = $record['id'];
  336. }
  337. // Now we need to either insert or update the fields which are
  338. // attached to this entity. We use the same primary_keys logic
  339. // to determine whether to update or insert, and which hook we
  340. // need to invoke. We do not attach fields when publishing an entity.
  341. // This is because a field may have default values and if so, those fields
  342. // will be attached and the storage backend may then try to insert
  343. // fields which should not be inserted because they already exist.
  344. if ($invocation == 'entity_insert') {
  345. field_attach_insert('TripalEntity', $entity);
  346. }
  347. if ($invocation == 'entity_update') {
  348. field_attach_update('TripalEntity', $entity);
  349. }
  350. // Set the title for this entity.
  351. $this->setTitle($entity);
  352. // Set the path/url alias for this entity.
  353. $this->setAlias($entity);
  354. // Invoke either hook_entity_update() or hook_entity_insert().
  355. module_invoke_all('entity_postsave', $entity, $entity->type);
  356. module_invoke_all($invocation, $entity, $entity->type);
  357. // Clear any cache entries for this entity so it can be reloaded using
  358. // the values that were just saved.
  359. $cid = 'field:TripalEntity:' . $entity->id;
  360. cache_clear_all($cid, 'cache_field', TRUE);
  361. return $entity;
  362. }
  363. catch (Exception $e) {
  364. $transaction->rollback();
  365. watchdog_exception('tripal', $e);
  366. drupal_set_message("Could not save the entity: " . $e->getMessage(), "error");
  367. return FALSE;
  368. }
  369. }
  370. /**
  371. * Override the load function.
  372. *
  373. * A TripalEntity may have a large number of fields attached which may
  374. * slow down the loading of pages and web services. Therefore, we only
  375. * want to attach fields that are needed.
  376. *
  377. * @param $ids
  378. * The list of entity IDs to load.
  379. * @param $conditions
  380. * The list of key/value filters for querying the entity.
  381. * @param $field_ids
  382. * The list of numeric field IDs for fields that should be attached.
  383. * @param $cache
  384. * When loading of entities they can be cached with Drupal for later
  385. * faster loading. However, this can cause memory issues when running
  386. * Tripal jobs that load lots of entities. Caching of entities can
  387. * be disabled to improve memory performance by setting this to FALSE.
  388. */
  389. public function load($ids = array(), $conditions = array(), $field_ids = array(), $cache = TRUE) {
  390. $entities = array();
  391. // Revisions are not statically cached, and require a different query to
  392. // other conditions, so separate the revision id into its own variable.
  393. if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
  394. $revision_id = $conditions[$this->revisionKey];
  395. unset($conditions[$this->revisionKey]);
  396. }
  397. else {
  398. $revision_id = FALSE;
  399. }
  400. // Create a new variable which is either a prepared version of the $ids
  401. // array for later comparison with the entity cache, or FALSE if no $ids
  402. // were passed. The $ids array is reduced as items are loaded from cache,
  403. // and we need to know if it's empty for this reason to avoid querying the
  404. // database when all requested entities are loaded from cache.
  405. $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
  406. // Try to load entities from the static cache.
  407. if ($this->cache && !$revision_id) {
  408. $entities = $this->cacheGet($ids, $conditions);
  409. // If any entities were loaded, remove them from the ids still to load.
  410. if ($passed_ids) {
  411. $ids = array_keys(array_diff_key($passed_ids, $entities));
  412. }
  413. }
  414. // Support the entitycache module if activated.
  415. if (!empty($this->entityInfo['entity cache']) && !$revision_id && $ids && !$conditions) {
  416. $cached_entities = EntityCacheControllerHelper::entityCacheGet($this, $ids, $conditions);
  417. // If any entities were loaded, remove them from the ids still to load.
  418. $ids = array_diff($ids, array_keys($cached_entities));
  419. $entities += $cached_entities;
  420. // Add loaded entities to the static cache if we are not loading a
  421. // revision.
  422. if ($this->cache && !empty($cached_entities) && !$revision_id) {
  423. $this->cacheSet($cached_entities);
  424. }
  425. }
  426. // Load any remaining entities from the database. This is the case if $ids
  427. // is set to FALSE (so we load all entities), if there are any ids left to
  428. // load or if loading a revision.
  429. if (!($this->cacheComplete && $ids === FALSE && !$conditions) && ($ids === FALSE || $ids || $revision_id)) {
  430. $queried_entities = array();
  431. foreach ($this->query($ids, $conditions, $revision_id) as $record) {
  432. // Skip entities already retrieved from cache.
  433. if (isset($entities[$record->{$this->idKey}])) {
  434. continue;
  435. }
  436. // For DB-based entities take care of serialized columns.
  437. if (!empty($this->entityInfo['base table'])) {
  438. $schema = drupal_get_schema($this->entityInfo['base table']);
  439. foreach ($schema['fields'] as $field => $info) {
  440. if (!empty($info['serialize']) && isset($record->$field)) {
  441. $record->$field = unserialize($record->$field);
  442. // Support automatic merging of 'data' fields into the entity.
  443. if (!empty($info['merge']) && is_array($record->$field)) {
  444. foreach ($record->$field as $key => $value) {
  445. $record->$key = $value;
  446. }
  447. unset($record->$field);
  448. }
  449. }
  450. }
  451. }
  452. $queried_entities[$record->{$this->idKey}] = $record;
  453. }
  454. }
  455. // Pass all entities loaded from the database through $this->attachLoad(),
  456. // which attaches fields (if supported by the entity type) and calls the
  457. // entity type specific load callback, for example hook_node_load().
  458. if (!empty($queried_entities)) {
  459. $this->attachLoad($queried_entities, $revision_id, $field_ids);
  460. $entities += $queried_entities;
  461. }
  462. // Entity cache module support: Add entities to the entity cache if we are
  463. // not loading a revision.
  464. if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
  465. EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
  466. }
  467. if ($this->cache and $cache) {
  468. // Add entities to the cache if we are not loading a revision.
  469. if (!empty($queried_entities) && !$revision_id) {
  470. $this->cacheSet($queried_entities);
  471. // Remember if we have cached all entities now.
  472. if (!$conditions && $ids === FALSE) {
  473. $this->cacheComplete = TRUE;
  474. }
  475. }
  476. }
  477. // Ensure that the returned array is ordered the same as the original
  478. // $ids array if this was passed in and remove any invalid ids.
  479. if ($passed_ids && $passed_ids = array_intersect_key($passed_ids, $entities)) {
  480. foreach ($passed_ids as $id => $value) {
  481. $passed_ids[$id] = $entities[$id];
  482. }
  483. $entities = $passed_ids;
  484. }
  485. return $entities;
  486. }
  487. /**
  488. * Override the attachLoad function.
  489. *
  490. * A TripalEntity may have a large number of fields attached which may
  491. * slow down the loading of pages and web services. Therefore, we only
  492. * want to attach fields that are needed.
  493. *
  494. * @param $queried_entities
  495. * The list of queried
  496. * @param $revision_id
  497. * ID of the revision that was loaded, or FALSE if the most current
  498. * revision was loaded.
  499. * @param $field_ids
  500. */
  501. protected function attachLoad(&$queried_entities, $revision_id = FALSE,
  502. $field_ids = array()) {
  503. // Attach fields.
  504. if ($this->entityInfo['fieldable']) {
  505. if ($revision_id) {
  506. $function = 'field_attach_load_revision';
  507. }
  508. else {
  509. $function = 'field_attach_load';
  510. }
  511. foreach ($queried_entities as $id => $entity) {
  512. $info = entity_get_info($entity->type);
  513. $field_cache = array_key_exists('field cache', $info) ? $info['field cache'] : FALSE;
  514. $bundle_name = $entity->bundle;
  515. // Iterate through the field instances and find those that are set to
  516. // 'auto_attach' and which are attached to this bundle. Add all
  517. // fields that don't need auto attach to the field_ids array.
  518. $instances = field_info_instances('TripalEntity', $bundle_name);
  519. foreach ($instances as $instance) {
  520. $field_name = $instance['field_name'];
  521. $field = field_info_field($field_name);
  522. $field_id = $field['id'];
  523. // Add this field to the entity with default value.
  524. if (!isset($queried_entities[$id]->$field_name)) {
  525. $queried_entities[$id]->$field_name = array();
  526. }
  527. // Options used for the field_attach_load function.
  528. $options = array();
  529. $options['field_id'] = $field['id'];
  530. // The cache ID for the entity. We must manually set the cache
  531. // because the field_attach_load won't do it for us.
  532. $cfid = "field:TripalEntity:$id:$field_name";
  533. // Check if the field is cached. if so, then don't reload.
  534. if ($field_cache) {
  535. $cache_data = cache_get($cfid, 'cache_field');
  536. if (!empty($cache_data)) {
  537. $queried_entities[$id]->$field_name = $cache_data->data;
  538. $queried_entities[$id]->{$field_name}['#processed'] = TRUE;
  539. continue;
  540. }
  541. }
  542. // If a list of field_ids is provided then we specifically want
  543. // to only load the fields specified.
  544. if (count($field_ids) > 0) {
  545. if (in_array($field_id, $field_ids)) {
  546. $function($this->entityType, array($entity->id => $queried_entities[$id]),
  547. FIELD_LOAD_CURRENT, $options);
  548. // Cache the field.
  549. if ($field_cache) {
  550. cache_set($cfid, $entity->$field_name, 'cache_field');
  551. }
  552. $queried_entities[$id]->{$field_name}['#processed'] = TRUE;
  553. }
  554. }
  555. // If we don't have a list of fields then load them all, but only
  556. // if the instance is a TripalField and it is set to not auto
  557. // attach then we will ignore it. It can only be set by providing
  558. // the id in the $field_id array handled previously.
  559. else {
  560. // We only load via AJAX if empty fields are not hidden.
  561. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  562. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  563. if (array_key_exists('settings', $instance) and
  564. array_key_exists('auto_attach', $instance['settings']) and
  565. $instance['settings']['auto_attach'] == FALSE and
  566. $hide_variable == 'show') {
  567. // Add an empty value. This will allow the tripal_entity_view()
  568. // hook to add the necessary prefixes to the field for ajax
  569. // loading.
  570. $queried_entities[$id]->$field_name['und'][0]['value'] = '';
  571. $queried_entities[$id]->{$field_name}['#processed'] = FALSE;
  572. }
  573. else {
  574. $function($this->entityType, array($entity->id => $queried_entities[$id]),
  575. FIELD_LOAD_CURRENT, $options);
  576. // Cache the field.
  577. if ($field_cache) {
  578. if (property_exists($entity, $field_name)) {
  579. cache_set($cfid, $entity->$field_name, 'cache_field');
  580. }
  581. }
  582. $queried_entities[$id]->{$field_name}['#processed'] = TRUE;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. // Call hook_entity_load().
  589. foreach (module_implements('entity_load') as $module) {
  590. $function = $module . '_entity_load';
  591. $function($queried_entities, $this->entityType);
  592. }
  593. // Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
  594. // always the queried entities, followed by additional arguments set in
  595. // $this->hookLoadArguments.
  596. $args = array_merge(array($queried_entities), $this->hookLoadArguments);
  597. foreach (module_implements($this->entityInfo['load hook']) as $module) {
  598. call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
  599. }
  600. }
  601. }