entity_test.module 7.58 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Test module for the entity API providing several entity types for testing.
6
7
 */

8
use Drupal\Core\Entity\EntityInterface;
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Filter that limits test entity list to revisable ones.
 */
const ENTITY_TEST_TYPES_REVISABLE = 1;

/**
 * Filter that limits test entity list to multilingual ones.
 */
const ENTITY_TEST_TYPES_MULTILINGUAL = 2;

/**
 * Returns a list of test entity types.
 *
 * The returned entity types are one for each available entity storage type:
 * - The plain entity_test type supports neither revisions nor multilingual
 *   properties.
 * - The entity_test_mul type supports multilingual properties.
 * - The entity_test_rev type supports revisions.
 * - The entity_test_mulrev type supports both revisions and multilingual
 *   properties.
 *
 * @param int $filter
 *   Either ENTITY_TEST_TYPES_REVISABLE to only return revisable entity types or
 *   ENTITY_TEST_TYPES_MULTILINGUAL to only return multilingual ones. Defaults
 *   to NULL, which returns all.
 *
 * @return array
 *   List with entity_types.
 */
function entity_test_entity_types($filter = NULL) {
  $types = array();
  if ($filter == NULL) {
    $types[] = 'entity_test';
  }
  if ($filter != ENTITY_TEST_TYPES_REVISABLE) {
    $types[] = 'entity_test_mul';
  }
  if ($filter != ENTITY_TEST_TYPES_MULTILINGUAL) {
    $types[] = 'entity_test_rev';
  }
  $types[] = 'entity_test_mulrev';
  return drupal_map_assoc($types);
}
53

54
/**
55
 * Implements hook_entity_info_alter().
56
 */
57
function entity_test_entity_info_alter(&$info) {
58
  // Optionally specify a translation handler for testing translations.
59
  if (state()->get('entity_test.translation')) {
60
61
62
    foreach(entity_test_entity_types() as $entity_type) {
      $info[$entity_type]['translation'][$entity_type] = TRUE;
    }
63
  }
64
65
}

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * Implements hook_field_extra_fields().
 */
function entity_test_field_extra_fields() {
  $extra['entity_test']['entity_test'] = array(
    'display' => array(
      // Note: those extra fields do not currently display anything, they are
      // just used in \Drupal\entity\Tests\EntityDisplayTest to test the
      // behavior of entity display objects,
      'display_extra_field' => array(
        'label' => t('Display extra field'),
        'description' => t('An extra field on the display side.'),
        'weight' => 5,
        'visible' => TRUE,
      ),
      'display_extra_field_hidden' => array(
        'label' => t('Display extra field (hidden)'),
        'description' => t('An extra field on the display side, hidden by default.'),
        'visible' => FALSE,
      ),
    )
  );

  return $extra;
}

92
93
94
95
96
97
98
99
100
/**
 * Implements hook_permission().
 */
function entity_test_permission() {
  $permissions = array(
    'administer entity_test content' => array(
      'title' => t('Administer entity_test content'),
      'description' => t('Manage entity_test content'),
    ),
101
102
103
104
105
106
    'view test entity' => array(
      'title' => t('View test entities'),
    ),
    'view test entity translations' => array(
      'title' => t('View translations of test entities'),
    ),
107
108
109
110
111
112
113
114
115
116
  );
  return $permissions;
}

/**
 * Implements hook_menu().
 */
