entity_test.module 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * @file
  4. * Test moduel for the entity API.
  5. */
  6. /**
  7. * Implements hook_entity_info().
  8. */
  9. function entity_test_entity_info() {
  10. $return = array(
  11. 'entity_test' => array(
  12. 'label' => t('Test Entity'),
  13. 'plural label' => t('Test Entities'),
  14. 'description' => t('An entity type used by the entity API tests.'),
  15. 'entity class' => 'EntityClass',
  16. 'controller class' => 'EntityAPIController',
  17. 'base table' => 'entity_test',
  18. 'fieldable' => TRUE,
  19. 'entity keys' => array(
  20. 'id' => 'pid',
  21. 'bundle' => 'name',
  22. ),
  23. // Make use the class' label() and uri() implementation by default.
  24. 'label callback' => 'entity_class_label',
  25. 'uri callback' => 'entity_class_uri',
  26. 'bundles' => array(),
  27. 'bundle keys' => array(
  28. 'bundle' => 'name',
  29. ),
  30. 'module' => 'entity_test',
  31. ),
  32. 'entity_test_type' => array(
  33. 'label' => t('Test entity type'),
  34. 'entity class' => 'Entity',
  35. 'controller class' => 'EntityAPIControllerExportable',
  36. 'base table' => 'entity_test_type',
  37. 'fieldable' => FALSE,
  38. 'bundle of' => 'entity_test',
  39. 'exportable' => TRUE,
  40. 'entity keys' => array(
  41. 'id' => 'id',
  42. 'name' => 'name',
  43. ),
  44. 'module' => 'entity_test',
  45. ),
  46. 'entity_test2' => array(
  47. 'label' => t('Test Entity (revision support)'),
  48. 'entity class' => 'EntityClassRevision',
  49. 'controller class' => 'EntityAPIController',
  50. 'base table' => 'entity_test2',
  51. 'revision table' => 'entity_test2_revision',
  52. 'fieldable' => TRUE,
  53. 'entity keys' => array(
  54. 'id' => 'pid',
  55. 'revision' => 'revision_id',
  56. ),
  57. // Make use of the class label() and uri() implementation by default.
  58. 'label callback' => 'entity_class_label',
  59. 'uri callback' => 'entity_class_uri',
  60. 'bundles' => array(),
  61. 'bundle keys' => array(
  62. 'bundle' => 'name',
  63. ),
  64. ),
  65. );
  66. // Add bundle info but bypass entity_load() as we cannot use it here.
  67. $types = db_select('entity_test_type', 'et')
  68. ->fields('et')
  69. ->execute()
  70. ->fetchAllAssoc('name');
  71. foreach ($types as $name => $type) {
  72. $return['entity_test']['bundles'][$name] = array(
  73. 'label' => $type->label,
  74. );
  75. }
  76. // Support entity cache module.
  77. if (module_exists('entitycache')) {
  78. $return['entity_test']['field cache'] = FALSE;
  79. $return['entity_test']['entity cache'] = TRUE;
  80. }
  81. return $return;
  82. }
  83. /**
  84. * Gets an array of all test entity types, keyed by the name.
  85. *
  86. * @param $name
  87. * If set, the type with the given name is returned.
  88. */
  89. function entity_test_get_types($name = NULL) {
  90. $types = entity_load_multiple_by_name('entity_test_type', isset($name) ? array($name) : FALSE);
  91. return isset($name) ? reset($types) : $types;
  92. }
  93. /**
  94. * Load multiple test entities based on certain conditions.
  95. *
  96. * @param $pids
  97. * An array of entity IDs.
  98. * @param $conditions
  99. * An array of conditions to match against the {entity} table.
  100. * @param $reset
  101. * A boolean indicating that the internal cache should be reset.
  102. * @return
  103. * An array of test entity objects, indexed by pid.
  104. */
  105. function entity_test_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
  106. return entity_load('entity_test', $pids, $conditions, $reset);
  107. }
  108. /**
  109. * Delete multiple test entities.
  110. *
  111. * @param $pids
  112. * An array of test entity IDs.
  113. */
  114. function entity_test_delete_multiple(array $pids) {
  115. entity_get_controller('entity_test')->delete($pids);
  116. }
  117. /**
  118. * Main class for test entities.
  119. */
  120. class EntityClass extends Entity {
  121. public function __construct(array $values = array(), $entityType = NULL) {
  122. parent::__construct($values, 'entity_test');
  123. }
  124. /**
  125. * Override buildContent() to add the username to the output.
  126. */
  127. public function buildContent($view_mode = 'full', $langcode = NULL) {
  128. $content['user'] = array(
  129. '#markup' => "User: ". format_username(user_load($this->uid)),
  130. );
  131. return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode, $content);
  132. }
  133. /**
  134. * Specifies the default label, which is picked up by label() by default.
  135. */
  136. protected function defaultLabel() {
  137. $type = entity_test_get_types($this->name);
  138. return $type->label;
  139. }
  140. /**
  141. * Specifies the default uri, which is picked up by uri() by default.
  142. */
  143. protected function defaultURI() {
  144. return array('path' => 'custom/' . $this->identifier());
  145. }
  146. }
  147. /**
  148. * Main class for test entities (with revision support).
  149. */
  150. class EntityClassRevision extends EntityClass {
  151. public function __construct(array $values = array(), $entityType = NULL) {
  152. Entity::__construct($values, 'entity_test2');
  153. }
  154. }
  155. /**
  156. *
  157. *
  158. * Some hook implementations used by the tests.
  159. *
  160. *
  161. */
  162. /**
  163. * Implements hook_entity_insert().
  164. */
  165. function entity_test_entity_insert($entity, $entity_type) {
  166. if ($entity_type == 'entity_test_type') {
  167. $_SESSION['entity_hook_test']['entity_insert'][] = entity_id($entity_type, $entity);
  168. }
  169. }
  170. /**
  171. * Implements hook_entity_update().
  172. */
  173. function entity_test_entity_update($entity, $entity_type) {
  174. $_SESSION['entity_hook_test']['entity_update'][] = entity_id($entity_type, $entity);
  175. }
  176. /**
  177. * Implements hook_entity_delete().
  178. */
  179. function entity_test_entity_delete($entity, $entity_type) {
  180. if ($entity_type == 'entity_test_type') {
  181. $_SESSION['entity_hook_test']['entity_delete'][] = entity_id($entity_type, $entity);
  182. }
  183. }
  184. /**
  185. * Implements hook_entity_test_type_insert().
  186. */
  187. function entity_test_entity_test_type_insert($entity) {
  188. $_SESSION['entity_hook_test']['entity_test_type_insert'][] = $entity->identifier();
  189. }
  190. /**
  191. * Implements hook_entity_test_type_update().
  192. */
  193. function entity_test_entity_test_type_update($entity) {
  194. $_SESSION['entity_hook_test']['entity_test_type_update'][] = $entity->identifier();
  195. // Determine changes on update.
  196. if (!empty($entity->original) && $entity->original->label == 'test_changes') {
  197. if ($entity->original->label != $entity->label) {
  198. $entity->label .= '_update';
  199. }
  200. }
  201. }
  202. /**
  203. * Implements hook_entity_test_type_delete().
  204. */
  205. function entity_test_entity_test_type_delete($entity) {
  206. $_SESSION['entity_hook_test']['entity_test_type_delete'][] = $entity->identifier();
  207. }
  208. /**
  209. * Implements hook_entity_test_type_presave().
  210. */
  211. function entity_test_entity_test_type_presave($entity) {
  212. // Determine changes.
  213. if (!empty($entity->original) && $entity->original->label == 'test_changes') {
  214. if ($entity->original->label != $entity->label) {
  215. $entity->label .= '_presave';
  216. }
  217. }
  218. }
  219. /**
  220. * Implements hook_entity_property_info_alter() for testing an property of type
  221. * 'entity'.
  222. */
  223. function entity_test_entity_property_info_alter(&$info) {
  224. $info['node']['properties']['reference'] = array(
  225. 'label' => t('Test reference'),
  226. 'description' => t('A generic entity reference.'),
  227. 'getter callback' => 'entity_test_entity_getter',
  228. 'setter callback' => 'entity_test_entity_setter',
  229. 'type' => 'entity',
  230. );
  231. }
  232. /**
  233. * Getter callback for the 'reference' property.
  234. */
  235. function entity_test_entity_getter($node) {
  236. if (empty($node->entity)) {
  237. $node->entity = array('type' => 'user', 'id' => $node->uid);
  238. }
  239. // We have to return the entity wrapped.
  240. // Special handling for anonymous user.
  241. if ($node->entity['type'] === 'user' && empty($node->entity['id'])) {
  242. return entity_metadata_wrapper('user', drupal_anonymous_user());
  243. }
  244. else {
  245. return entity_metadata_wrapper($node->entity['type'], $node->entity['id']);
  246. }
  247. }
  248. /**
  249. * Setter callback for the 'reference' property.
  250. */
  251. function entity_test_entity_setter($node, $property_name, $wrapper) {
  252. // The entity has to be passed wrapped.
  253. $node->entity = array('type' => $wrapper->type(), 'id' => $wrapper->getIdentifier());
  254. }