TripalEntityController.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. * @param $title
  86. */
  87. public function setTitle($entity, $title = NULL) {
  88. // If no title was supplied then we should try to generate one using the
  89. // default format set by admins.
  90. if (!$title) {
  91. // Load the TripalBundle entity for this TripalEntity.
  92. // Get the format for the title based on the bundle of the entity.
  93. // And then replace all the tokens with values from the entity fields.
  94. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  95. $title = tripal_get_title_format($bundle_entity);
  96. $title = tripal_replace_entity_tokens($title, $entity, $bundle_entity);
  97. }
  98. // Check if the passed alias has tokens.
  99. if($title && (preg_match_all("/\[[^\]]*\]/", $title, $bundle_tokens))) {
  100. // Load the TripalBundle entity for this TripalEntity.
  101. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  102. // And then replace all the tokens with values from the entity fields.
  103. $title = tripal_replace_entity_tokens($title, $entity, $bundle_entity);
  104. }
  105. // As long as we were able to determine a title, we should update it ;-).
  106. if ($title) {
  107. db_update('tripal_entity')
  108. ->fields(array(
  109. 'title' => $title
  110. ))
  111. ->condition('id', $entity->id)
  112. ->execute();
  113. }
  114. }
  115. /**
  116. * Sets the URL alias for an entity.
  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. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  125. // First get the format for the url alias based on the bundle of the entity.
  126. $alias = tripal_get_bundle_variable('url_format', $bundle_entity->id);
  127. // And then replace all the tokens with values from the entity fields.
  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.
  134. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  135. $term = reset($term);
  136. // Set a default based on the term name and entity id.
  137. $alias = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
  138. // And then replace all the tokens with values from the entity fields.
  139. $alias = tripal_replace_entity_tokens($alias, $entity, $bundle_entity);
  140. }
  141. // Check if the passed alias has tokens.
  142. if($alias && (preg_match_all("/\[[^\]]*\]/", $alias, $bundle_tokens))) {
  143. // Load the TripalBundle entity for this TripalEntity.
  144. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  145. // And then replace all the tokens with values from the entity fields.
  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. // path_delete(array('source' => $source_url));
  177. // $path = array('source' => $source_url, 'alias' => $alias);
  178. // path_save($path);
  179. //Now check if an entry with the source url for this entity already
  180. // exists. This is an issue when updating existing url aliases. To avoid
  181. // creating 404s existing aliases need to be updated and a redirect
  182. // created to handle the old alias.
  183. $existing_aliases = db_select('url_alias', 'ua')
  184. ->fields('ua')
  185. ->condition('source', $source_url, '=')
  186. ->execute()->fetchAll();
  187. $num_aliases = count($existing_aliases);
  188. if($num_aliases) {
  189. // For each existing entry create a redirect.
  190. foreach ($existing_aliases as $ea) {
  191. $path = [
  192. 'source' => $ea->source,
  193. 'alias' => $alias,
  194. 'pid' => $ea->pid,
  195. 'original' => [
  196. 'alias' => $ea->alias,
  197. 'pid' => $ea->pid,
  198. 'language' => $ea->language,
  199. ]
  200. ];
  201. module_load_include('module', 'redirect', 'redirect');
  202. redirect_path_update($path);
  203. //After redirects created now update the url_aliases table.
  204. db_update('url_alias')
  205. ->fields([
  206. 'alias' => $alias,
  207. ])
  208. ->condition('source', $source_url, '=')
  209. ->condition('pid', $ea->pid, '=')
  210. ->execute();
  211. }
  212. }
  213. else {
  214. drupal_write_record('url_alias', $values);
  215. }
  216. }
  217. // If there is only one alias matching then it might just be that we already
  218. // assigned this alias to this entity in a previous save.
  219. elseif ($num_aliases == 1) {
  220. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  221. // Check to see if the single alias is for the same entity and if not
  222. // warn the admin that the alias is already used (ie: not unique?)
  223. $sql = "
  224. SELECT count(*) as num_alias
  225. FROM {url_alias}
  226. WHERE alias=:alias AND source=:source
  227. ";
  228. $replace = array(':alias' => $alias, ':source' => $source_url);
  229. $same_alias = db_query($sql, $replace)->fetchField();
  230. if (!$same_alias) {
  231. $msg = 'The URL alias, %alias, already exists for another page. ' .
  232. 'Please ensure the pattern supplied on the <a href="!link" ' .
  233. 'target="_blank">%type Edit Page</a> under URL Path options is ' .
  234. 'unique.';
  235. $msg_var = array(
  236. '%alias' => $alias,
  237. '!link' => url("admin/structure/bio_data/manage/$entity->bundle"),
  238. '%type' => $bundle_entity->label
  239. );
  240. tripal_report_error('trpentity', TRIPAL_WARNING, $msg, $msg_var);
  241. drupal_set_message(t($msg, $msg_var), 'warning');
  242. }
  243. }
  244. // If there are more then one alias' matching what we generated then there's
  245. // a real problem and we need to warn the administrator.
  246. else {
  247. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  248. $aliases = db_query('SELECT source FROM {url_alias} WHERE alias=:alias',
  249. array(':alias' => $alias))->fetchAll();
  250. $pages = array();
  251. foreach($aliases as $a) {
  252. $pages[] = $a->source;
  253. }
  254. $msg = 'The URL alias, %alias, already exists for multiple pages! '.
  255. 'Please ensure the pattern supplied on the <a href="!link" ' .
  256. 'target="_blank">%type Edit Page</a> under URL Path options is ' .
  257. 'unique.';
  258. $msg_var = array(
  259. '%alias' => $alias,
  260. '!link' => url("admin/structure/bio_data/manage/$entity->bundle"),
  261. '%type' => $bundle_entity->label
  262. );
  263. drupal_set_message(t($msg, $msg_var), 'error');
  264. $msg .= ' This url alias has already been used for the following pages: %pages.
  265. You can manually delete alias\' using a combination of path_load() and path_delete().';
  266. $msg_var['%pages'] = implode(', ', $pages);
  267. tripal_report_error('trpentity', TRIPAL_ERROR, $msg, $msg_var);
  268. }
  269. }
  270. }
  271. /**
  272. * Saves a new entity.
  273. *
  274. * @param $entity
  275. * A TripalEntity object to save.
  276. *
  277. * @return
  278. * The saved entity object with updated properties.
  279. */
  280. public function save($entity) {
  281. global $user;
  282. $pkeys = array();
  283. // Get the author information.
  284. $author = $user;
  285. if (property_exists($entity, 'uid')) {
  286. $author = user_load($entity->uid);
  287. }
  288. $changed_date = time();
  289. $create_date = $changed_date;
  290. if (property_exists($entity, 'created')) {
  291. if (!is_numeric($entity->created)) {
  292. $temp = new DateTime($entity->created);
  293. $create_date = $temp->getTimestamp();
  294. }
  295. }
  296. $status = 1;
  297. if (property_exists($entity, 'status')) {
  298. if ($entity->status === 0 or $entity->status === 1) {
  299. $status = $entity->status;
  300. }
  301. }
  302. $transaction = db_transaction();
  303. try {
  304. // If our entity has no id, then we need to give it a
  305. // time of creation.
  306. if (empty($entity->id)) {
  307. $entity->created = time();
  308. $invocation = 'entity_insert';
  309. }
  310. else {
  311. $invocation = 'entity_update';
  312. $pkeys = array('id');
  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.
  341. if ($invocation == 'entity_insert') {
  342. field_attach_insert('TripalEntity', $entity);
  343. }
  344. else {
  345. field_attach_update('TripalEntity', $entity);
  346. }
  347. // Set the title for this entity.
  348. $this->setTitle($entity);
  349. // Set the path/url alias for this entity.
  350. $this->setAlias($entity);
  351. // Invoke either hook_entity_update() or hook_entity_insert().
  352. module_invoke_all('entity_postsave', $entity, $entity->type);
  353. module_invoke_all($invocation, $entity, $entity->type);
  354. // Clear any cache entries for this entity so it can be reloaded using
  355. // the values that were just saved.
  356. $cid = 'field:TripalEntity:' . $entity->id;
  357. cache_clear_all($cid, 'cache_field', TRUE);
  358. return $entity;
  359. }
  360. catch (Exception $e) {
  361. $transaction->rollback();
  362. watchdog_exception('tripal', $e);
  363. drupal_set_message("Could not save the entity: " . $e->getMessage(), "error");
  364. return FALSE;
  365. }
  366. }
  367. /**
  368. * Override the load function.
  369. *
  370. * A TripalEntity may have a large number of fields attached which may
  371. * slow down the loading of pages and web services. Therefore, we only
  372. * want to attach fields that are needed.
  373. *
  374. * @param $ids
  375. * The list of entity IDs to load.
  376. * @param $conditions
  377. * The list of key/value filters for querying the entity.
  378. * @param $field_ids
  379. * The list of numeric field IDs for fields that should be attached.
  380. */
  381. public function load($ids = array(), $conditions = array(), $field_ids = array()) {
  382. $entities = array();
  383. // Revisions are not statically cached, and require a different query to
  384. // other conditions, so separate the revision id into its own variable.
  385. if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
  386. $revision_id = $conditions[$this->revisionKey];
  387. unset($conditions[$this->revisionKey]);
  388. }
  389. else {
  390. $revision_id = FALSE;
  391. }
  392. // Create a new variable which is either a prepared version of the $ids
  393. // array for later comparison with the entity cache, or FALSE if no $ids
  394. // were passed. The $ids array is reduced as items are loaded from cache,
  395. // and we need to know if it's empty for this reason to avoid querying the
  396. // database when all requested entities are loaded from cache.
  397. $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
  398. // Try to load entities from the static cache.
  399. if ($this->cache && !$revision_id) {
  400. $entities = $this->cacheGet($ids, $conditions);
  401. // If any entities were loaded, remove them from the ids still to load.
  402. if ($passed_ids) {
  403. $ids = array_keys(array_diff_key($passed_ids, $entities));
  404. }
  405. }
  406. // Support the entitycache module if activated.
  407. if (!empty($this->entityInfo['entity cache']) && !$revision_id && $ids && !$conditions) {
  408. $cached_entities = EntityCacheControllerHelper::entityCacheGet($this, $ids, $conditions);
  409. // If any entities were loaded, remove them from the ids still to load.
  410. $ids = array_diff($ids, array_keys($cached_entities));
  411. $entities += $cached_entities;
  412. // Add loaded entities to the static cache if we are not loading a
  413. // revision.
  414. if ($this->cache && !empty($cached_entities) && !$revision_id) {
  415. $this->cacheSet($cached_entities);
  416. }
  417. }
  418. // Load any remaining entities from the database. This is the case if $ids
  419. // is set to FALSE (so we load all entities), if there are any ids left to
  420. // load or if loading a revision.
  421. if (!($this->cacheComplete && $ids === FALSE && !$conditions) && ($ids === FALSE || $ids || $revision_id)) {
  422. $queried_entities = array();
  423. foreach ($this->query($ids, $conditions, $revision_id) as $record) {
  424. // Skip entities already retrieved from cache.
  425. if (isset($entities[$record->{$this->idKey}])) {
  426. continue;
  427. }
  428. // For DB-based entities take care of serialized columns.
  429. if (!empty($this->entityInfo['base table'])) {
  430. $schema = drupal_get_schema($this->entityInfo['base table']);
  431. foreach ($schema['fields'] as $field => $info) {
  432. if (!empty($info['serialize']) && isset($record->$field)) {
  433. $record->$field = unserialize($record->$field);
  434. // Support automatic merging of 'data' fields into the entity.
  435. if (!empty($info['merge']) && is_array($record->$field)) {
  436. foreach ($record->$field as $key => $value) {
  437. $record->$key = $value;
  438. }
  439. unset($record->$field);
  440. }
  441. }
  442. }
  443. }
  444. $queried_entities[$record->{$this->idKey}] = $record;
  445. }
  446. }
  447. // Pass all entities loaded from the database through $this->attachLoad(),
  448. // which attaches fields (if supported by the entity type) and calls the
  449. // entity type specific load callback, for example hook_node_load().
  450. if (!empty($queried_entities)) {
  451. $this->attachLoad($queried_entities, $revision_id, $field_ids);
  452. $entities += $queried_entities;
  453. }
  454. // Entity cache module support: Add entities to the entity cache if we are
  455. // not loading a revision.
  456. if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
  457. EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
  458. }
  459. if ($this->cache) {
  460. // Add entities to the cache if we are not loading a revision.
  461. if (!empty($queried_entities) && !$revision_id) {
  462. $this->cacheSet($queried_entities);
  463. // Remember if we have cached all entities now.
  464. if (!$conditions && $ids === FALSE) {
  465. $this->cacheComplete = TRUE;
  466. }
  467. }
  468. }
  469. // Ensure that the returned array is ordered the same as the original
  470. // $ids array if this was passed in and remove any invalid ids.
  471. if ($passed_ids && $passed_ids = array_intersect_key($passed_ids, $entities)) {
  472. foreach ($passed_ids as $id => $value) {
  473. $passed_ids[$id] = $entities[$id];
  474. }
  475. $entities = $passed_ids;
  476. }
  477. return $entities;
  478. }
  479. /**
  480. * Override the attachLoad function.
  481. *
  482. * A TripalEntity may have a large number of fields attached which may
  483. * slow down the loading of pages and web services. Therefore, we only
  484. * want to attach fields that are needed.
  485. *
  486. * @param $queried_entities
  487. * The list of queried
  488. * @param $revision_id
  489. * ID of the revision that was loaded, or FALSE if the most current
  490. * revision was loaded.
  491. * @param $field_ids
  492. */
  493. protected function attachLoad(&$queried_entities, $revision_id = FALSE,
  494. $field_ids = array()) {
  495. // Attach fields.
  496. if ($this->entityInfo['fieldable']) {
  497. if ($revision_id) {
  498. $function = 'field_attach_load_revision';
  499. }
  500. else {
  501. $function = 'field_attach_load';
  502. }
  503. foreach ($queried_entities as $id => $entity) {
  504. $info = entity_get_info($entity->type);
  505. $field_cache = array_key_exists('field cache', $info) ? $info['field cache'] : FALSE;
  506. $bundle_name = $entity->bundle;
  507. // Iterate through the field instances and find those that are set to
  508. // 'auto_attach' and which are attached to this bundle. Add all
  509. // fields that don't need auto attach to the field_ids array.
  510. $instances = field_info_instances('TripalEntity', $bundle_name);
  511. foreach ($instances as $instance) {
  512. $field_name = $instance['field_name'];
  513. $field = field_info_field($field_name);
  514. $field_id = $field['id'];
  515. // Add this field to the entity with default value.
  516. if (!isset($queried_entities[$id]->$field_name)) {
  517. $queried_entities[$id]->$field_name = array();
  518. }
  519. // Options used for the field_attach_load function.
  520. $options = array();
  521. $options['field_id'] = $field['id'];
  522. // The cache ID for the entity. We must manually set the cache
  523. // because the field_attach_load won't do it for us.
  524. $cfid = "field:TripalEntity:$id:$field_name";
  525. // Check if the field is cached. if so, then don't reload.
  526. if ($field_cache) {
  527. $cache_data = cache_get($cfid, 'cache_field');
  528. if (!empty($cache_data)) {
  529. $queried_entities[$id]->$field_name = $cache_data->data;
  530. $queried_entities[$id]->{$field_name}['#processed'] = TRUE;
  531. continue;
  532. }
  533. }
  534. // If a list of field_ids is provided then we specifically want
  535. // to only load the fields specified.
  536. if (count($field_ids) > 0) {
  537. if (in_array($field_id, $field_ids)) {
  538. $function($this->entityType, array($entity->id => $queried_entities[$id]),
  539. FIELD_LOAD_CURRENT, $options);
  540. // Cache the field.
  541. if ($field_cache) {
  542. cache_set($cfid, $entity->$field_name, 'cache_field');
  543. }
  544. $queried_entities[$id]->{$field_name}['#processed'] = TRUE;
  545. }
  546. }
  547. // If we don't have a list of fields then load them all, but only
  548. // if the instance is a TripalField and it is set to not auto
  549. // attach then we will ignore it. It can only be set by providing
  550. // the id in the $field_id array handled previously.
  551. else {
  552. // We only load via AJAX if empty fields are not hidden.
  553. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  554. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  555. if (array_key_exists('settings', $instance) and
  556. array_key_exists('auto_attach', $instance['settings']) and
  557. $instance['settings']['auto_attach'] == FALSE and
  558. $hide_variable == 'show') {
  559. // Add an empty value. This will allow the tripal_entity_view()
  560. // hook to add the necessary prefixes to the field for ajax
  561. // loading.
  562. $queried_entities[$id]->$field_name['und'][0]['value'] = '';
  563. $queried_entities[$id]->{$field_name}['#processed'] = FALSE;
  564. }
  565. else {
  566. $function($this->entityType, array($entity->id => $queried_entities[$id]),
  567. FIELD_LOAD_CURRENT, $options);
  568. // Cache the field.
  569. if ($field_cache) {
  570. cache_set($cfid, $entity->$field_name, 'cache_field');
  571. }
  572. $queried_entities[$id]->{$field_name}['#processed'] = TRUE;
  573. }
  574. }
  575. }
  576. }
  577. }
  578. // Call hook_entity_load().
  579. foreach (module_implements('entity_load') as $module) {
  580. $function = $module . '_entity_load';
  581. $function($queried_entities, $this->entityType);
  582. }
  583. // Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
  584. // always the queried entities, followed by additional arguments set in
  585. // $this->hookLoadArguments.
  586. $args = array_merge(array($queried_entities), $this->hookLoadArguments);
  587. foreach (module_implements($this->entityInfo['load hook']) as $module) {
  588. call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
  589. }
  590. }
  591. }