field_test.entity.inc

  1. cis7 modules/field/tests/field_test.entity.inc
  2. cle7 modules/field/tests/field_test.entity.inc
  3. ecd7 modules/field/tests/field_test.entity.inc
  4. elmsmedia7 modules/field/tests/field_test.entity.inc
  5. harmony7 modules/field/tests/field_test.entity.inc
  6. icor7 modules/field/tests/field_test.entity.inc
  7. meedjum_blog7 modules/field/tests/field_test.entity.inc
  8. mooc7 modules/field/tests/field_test.entity.inc

Defines an entity type.

Functions

Namesort descending Description
field_test_create_bundle Creates a new bundle for test_entity entities.
field_test_create_stub_entity Creates a basic test_entity entity.
field_test_delete_bundle Deletes a bundle for test_entity objects.
field_test_entity_add Menu callback: displays the 'Add new test_entity' form.
field_test_entity_edit Menu callback: displays the 'Edit exiisting test_entity' form.
field_test_entity_form Test_entity form.
field_test_entity_form_submit Submit handler for field_test_entity_form().
field_test_entity_form_submit_build_test_entity Updates the form state's entity by processing this submission's values.
field_test_entity_form_validate Validate handler for field_test_entity_form().
field_test_entity_info Implements hook_entity_info().
field_test_entity_info_alter Implements hook_entity_info_alter().
field_test_entity_info_translatable Helper function to enable entity translations.
field_test_entity_nested_form Form combining two separate entities.
field_test_entity_nested_form_submit Submit handler for field_test_entity_nested_form().
field_test_entity_nested_form_validate Validate handler for field_test_entity_nested_form().
field_test_entity_save Saves a test_entity.
field_test_entity_test_load Loads a test_entity.
field_test_rename_bundle Renames a bundle for test_entity entities.

Classes

Namesort descending Description
TestEntityBundleController Controller class for the test_entity_bundle entity type.

File