function entity_test_menu() {
  $items = array();

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  foreach(entity_test_entity_types() as $entity_type) {
    $items[$entity_type . '/add'] = array(
      'title' => 'Add an @type',
      'title arguments' => array('@type' => $entity_type),
      'page callback' => 'entity_test_add',
      'page arguments' => array($entity_type),
      'access arguments' => array('administer entity_test content'),
      'type' => MENU_NORMAL_ITEM,
    );

    $items[$entity_type . '/manage/%' . $entity_type] = array(
      'title' => 'Edit @type',
      'title arguments' => array('@type' => $entity_type),
      'page callback' => 'entity_test_edit',
      'page arguments' => array(2),
      'access arguments' => array('administer entity_test content'),
      'type' => MENU_NORMAL_ITEM,
    );

    $items[$entity_type . '/manage/%' . $entity_type . '/edit'] = array(
      'title' => 'Edit',
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
  }
141
142
143
144

  return $items;
}

145
146
147
148
149
/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function entity_test_form_node_form_alter(&$form, &$form_state, $form_id) {
  $langcode = $form_state['controller']->getFormLangcode($form_state);
150
  state()->set('entity_test.form_langcode', $langcode);
151
152
}

153
154
/**
 * Menu callback: displays the 'Add new entity_test' form.
155
 *
156
157
158
 * @param string $entity_type
 *   Name of the entity type for which a create form should be displayed.
 *
159
160
161
162
 * @return array
 *   The processed form for a new entity_test.
 *
 * @see entity_test_menu()
163
 */
164
165
166
function entity_test_add($entity_type) {
  drupal_set_title(t('Create an @type', array('@type' => $entity_type)));
  $entity = entity_create($entity_type, array());
167
168
169
170
171
  return entity_get_form($entity);
}

/**
 * Menu callback: displays the 'Edit existing entity_test' form.
172
 *
173
 * @param \Drupal\Core\Entity\EntityInterface $entity
174
175
176
 *   The entity to be edited.
 *
 * @return array
177
 *   The processed form for the edited entity.
178
179
 *
 * @see entity_test_menu()
180
 */
181
function entity_test_edit(EntityInterface $entity) {
182
  drupal_set_title($entity->label(), PASS_THROUGH);
183
184
185
  return entity_get_form($entity);
}

186
187
188
/**
 * Loads a test entity.
 *
189
 * @param int $id
190
 *   A test entity ID.
191
 * @param bool $reset
192
193
 *   A boolean indicating that the internal cache should be reset.
 *
194
 * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTest
195
196
197
 *   The loaded entity object, or FALSE if the entity cannot be loaded.
 */
function entity_test_load($id, $reset = FALSE) {
198
  return entity_load('entity_test', $id, $reset);
199
200
201
}

/**
202
 * Loads a test entity.
203
 *
204
205
 * @param int $id
 *   A test entity ID.
206
 * @param bool $reset
207
208
 *   A boolean indicating that the internal cache should be reset.
 *
209
210
 * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestRev
 *   The loaded entity object, or FALSE if the entity cannot be loaded.
211
 */
212
213
function entity_test_rev_load($id, $reset = FALSE) {
  return entity_load('entity_test_rev', $id, $reset);
214
215
216
}

/**
217
218
219
220
221
222
 * Loads a test entity.
 *
 * @param int $id
 *   A test entity ID.
 * @param bool $reset
 *   A boolean indicating that the internal cache should be reset.
223
 *
224
225
 * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestMul
 *   The loaded entity object, or FALSE if the entity cannot be loaded.
226
 */
227
228
function entity_test_mul_load($id, $reset = FALSE) {
  return entity_load('entity_test_mul', $id, $reset);
229
}
230
231

/**
232
233
234
235
236
237
238
239
240
 * Loads a test entity.
 *
 * @param int $id
 *   A test entity ID.
 * @param bool $reset
 *   A boolean indicating that the internal cache should be reset.
 *
 * @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestMulRev
 *   The loaded entity object, or FALSE if the entity cannot be loaded.
241
 */
242
243
function entity_test_mulrev_load($id, $reset = FALSE) {
  return entity_load('entity_test_mulrev', $id, $reset);
244
}
245
246
247
248
249
250
251
252
253

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function entity_test_entity_test_insert($entity) {
  if ($entity->name->value == 'fail_insert') {
    throw new Exception("Test exception rollback.");
  }
}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

/**
 * Entity label callback.
 *
 * @param $entity_type
 *   The entity type.
 * @param $entity
 *   The entity object.
 * @param $langcocde
 *   (optional) The langcode.
 *
 * @return
 *   The label of the entity prefixed with "label callback".
 */
function entity_test_label_callback($entity_type, $entity, $langcode = NULL) {
  return 'label callback ' . $entity->name->value;
}
271
272
273
274
275
276
277
278
279
280
281
282
283
284

/**
 * Implements hook_entity_view_mode_info().
 */
function entity_test_entity_view_mode_info() {
  $view_modes['entity_test_render']['full'] = array(
    'label' => t('Full'),
  );
  $view_modes['entity_test_render']['test'] = array(
    'label' => t('Test'),
  );

  return $view_modes;
}