entity_test.module

Test module for the entity API providing several entity types for testing.

File

drupal/core/modules/system/tests/modules/entity_test/entity_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Test module for the entity API providing several entity types for testing.
  5. */
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\entity\Plugin\Core\Entity\EntityFormDisplay;
  8. /**
  9. * Filter that limits test entity list to revisable ones.
  10. */
  11. const ENTITY_TEST_TYPES_REVISABLE = 1;
  12. /**
  13. * Filter that limits test entity list to multilingual ones.
  14. */
  15. const ENTITY_TEST_TYPES_MULTILINGUAL = 2;
  16. /**
  17. * Returns a list of test entity types.
  18. *
  19. * The returned entity types are one for each available entity storage type:
  20. * - The plain entity_test type supports neither revisions nor multilingual
  21. * properties.
  22. * - The entity_test_mul type supports multilingual properties.
  23. * - The entity_test_rev type supports revisions.
  24. * - The entity_test_mulrev type supports both revisions and multilingual
  25. * properties.
  26. *
  27. * @param int $filter
  28. * Either ENTITY_TEST_TYPES_REVISABLE to only return revisable entity types or
  29. * ENTITY_TEST_TYPES_MULTILINGUAL to only return multilingual ones. Defaults
  30. * to NULL, which returns all.
  31. *
  32. * @return array
  33. * List with entity_types.
  34. */
  35. function entity_test_entity_types($filter = NULL) {
  36. $types = array();
  37. if ($filter == NULL) {
  38. $types[] = 'entity_test';
  39. }
  40. if ($filter != ENTITY_TEST_TYPES_REVISABLE) {
  41. $types[] = 'entity_test_mul';
  42. }
  43. if ($filter != ENTITY_TEST_TYPES_MULTILINGUAL) {
  44. $types[] = 'entity_test_rev';
  45. }
  46. $types[] = 'entity_test_mulrev';
  47. return drupal_map_assoc($types);
  48. }
  49. /**
  50. * Implements hook_entity_info_alter().
  51. */
  52. function entity_test_entity_info_alter(&$info) {
  53. // Optionally specify a translation handler for testing translations.
  54. if (Drupal::state()->get('entity_test.translation')) {
  55. foreach(entity_test_entity_types() as $entity_type) {
  56. $info[$entity_type]['translation'][$entity_type] = TRUE;
  57. }
  58. }
  59. }
  60. /**
  61. * Creates a new bundle for entity_test entities.
  62. *
  63. * @param string $bundle
  64. * The machine-readable name of the bundle.
  65. * @param string $text
  66. * (optional) The human-readable name of the bundle. If none is provided, the
  67. * machine name will be used.
  68. * @param string $entity_type
  69. * (optional) The entity type for which the bundle is created. Defaults to
  70. * 'entity_test'.
  71. */
  72. function entity_test_create_bundle($bundle, $text = NULL, $entity_type = 'entity_test') {
  73. $bundles = Drupal::state()->get($entity_type . '.bundles') ?: array('entity_test' => array('label' => 'Entity Test Bundle'));
  74. $bundles += array($bundle => array('label' => $text ? $text : $bundle));
  75. Drupal::state()->set($entity_type . '.bundles', $bundles);
  76. entity_invoke_bundle_hook('create', $entity_type, $bundle);
  77. }
  78. /**
  79. * Renames a bundle for entity_test entities.
  80. *
  81. * @param string $bundle_old
  82. * The machine-readable name of the bundle to rename.
  83. * @param string $bundle_new
  84. * The new machine-readable name of the bundle
  85. * @param string $entity_type
  86. * (optional) The entity type for which the bundle is renamed. Defaults to
  87. * 'entity_test'.
  88. */
  89. function entity_test_rename_bundle($bundle_old, $bundle_new, $entity_type = 'entity_test') {
  90. $bundles = Drupal::state()->get($entity_type . '.bundles') ?: array('entity_test' => array('label' => 'Entity Test Bundle'));
  91. $bundles[$bundle_new] = $bundles[$bundle_old];
  92. unset($bundles[$bundle_old]);
  93. Drupal::state()->set($entity_type . '.bundles', $bundles);
  94. entity_invoke_bundle_hook('rename', $entity_type, $bundle_old, $bundle_new);
  95. }
  96. /**
  97. * Deletes a bundle for entity_test entities.
  98. *
  99. * @param string $bundle
  100. * The machine-readable name of the bundle to delete.
  101. * @param string $entity_type
  102. * (optional) The entity type for which the bundle is deleted. Defaults to
  103. * 'entity_test'.
  104. */
  105. function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') {
  106. $bundles = Drupal::state()->get($entity_type . '.bundles') ?: array('entity_test' => array('label' => 'Entity Test Bundle'));
  107. unset($bundles[$bundle]);
  108. Drupal::state()->set($entity_type . '.bundles', $bundles);
  109. entity_invoke_bundle_hook('delete', $entity_type, $bundle);
  110. }
  111. /**
  112. * Implements hook_entity_bundle_info_alter().
  113. */
  114. function entity_test_entity_bundle_info_alter(&$bundles) {
  115. $entity_info = entity_get_info();
  116. foreach ($bundles as $entity_type => $info) {
  117. if ($entity_info[$entity_type]['module'] == 'entity_test') {
  118. $bundles[$entity_type] = Drupal::state()->get($entity_type . '.bundles') ?: array($entity_type => array('label' => 'Entity Test Bundle'));
  119. }
  120. }
  121. }
  122. /**
  123. * Implements hook_entity_view_mode_info_alter().
  124. */
  125. function entity_test_entity_view_mode_info_alter(&$view_modes) {
  126. $entity_info = entity_get_info();
  127. foreach ($entity_info as $entity_type => $info) {
  128. if ($entity_info[$entity_type]['module'] == 'entity_test') {
  129. $view_modes[$entity_type] = array(
  130. 'full' => array(
  131. 'label' => t('Full object'),
  132. 'status' => TRUE,
  133. ),
  134. 'teaser' => array(
  135. 'label' => t('Teaser'),
  136. 'status' => TRUE,
  137. ),
  138. );
  139. }
  140. }
  141. }
  142. /**
  143. * Implements hook_field_extra_fields().
  144. */
  145. function entity_test_field_extra_fields() {
  146. $extra['entity_test']['entity_test'] = array(
  147. 'display' => array(
  148. // Note: those extra fields do not currently display anything, they are
  149. // just used in \Drupal\entity\Tests\EntityDisplayTest to test the
  150. // behavior of entity display objects,
  151. 'display_extra_field' => array(
  152. 'label' => t('Display extra field'),
  153. 'description' => t('An extra field on the display side.'),
  154. 'weight' => 5,
  155. 'visible' => TRUE,
  156. ),
  157. 'display_extra_field_hidden' => array(
  158. 'label' => t('Display extra field (hidden)'),
  159. 'description' => t('An extra field on the display side, hidden by default.'),
  160. 'visible' => FALSE,
  161. ),
  162. )
  163. );
  164. return $extra;
  165. }
  166. /**
  167. * Implements hook_permission().
  168. */
  169. function entity_test_permission() {
  170. $permissions = array(
  171. 'administer entity_test content' => array(
  172. 'title' => t('Administer entity_test content'),
  173. 'description' => t('Manage entity_test content'),
  174. ),
  175. 'view test entity' => array(
  176. 'title' => t('View test entities'),
  177. ),
  178. 'view test entity translations' => array(
  179. 'title' => t('View translations of test entities'),
  180. ),
  181. );
  182. return $permissions;
  183. }
  184. /**
  185. * Implements hook_menu().
  186. */
  187. function entity_test_menu() {
  188. $items = array();
  189. foreach(entity_test_entity_types() as $entity_type) {
  190. $items[$entity_type . '/add'] = array(
  191. 'title' => 'Add an @type',
  192. 'title arguments' => array('@type' => $entity_type),
  193. 'page callback' => 'entity_test_add',
  194. 'page arguments' => array($entity_type),
  195. 'access arguments' => array('administer entity_test content'),
  196. 'type' => MENU_NORMAL_ITEM,
  197. );
  198. $items[$entity_type . '/manage/%' . $entity_type] = array(
  199. 'title' => 'Edit @type',
  200. 'title arguments' => array('@type' => $entity_type),
  201. 'page callback' => 'entity_test_edit',
  202. 'page arguments' => array(2),
  203. 'access arguments' => array('administer entity_test content'),
  204. 'type' => MENU_NORMAL_ITEM,
  205. );
  206. $items[$entity_type . '/manage/%' . $entity_type . '/edit'] = array(
  207. 'title' => 'Edit',
  208. 'type' => MENU_DEFAULT_LOCAL_TASK,
  209. );
  210. }
  211. return $items;
  212. }
  213. /**
  214. * Implements hook_form_BASE_FORM_ID_alter().
  215. */
  216. function entity_test_form_node_form_alter(&$form, &$form_state, $form_id) {
  217. $langcode = $form_state['controller']->getFormLangcode($form_state);
  218. Drupal::state()->set('entity_test.form_langcode', $langcode);
  219. }
  220. /**
  221. * Menu callback: displays the 'Add new entity_test' form.
  222. *
  223. * @param string $entity_type
  224. * Name of the entity type for which a create form should be displayed.
  225. *
  226. * @return array
  227. * The processed form for a new entity_test.
  228. *
  229. * @see entity_test_menu()
  230. */
  231. function entity_test_add($entity_type) {
  232. drupal_set_title(t('Create an @type', array('@type' => $entity_type)));
  233. $entity = entity_create($entity_type, array());
  234. return entity_get_form($entity);
  235. }
  236. /**
  237. * Menu callback: displays the 'Edit existing entity_test' form.
  238. *
  239. * @param \Drupal\Core\Entity\EntityInterface $entity
  240. * The entity to be edited.
  241. *
  242. * @return array
  243. * The processed form for the edited entity.
  244. *
  245. * @see entity_test_menu()
  246. */
  247. function entity_test_edit(EntityInterface $entity) {
  248. drupal_set_title($entity->label(), PASS_THROUGH);
  249. return entity_get_form($entity);
  250. }
  251. /**
  252. * Loads a test entity.
  253. *
  254. * @param int $id
  255. * A test entity ID.
  256. * @param bool $reset
  257. * A boolean indicating that the internal cache should be reset.
  258. *
  259. * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTest
  260. * The loaded entity object, or FALSE if the entity cannot be loaded.
  261. */
  262. function entity_test_load($id, $reset = FALSE) {
  263. return entity_load('entity_test', $id, $reset);
  264. }
  265. /**
  266. * Loads a test entity.
  267. *
  268. * @param int $id
  269. * A test entity ID.
  270. * @param bool $reset
  271. * A boolean indicating that the internal cache should be reset.
  272. *
  273. * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestRev
  274. * The loaded entity object, or FALSE if the entity cannot be loaded.
  275. */
  276. function entity_test_rev_load($id, $reset = FALSE) {
  277. return entity_load('entity_test_rev', $id, $reset);
  278. }
  279. /**
  280. * Loads a test entity.
  281. *
  282. * @param int $id
  283. * A test entity ID.
  284. * @param bool $reset
  285. * A boolean indicating that the internal cache should be reset.
  286. *
  287. * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestMul
  288. * The loaded entity object, or FALSE if the entity cannot be loaded.
  289. */
  290. function entity_test_mul_load($id, $reset = FALSE) {
  291. return entity_load('entity_test_mul', $id, $reset);
  292. }
  293. /**
  294. * Loads a test entity.
  295. *
  296. * @param int $id
  297. * A test entity ID.
  298. * @param bool $reset
  299. * A boolean indicating that the internal cache should be reset.
  300. *
  301. * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestMulRev
  302. * The loaded entity object, or FALSE if the entity cannot be loaded.
  303. */
  304. function entity_test_mulrev_load($id, $reset = FALSE) {
  305. return entity_load('entity_test_mulrev', $id, $reset);
  306. }
  307. /**
  308. * Implements hook_ENTITY_TYPE_insert().
  309. */
  310. function entity_test_entity_test_insert($entity) {
  311. if ($entity->name->value == 'fail_insert') {
  312. throw new Exception("Test exception rollback.");
  313. }
  314. }
  315. /**
  316. * Entity label callback.
  317. *
  318. * @param $entity_type
  319. * The entity type.
  320. * @param $entity
  321. * The entity object.
  322. * @param $langcocde
  323. * (optional) The langcode.
  324. *
  325. * @return
  326. * The label of the entity prefixed with "label callback".
  327. */
  328. function entity_test_label_callback($entity_type, $entity, $langcode = NULL) {
  329. return 'label callback ' . $entity->name->value;
  330. }
  331. /**
  332. * Implements hook_entity_field_access().
  333. *
  334. * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
  335. */
  336. function entity_test_entity_field_access($operation, $field, $account) {
  337. if ($field->getName() == 'field_test_text') {
  338. if ($field->value == 'no access value') {
  339. return FALSE;
  340. }
  341. elseif ($operation == 'delete' && $field->value == 'no delete access value') {
  342. return FALSE;
  343. }
  344. }
  345. }
  346. /**
  347. * Implements hook_entity_field_access_alter().
  348. *
  349. * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
  350. */
  351. function entity_test_entity_field_access_alter(array &$grants, array $context) {
  352. $field = $context['field'];
  353. if ($field->getName() == 'field_test_text' && $field->value == 'access alter value') {
  354. $grants[':default'] = FALSE;
  355. }
  356. }
  357. /**
  358. * Implements hook_entity_form_display_alter().
  359. */
  360. function entity_test_entity_form_display_alter(EntityFormDisplay $form_display, $context) {
  361. // Make the field_test_text field 42 characters for entity_test_mul.
  362. if ($context['entity_type'] == 'entity_test') {
  363. if ($component_options = $form_display->getComponent('field_test_text')) {
  364. $component_options['settings']['size'] = 42;
  365. $form_display->setComponent('field_test_text', $component_options);
  366. }
  367. }
  368. }
  369. /**
  370. * Implements hook_entity_presave()
  371. */
  372. function entity_test_entity_presave(EntityInterface $entity) {
  373. if (isset($GLOBALS['entity_test_throw_exception'])) {
  374. throw new Exception('Entity presave exception', 1);
  375. }
  376. }
  377. /**
  378. * Implements hook_entity_predelete()
  379. */
  380. function entity_test_entity_predelete(EntityInterface $entity) {
  381. if (isset($GLOBALS['entity_test_throw_exception'])) {
  382. throw new Exception('Entity predelete exception', 2);
  383. }
  384. }
  385. /**
  386. * Implements hook_entity_operation_alter().
  387. */
  388. function entity_test_entity_operation_alter(array &$operations, EntityInterface $entity) {
  389. $uri = $entity->uri();
  390. $operations['test_operation'] = array(
  391. 'title' => format_string('Test Operation: @label', array('@label' => $entity->label())),
  392. 'href' => $uri['path'] . '/test_operation',
  393. 'weight' => 50,
  394. );
  395. }

