title_test.module 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * Testing functionality for Title module.
  5. */
  6. /**
  7. * Implements hook_entity_info().
  8. */
  9. function title_test_entity_info() {
  10. $info = array();
  11. $field = array(
  12. 'type' => 'text',
  13. 'cardinality' => 1,
  14. 'translatable' => TRUE,
  15. );
  16. $instance = array(
  17. 'required' => TRUE,
  18. 'settings' => array(
  19. 'text_processing' => 0,
  20. ),
  21. 'widget' => array(
  22. 'weight' => -5,
  23. ),
  24. );
  25. $info['test_entity'] = array(
  26. 'entity keys' => array(
  27. 'label' => 'ftlabel',
  28. ),
  29. 'field replacement' => array(
  30. 'ftlabel' => array(
  31. 'field' => $field,
  32. 'instance' => array(
  33. 'label' => t('Title'),
  34. 'description' => t('A field replacing node title.'),
  35. ) + $instance,
  36. ),
  37. ),
  38. 'controller class' => 'EntityAPIController',
  39. );
  40. return $info;
  41. }
  42. /**
  43. * Save the given test entity.
  44. */
  45. function title_test_entity_save($entity) {
  46. // field_test_entity_save does not invoke hook_entity_presave().
  47. module_invoke_all('entity_presave', $entity, 'test_entity');
  48. field_test_entity_save($entity);
  49. // field_test_entity_save does not invoke hook_entity_insert().
  50. $hook = $entity->is_new ? 'entity_insert' : 'entity_update';
  51. module_invoke_all($hook, $entity, 'test_entity');
  52. }
  53. /**
  54. * Load the given test entity.
  55. */
  56. function title_test_entity_test_load($entity) {
  57. $entity = field_test_entity_test_load($entity->ftid);
  58. // field_test_entity_load does not invoke hook_entity_load().
  59. module_invoke_all('entity_load', array($entity), 'test_entity');
  60. return $entity;
  61. }
  62. /**
  63. * Store a value for the given phase.
  64. */
  65. function title_test_phase_store($phase = NULL, $value = NULL) {
  66. $store = &drupal_static(__FUNCTION__, array());
  67. if (isset($phase)) {
  68. $store[$phase] = $value;
  69. }
  70. return $store;
  71. }
  72. /**
  73. * Check the entity label at a give phase.
  74. */
  75. function title_test_phase_check($phase, $entity) {
  76. $info = entity_get_info('test_entity');
  77. $label_key = $info['entity keys']['label'];
  78. $field_name = $label_key . '_field';
  79. $value = $entity->{$label_key} == $entity->{$field_name}[LANGUAGE_NONE][0]['value'];
  80. title_test_phase_store($phase, $value);
  81. return $value;
  82. }
  83. /**
  84. * Implements hook_entity_presave().
  85. */
  86. function title_test_entity_presave($entity, $type) {
  87. if ($type == 'test_entity') {
  88. $info = entity_get_info('test_entity');
  89. $label_key = $info['entity keys']['label'];
  90. $entity->{$label_key} = DrupalWebTestCase::randomName();
  91. }
  92. }
  93. /**
  94. * Implements hook_field_attach_load().
  95. */
  96. function title_test_field_attach_load($entity_type, $entities, $age, $options) {
  97. if ($entity_type == 'test_entity') {
  98. title_test_phase_check('field_attach_load', current($entities));
  99. }
  100. }
  101. /**
  102. * Implements hook_entity_load().
  103. */
  104. function title_test_entity_load($entities, $type) {
  105. if ($type == 'test_entity') {
  106. title_test_phase_check('entity_load', current($entities));
  107. }
  108. }
  109. /**
  110. * Implements hook_entity_prepare_view().
  111. */
  112. function title_test_entity_prepare_view($entities, $type, $langcode = NULL) {
  113. if ($type == 'test_entity') {
  114. title_test_phase_check('entity_prepare_view', current($entities));
  115. }
  116. }