entity_test.module
7.71 KB
1
2
3
4
5
6
7
8
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
53
54
55
56
57
58
59
60
61
62
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* @file
* Test moduel for the entity API.
*/
/**
* Implements hook_entity_info().
*/
function entity_test_entity_info() {
$return = array(
'entity_test' => array(
'label' => t('Test Entity'),
'plural label' => t('Test Entities'),
'description' => t('An entity type used by the entity API tests.'),
'entity class' => 'EntityClass',
'controller class' => 'EntityAPIController',
'base table' => 'entity_test',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pid',
'bundle' => 'name',
),
// Make use the class' label() and uri() implementation by default.
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
'module' => 'entity_test',
),
'entity_test_type' => array(
'label' => t('Test entity type'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIControllerExportable',
'base table' => 'entity_test_type',
'fieldable' => FALSE,
'bundle of' => 'entity_test',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
),
'module' => 'entity_test',
),
'entity_test2' => array(
'label' => t('Test Entity (revision support)'),
'entity class' => 'EntityClassRevision',
'controller class' => 'EntityAPIController',
'base table' => 'entity_test2',
'revision table' => 'entity_test2_revision',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pid',
'revision' => 'revision_id',
),
// Make use of the class label() and uri() implementation by default.
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
),
);
// Add bundle info but bypass entity_load() as we cannot use it here.
$types = db_select('entity_test_type', 'et')
->fields('et')
->execute()
->fetchAllAssoc('name');
foreach ($types as $name => $type) {
$return['entity_test']['bundles'][$name] = array(
'label' => $type->label,
);
}
// Support entity cache module.
if (module_exists('entitycache')) {
$return['entity_test']['field cache'] = FALSE;
$return['entity_test']['entity cache'] = TRUE;
}
return $return;
}
/**
* Gets an array of all test entity types, keyed by the name.
*
* @param $name
* If set, the type with the given name is returned.
*/
function entity_test_get_types($name = NULL) {
$types = entity_load_multiple_by_name('entity_test_type', isset($name) ? array($name) : FALSE);
return isset($name) ? reset($types) : $types;
}
/**
* Load multiple test entities based on certain conditions.
*
* @param $pids
* An array of entity IDs.
* @param $conditions
* An array of conditions to match against the {entity} table.
* @param $reset
* A boolean indicating that the internal cache should be reset.
* @return
* An array of test entity objects, indexed by pid.
*/
function entity_test_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('entity_test', $pids, $conditions, $reset);
}
/**
* Delete multiple test entities.
*
* @param $pids
* An array of test entity IDs.
*/
function entity_test_delete_multiple(array $pids) {
entity_get_controller('entity_test')->delete($pids);
}
/**
* Main class for test entities.
*/
class EntityClass extends Entity {
public function __construct(array $values = array(), $entityType = NULL) {
parent::__construct($values, 'entity_test');
}
/**
* Override buildContent() to add the username to the output.
*/
public function buildContent($view_mode = 'full', $langcode = NULL) {
$content['user'] = array(
'#markup' => "User: ". format_username(user_load($this->uid)),
);
return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode, $content);
}
/**
* Specifies the default label, which is picked up by label() by default.
*/
protected function defaultLabel() {
$type = entity_test_get_types($this->name);
return $type->label;
}
/**
* Specifies the default uri, which is picked up by uri() by default.
*/
protected function defaultURI() {
return array('path' => 'custom/' . $this->identifier());
}
}
/**
* Main class for test entities (with revision support).
*/
class EntityClassRevision extends EntityClass {
public function __construct(array $values = array(), $entityType = NULL) {
Entity::__construct($values, 'entity_test2');
}
}
/**
*
*
* Some hook implementations used by the tests.
*
*
*/
/**
* Implements hook_entity_insert().
*/
function entity_test_entity_insert($entity, $entity_type) {
if ($entity_type == 'entity_test_type') {
$_SESSION['entity_hook_test']['entity_insert'][] = entity_id($entity_type, $entity);
}
}
/**
* Implements hook_entity_update().
*/
function entity_test_entity_update($entity, $entity_type) {
$_SESSION['entity_hook_test']['entity_update'][] = entity_id($entity_type, $entity);
}
/**
* Implements hook_entity_delete().
*/
function entity_test_entity_delete($entity, $entity_type) {
if ($entity_type == 'entity_test_type') {
$_SESSION['entity_hook_test']['entity_delete'][] = entity_id($entity_type, $entity);
}
}
/**
* Implements hook_entity_test_type_insert().
*/
function entity_test_entity_test_type_insert($entity) {
$_SESSION['entity_hook_test']['entity_test_type_insert'][] = $entity->identifier();
}
/**
* Implements hook_entity_test_type_update().
*/
function entity_test_entity_test_type_update($entity) {
$_SESSION['entity_hook_test']['entity_test_type_update'][] = $entity->identifier();
// Determine changes on update.
if (!empty($entity->original) && $entity->original->label == 'test_changes') {
if ($entity->original->label != $entity->label) {
$entity->label .= '_update';
}
}
}
/**
* Implements hook_entity_test_type_delete().
*/
function entity_test_entity_test_type_delete($entity) {
$_SESSION['entity_hook_test']['entity_test_type_delete'][] = $entity->identifier();
}
/**
* Implements hook_entity_test_type_presave().
*/
function entity_test_entity_test_type_presave($entity) {
// Determine changes.
if (!empty($entity->original) && $entity->original->label == 'test_changes') {
if ($entity->original->label != $entity->label) {
$entity->label .= '_presave';
}
}
}
/**
* Implements hook_entity_property_info_alter() for testing an property of type
* 'entity'.
*/
function entity_test_entity_property_info_alter(&$info) {
$info['node']['properties']['reference'] = array(
'label' => t('Test reference'),
'description' => t('A generic entity reference.'),
'getter callback' => 'entity_test_entity_getter',
'setter callback' => 'entity_test_entity_setter',
'type' => 'entity',
);
}
/**
* Getter callback for the 'reference' property.
*/
function entity_test_entity_getter($node) {
if (empty($node->entity)) {
$node->entity = array('type' => 'user', 'id' => $node->uid);
}
// We have to return the entity wrapped.
// Special handling for anonymous user.
if ($node->entity['type'] === 'user' && empty($node->entity['id'])) {
return entity_metadata_wrapper('user', drupal_anonymous_user());
}
else {
return entity_metadata_wrapper($node->entity['type'], $node->entity['id']);
}
}
/**
* Setter callback for the 'reference' property.
*/
function entity_test_entity_setter($node, $property_name, $wrapper) {
// The entity has to be passed wrapped.
$node->entity = array('type' => $wrapper->type(), 'id' => $wrapper->getIdentifier());
}