modules/field/tests/field_test.entity.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Defines an entity type.
  5. */
  6. /**
  7. * Implements hook_entity_info().
  8. */
  9. function field_test_entity_info() {
  10. // If requested, clear the field cache while this hook is being called. See
  11. // testFieldInfoCache().
  12. if (variable_get('field_test_clear_info_cache_in_hook_entity_info', FALSE)) {
  13. field_info_cache_clear();
  14. }
  15. $bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
  16. $test_entity_modes = array(
  17. 'full' => array(
  18. 'label' => t('Full object'),
  19. 'custom settings' => TRUE,
  20. ),
  21. 'teaser' => array(
  22. 'label' => t('Teaser'),
  23. 'custom settings' => TRUE,
  24. ),
  25. );
  26. return array(
  27. 'test_entity' => array(
  28. 'label' => t('Test Entity'),
  29. 'fieldable' => TRUE,
  30. 'field cache' => FALSE,
  31. 'base table' => 'test_entity',
  32. 'revision table' => 'test_entity_revision',
  33. 'entity keys' => array(
  34. 'id' => 'ftid',
  35. 'revision' => 'ftvid',
  36. 'bundle' => 'fttype',
  37. ),
  38. 'bundles' => $bundles,
  39. 'view modes' => $test_entity_modes,
  40. ),
  41. // This entity type doesn't get form handling for now...
  42. 'test_cacheable_entity' => array(
  43. 'label' => t('Test Entity, cacheable'),
  44. 'fieldable' => TRUE,
  45. 'field cache' => TRUE,
  46. 'entity keys' => array(
  47. 'id' => 'ftid',
  48. 'revision' => 'ftvid',
  49. 'bundle' => 'fttype',
  50. ),
  51. 'bundles' => $bundles,
  52. 'view modes' => $test_entity_modes,
  53. ),
  54. 'test_entity_bundle_key' => array(
  55. 'label' => t('Test Entity with a bundle key.'),
  56. 'base table' => 'test_entity_bundle_key',
  57. 'fieldable' => TRUE,
  58. 'field cache' => FALSE,
  59. 'entity keys' => array(
  60. 'id' => 'ftid',
  61. 'bundle' => 'fttype',
  62. ),
  63. 'bundles' => array('bundle1' => array('label' => 'Bundle1'), 'bundle2' => array('label' => 'Bundle2')) + $bundles,
  64. 'view modes' => $test_entity_modes,
  65. ),
  66. // In this case, the bundle key is not stored in the database.
  67. 'test_entity_bundle' => array(
  68. 'label' => t('Test Entity with a specified bundle.'),
  69. 'base table' => 'test_entity_bundle',
  70. 'fieldable' => TRUE,
  71. 'controller class' => 'TestEntityBundleController',
  72. 'field cache' => FALSE,
  73. 'entity keys' => array(
  74. 'id' => 'ftid',
  75. 'bundle' => 'fttype',
  76. ),
  77. 'bundles' => array('test_entity_2' => array('label' => 'Test entity 2')) + $bundles,
  78. 'view modes' => $test_entity_modes,
  79. ),
  80. // @see EntityPropertiesTestCase::testEntityLabel()
  81. 'test_entity_no_label' => array(
  82. 'label' => t('Test entity without label'),
  83. 'fieldable' => TRUE,
  84. 'field cache' => FALSE,
  85. 'base table' => 'test_entity',
  86. 'entity keys' => array(
  87. 'id' => 'ftid',
  88. 'revision' => 'ftvid',
  89. 'bundle' => 'fttype',
  90. ),
  91. 'bundles' => $bundles,
  92. 'view modes' => $test_entity_modes,
  93. ),
  94. 'test_entity_label' => array(
  95. 'label' => t('Test entity label'),
  96. 'fieldable' => TRUE,
  97. 'field cache' => FALSE,
  98. 'base table' => 'test_entity',
  99. 'entity keys' => array(
  100. 'id' => 'ftid',
  101. 'revision' => 'ftvid',
  102. 'bundle' => 'fttype',
  103. 'label' => 'ftlabel',
  104. ),
  105. 'bundles' => $bundles,
  106. 'view modes' => $test_entity_modes,
  107. ),
  108. 'test_entity_label_callback' => array(
  109. 'label' => t('Test entity label callback'),
  110. 'fieldable' => TRUE,
  111. 'field cache' => FALSE,
  112. 'base table' => 'test_entity',
  113. 'label callback' => 'field_test_entity_label_callback',
  114. 'entity keys' => array(
  115. 'id' => 'ftid',
  116. 'revision' => 'ftvid',
  117. 'bundle' => 'fttype',
  118. ),
  119. 'bundles' => $bundles,
  120. 'view modes' => $test_entity_modes,
  121. ),
  122. );
  123. }
  124. /**
  125. * Implements hook_entity_info_alter().
  126. */
  127. function field_test_entity_info_alter(&$entity_info) {
  128. // Enable/disable field_test as a translation handler.
  129. foreach (field_test_entity_info_translatable() as $entity_type => $translatable) {
  130. $entity_info[$entity_type]['translation']['field_test'] = $translatable;
  131. }
  132. // Disable locale as a translation handler.
  133. foreach ($entity_info as $entity_type => $info) {
  134. $entity_info[$entity_type]['translation']['locale'] = FALSE;
  135. }
  136. }
  137. /**
  138. * Helper function to enable entity translations.
  139. */
  140. function field_test_entity_info_translatable($entity_type = NULL, $translatable = NULL) {
  141. drupal_static_reset('field_has_translation_handler');
  142. $stored_value = &drupal_static(__FUNCTION__, array());
  143. if (isset($entity_type)) {
  144. $stored_value[$entity_type] = $translatable;
  145. entity_info_cache_clear();
  146. }
  147. return $stored_value;
  148. }
  149. /**
  150. * Creates a new bundle for test_entity entities.
  151. *
  152. * @param $bundle
  153. * The machine-readable name of the bundle.
  154. * @param $text
  155. * The human-readable name of the bundle. If none is provided, the machine
  156. * name will be used.
  157. */
  158. function field_test_create_bundle($bundle, $text = NULL) {
  159. $bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
  160. $bundles += array($bundle => array('label' => $text ? $text : $bundle));
  161. variable_set('field_test_bundles', $bundles);
  162. $info = field_test_entity_info();
  163. foreach ($info as $type => $type_info) {
  164. field_attach_create_bundle($type, $bundle);
  165. }
  166. }
  167. /**
  168. * Renames a bundle for test_entity entities.
  169. *
  170. * @param $bundle_old
  171. * The machine-readable name of the bundle to rename.
  172. * @param $bundle_new
  173. * The new machine-readable name of the bundle.
  174. */
  175. function field_test_rename_bundle($bundle_old, $bundle_new) {
  176. $bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
  177. $bundles[$bundle_new] = $bundles[$bundle_old];
  178. unset($bundles[$bundle_old]);
  179. variable_set('field_test_bundles', $bundles);
  180. $info = field_test_entity_info();
  181. foreach ($info as $type => $type_info) {
  182. field_attach_rename_bundle($type, $bundle_old, $bundle_new);
  183. }
  184. }
  185. /**
  186. * Deletes a bundle for test_entity objects.
  187. *
  188. * @param $bundle
  189. * The machine-readable name of the bundle to delete.
  190. */
  191. function field_test_delete_bundle($bundle) {
  192. $bundles = variable_get('field_test_bundles', array('test_bundle' => array('label' => 'Test Bundle')));
  193. unset($bundles[$bundle]);
  194. variable_set('field_test_bundles', $bundles);
  195. $info = field_test_entity_info();
  196. foreach ($info as $type => $type_info) {
  197. field_attach_delete_bundle($type, $bundle);
  198. }
  199. }
  200. /**
  201. * Creates a basic test_entity entity.
  202. */
  203. function field_test_create_stub_entity($id = 1, $vid = 1, $bundle = 'test_bundle', $label = '') {
  204. $entity = new stdClass();
  205. // Only set id and vid properties if they don't come as NULL (creation form).
  206. if (isset($id)) {
  207. $entity->ftid = $id;
  208. }
  209. if (isset($vid)) {
  210. $entity->ftvid = $vid;
  211. }
  212. $entity->fttype = $bundle;
  213. $label = !empty($label) ? $label : $bundle . ' label';
  214. $entity->ftlabel = $label;
  215. return $entity;
  216. }
  217. /**
  218. * Loads a test_entity.
  219. *
  220. * @param $ftid
  221. * The id of the entity to load.
  222. * @param $ftvid
  223. * (Optional) The revision id of the entity to load. If not specified, the
  224. * current revision will be used.
  225. * @return
  226. * The loaded entity.
  227. */
  228. function field_test_entity_test_load($ftid, $ftvid = NULL) {
  229. // Load basic strucure.
  230. $query = db_select('test_entity', 'fte', array())
  231. ->condition('fte.ftid', $ftid);
  232. if ($ftvid) {
  233. $query->join('test_entity_revision', 'fter', 'fte.ftid = fter.ftid');
  234. $query->addField('fte', 'ftid');
  235. $query->addField('fte', 'fttype');
  236. $query->addField('fter', 'ftvid');
  237. $query->condition('fter.ftvid', $ftvid);
  238. }
  239. else {
  240. $query->fields('fte');
  241. }
  242. $entities = $query->execute()->fetchAllAssoc('ftid');
  243. // Attach fields.
  244. if ($ftvid) {
  245. field_attach_load_revision('test_entity', $entities);
  246. }
  247. else {
  248. field_attach_load('test_entity', $entities);
  249. }
  250. return $entities[$ftid];
  251. }
  252. /**
  253. * Saves a test_entity.
  254. *
  255. * A new entity is created if $entity->ftid and $entity->is_new are both empty.
  256. * A new revision is created if $entity->revision is not empty.
  257. *
  258. * @param $entity
  259. * The entity to save.
  260. */
  261. function field_test_entity_save(&$entity) {
  262. field_attach_presave('test_entity', $entity);
  263. if (!isset($entity->is_new)) {
  264. $entity->is_new = empty($entity->ftid);
  265. }
  266. if (!$entity->is_new && !empty($entity->revision)) {
  267. $entity->old_ftvid = $entity->ftvid;
  268. unset($entity->ftvid);
  269. }
  270. $update_entity = TRUE;
  271. if ($entity->is_new) {
  272. drupal_write_record('test_entity', $entity);
  273. drupal_write_record('test_entity_revision', $entity);
  274. $op = 'insert';
  275. }
  276. else {
  277. drupal_write_record('test_entity', $entity, 'ftid');
  278. if (!empty($entity->revision)) {
  279. drupal_write_record('test_entity_revision', $entity);
  280. }
  281. else {
  282. drupal_write_record('test_entity_revision', $entity, 'ftvid');
  283. $update_entity = FALSE;
  284. }
  285. $op = 'update';
  286. }
  287. if ($update_entity) {
  288. db_update('test_entity')
  289. ->fields(array('ftvid' => $entity->ftvid))
  290. ->condition('ftid', $entity->ftid)
  291. ->execute();
  292. }
  293. // Save fields.
  294. $function = "field_attach_$op";
  295. $function('test_entity', $entity);
  296. }
  297. /**
  298. * Menu callback: displays the 'Add new test_entity' form.
  299. */
  300. function field_test_entity_add($fttype) {
  301. $fttype = str_replace('-', '_', $fttype);
  302. $entity = (object)array('fttype' => $fttype);
  303. drupal_set_title(t('Create test_entity @bundle', array('@bundle' => $fttype)), PASS_THROUGH);
  304. return drupal_get_form('field_test_entity_form', $entity, TRUE);
  305. }
  306. /**
  307. * Menu callback: displays the 'Edit exiisting test_entity' form.
  308. */
  309. function field_test_entity_edit($entity) {
  310. drupal_set_title(t('test_entity @ftid revision @ftvid', array('@ftid' => $entity->ftid, '@ftvid' => $entity->ftvid)), PASS_THROUGH);
  311. return drupal_get_form('field_test_entity_form', $entity);
  312. }
  313. /**
  314. * Test_entity form.
  315. */
  316. function field_test_entity_form($form, &$form_state, $entity, $add = FALSE) {
  317. // During initial form build, add the entity to the form state for use during
  318. // form building and processing. During a rebuild, use what is in the form
  319. // state.
  320. if (!isset($form_state['test_entity'])) {
  321. $form_state['test_entity'] = $entity;
  322. }
  323. else {
  324. $entity = $form_state['test_entity'];
  325. }
  326. foreach (array('ftid', 'ftvid', 'fttype') as $key) {
  327. $form[$key] = array(
  328. '#type' => 'value',
  329. '#value' => isset($entity->$key) ? $entity->$key : NULL,
  330. );
  331. }
  332. // Add field widgets.
  333. field_attach_form('test_entity', $entity, $form, $form_state);
  334. if (!$add) {
  335. $form['revision'] = array(
  336. '#access' => user_access('administer field_test content'),
  337. '#type' => 'checkbox',
  338. '#title' => t('Create new revision'),
  339. '#default_value' => FALSE,
  340. '#weight' => 100,
  341. );
  342. }
  343. $form['submit'] = array(
  344. '#type' => 'submit',
  345. '#value' => t('Save'),
  346. '#weight' => 101,
  347. );
  348. return $form;
  349. }
  350. /**
  351. * Validate handler for field_test_entity_form().
  352. */
  353. function field_test_entity_form_validate($form, &$form_state) {
  354. entity_form_field_validate('test_entity', $form, $form_state);
  355. }
  356. /**
  357. * Submit handler for field_test_entity_form().
  358. */
  359. function field_test_entity_form_submit($form, &$form_state) {
  360. $entity = field_test_entity_form_submit_build_test_entity($form, $form_state);
  361. $insert = empty($entity->ftid);
  362. field_test_entity_save($entity);
  363. $message = $insert ? t('test_entity @id has been created.', array('@id' => $entity->ftid)) : t('test_entity @id has been updated.', array('@id' => $entity->ftid));
  364. drupal_set_message($message);
  365. if ($entity->ftid) {
  366. $form_state['redirect'] = 'test-entity/manage/' . $entity->ftid . '/edit';
  367. }
  368. else {
  369. // Error on save.
  370. drupal_set_message(t('The entity could not be saved.'), 'error');
  371. $form_state['rebuild'] = TRUE;
  372. }
  373. }
  374. /**
  375. * Updates the form state's entity by processing this submission's values.
  376. */
  377. function field_test_entity_form_submit_build_test_entity($form, &$form_state) {
  378. $entity = $form_state['test_entity'];
  379. entity_form_submit_build_entity('test_entity', $entity, $form, $form_state);
  380. return $entity;
  381. }
  382. /**
  383. * Form combining two separate entities.
  384. */
  385. function field_test_entity_nested_form($form, &$form_state, $entity_1, $entity_2) {
  386. // First entity.
  387. foreach (array('ftid', 'ftvid', 'fttype') as $key) {
  388. $form[$key] = array(
  389. '#type' => 'value',
  390. '#value' => $entity_1->$key,
  391. );
  392. }
  393. field_attach_form('test_entity', $entity_1, $form, $form_state);
  394. // Second entity.
  395. $form['entity_2'] = array(
  396. '#type' => 'fieldset',
  397. '#title' => t('Second entity'),
  398. '#tree' => TRUE,
  399. '#parents' => array('entity_2'),
  400. '#weight' => 50,
  401. );
  402. foreach (array('ftid', 'ftvid', 'fttype') as $key) {
  403. $form['entity_2'][$key] = array(
  404. '#type' => 'value',
  405. '#value' => $entity_2->$key,
  406. );
  407. }
  408. field_attach_form('test_entity', $entity_2, $form['entity_2'], $form_state);
  409. $form['save'] = array(
  410. '#type' => 'submit',
  411. '#value' => t('Save'),
  412. '#weight' => 100,
  413. );
  414. return $form;
  415. }
  416. /**
  417. * Validate handler for field_test_entity_nested_form().
  418. */
  419. function field_test_entity_nested_form_validate($form, &$form_state) {
  420. $entity_1 = (object) $form_state['values'];
  421. field_attach_form_validate('test_entity', $entity_1, $form, $form_state);
  422. $entity_2 = (object) $form_state['values']['entity_2'];
  423. field_attach_form_validate('test_entity', $entity_2, $form['entity_2'], $form_state);
  424. }
  425. /**
  426. * Submit handler for field_test_entity_nested_form().
  427. */
  428. function field_test_entity_nested_form_submit($form, &$form_state) {
  429. $entity_1 = (object) $form_state['values'];
  430. field_attach_submit('test_entity', $entity_1, $form, $form_state);
  431. field_test_entity_save($entity_1);
  432. $entity_2 = (object) $form_state['values']['entity_2'];
  433. field_attach_submit('test_entity', $entity_2, $form['entity_2'], $form_state);
  434. field_test_entity_save($entity_2);
  435. drupal_set_message(t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->ftid, '@id_2' => $entity_2->ftid)));
  436. }
  437. /**
  438. * Controller class for the test_entity_bundle entity type.
  439. *
  440. * This extends the DrupalDefaultEntityController class, adding required
  441. * special handling for bundles (since they are not stored in the database).
  442. */
  443. class TestEntityBundleController extends DrupalDefaultEntityController {
  444. protected function attachLoad(&$entities, $revision_id = FALSE) {
  445. // Add bundle information.
  446. foreach ($entities as $key => $entity) {
  447. $entity->fttype = 'test_entity_bundle';
  448. $entities[$key] = $entity;
  449. }
  450. parent::attachLoad($entities, $revision_id);
  451. }
  452. }