title_test.module 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $langcode = LANGUAGE_NONE;
  80. if (isset($entity->type)) {
  81. $langcode = entity_language($entity->type, $entity);
  82. }
  83. $value = $entity->{$label_key} == $entity->{$field_name}[$langcode][0]['value'];
  84. title_test_phase_store($phase, $value);
  85. return $value;
  86. }
  87. /**
  88. * Implements hook_entity_presave().
  89. */
  90. function title_test_entity_presave($entity, $type) {
  91. if ($type == 'test_entity') {
  92. $info = entity_get_info('test_entity');
  93. $label_key = $info['entity keys']['label'];
  94. $entity->{$label_key} = DrupalWebTestCase::randomName();
  95. }
  96. }
  97. /**
  98. * Implements hook_field_attach_load().
  99. */
  100. function title_test_field_attach_load($entity_type, $entities, $age, $options) {
  101. if ($entity_type == 'test_entity') {
  102. title_test_phase_check('field_attach_load', current($entities));
  103. }
  104. }
  105. /**
  106. * Implements hook_entity_load().
  107. */
  108. function title_test_entity_load($entities, $type) {
  109. if ($type == 'test_entity') {
  110. title_test_phase_check('entity_load', current($entities));
  111. }
  112. }
  113. /**
  114. * Implements hook_entity_prepare_view().
  115. */
  116. function title_test_entity_prepare_view($entities, $type, $langcode = NULL) {
  117. if ($type == 'test_entity') {
  118. title_test_phase_check('entity_prepare_view', current($entities));
  119. }
  120. }