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