Functions

Namesort descending Description
entity_test_add Menu callback: displays the 'Add new entity_test' form.
entity_test_create_bundle Creates a new bundle for entity_test entities.
entity_test_delete_bundle Deletes a bundle for entity_test entities.
entity_test_edit Menu callback: displays the 'Edit existing entity_test' form.
entity_test_entity_bundle_info_alter Implements hook_entity_bundle_info_alter().
entity_test_entity_field_access Implements hook_entity_field_access().
entity_test_entity_field_access_alter Implements hook_entity_field_access_alter().
entity_test_entity_form_display_alter Implements hook_entity_form_display_alter().
entity_test_entity_info_alter Implements hook_entity_info_alter().
entity_test_entity_operation_alter Implements hook_entity_operation_alter().
entity_test_entity_predelete Implements hook_entity_predelete()
entity_test_entity_presave Implements hook_entity_presave()
entity_test_entity_test_insert Implements hook_ENTITY_TYPE_insert().
entity_test_entity_types Returns a list of test entity types.
entity_test_entity_view_mode_info_alter Implements hook_entity_view_mode_info_alter().
entity_test_field_extra_fields Implements hook_field_extra_fields().
entity_test_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter().
entity_test_label_callback Entity label callback.
entity_test_load Loads a test entity.
entity_test_menu Implements hook_menu().
entity_test_mulrev_load Loads a test entity.
entity_test_mul_load Loads a test entity.
entity_test_permission Implements hook_permission().
entity_test_rename_bundle Renames a bundle for entity_test entities.
entity_test_rev_load Loads a test entity.

Constants

Namesort descending Description
ENTITY_TEST_TYPES_MULTILINGUAL Filter that limits test entity list to multilingual ones.
ENTITY_TEST_TYPES_REVISABLE Filter that limits test entity list to revisable ones.