field_test.entity.inc 14 KB

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