1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Interface for entity controller classes.
|
5
|
*
|
6
|
* All entity controller classes specified via the 'controller class' key
|
7
|
* returned by hook_entity_info() or hook_entity_info_alter() have to implement
|
8
|
* this interface.
|
9
|
*
|
10
|
* Most simple, SQL-based entity controllers will do better by extending
|
11
|
* DrupalDefaultEntityController instead of implementing this interface
|
12
|
* directly.
|
13
|
*/
|
14
|
interface DrupalEntityControllerInterface {
|
15
|
|
16
|
/**
|
17
|
* Resets the internal, static entity cache.
|
18
|
*
|
19
|
* @param $ids
|
20
|
* (optional) If specified, the cache is reset for the entities with the
|
21
|
* given ids only.
|
22
|
*/
|
23
|
public function resetCache(array $ids = NULL);
|
24
|
|
25
|
/**
|
26
|
* Loads one or more entities.
|
27
|
*
|
28
|
* @param $ids
|
29
|
* An array of entity IDs, or FALSE to load all entities.
|
30
|
* @param $conditions
|
31
|
* An array of conditions. Keys are field names on the entity's base table.
|
32
|
* Values will be compared for equality. All the comparisons will be ANDed
|
33
|
* together. This parameter is deprecated; use an EntityFieldQuery instead.
|
34
|
*
|
35
|
* @return
|
36
|
* An array of entity objects indexed by their ids. When no results are
|
37
|
* found, an empty array is returned.
|
38
|
*/
|
39
|
public function load($ids = array(), $conditions = array());
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* Default implementation of DrupalEntityControllerInterface.
|
44
|
*
|
45
|
* This class can be used as-is by most simple entity types. Entity types
|
46
|
* requiring special handling can extend the class.
|
47
|
*/
|
48
|
class DrupalDefaultEntityController implements DrupalEntityControllerInterface {
|
49
|
|
50
|
/**
|
51
|
* Static cache of entities, keyed by entity ID.
|
52
|
*
|
53
|
* @var array
|
54
|
*/
|
55
|
protected $entityCache;
|
56
|
|
57
|
/**
|
58
|
* Entity type for this controller instance.
|
59
|
*
|
60
|
* @var string
|
61
|
*/
|
62
|
protected $entityType;
|
63
|
|
64
|
/**
|
65
|
* Array of information about the entity.
|
66
|
*
|
67
|
* @var array
|
68
|
*
|
69
|
* @see entity_get_info()
|
70
|
*/
|
71
|
protected $entityInfo;
|
72
|
|
73
|
/**
|
74
|
* Additional arguments to pass to hook_TYPE_load().
|
75
|
*
|
76
|
* Set before calling DrupalDefaultEntityController::attachLoad().
|
77
|
*
|
78
|
* @var array
|
79
|
*/
|
80
|
protected $hookLoadArguments;
|
81
|
|
82
|
/**
|
83
|
* Name of the entity's ID field in the entity database table.
|
84
|
*
|
85
|
* @var string
|
86
|
*/
|
87
|
protected $idKey;
|
88
|
|
89
|
/**
|
90
|
* Name of entity's revision database table field, if it supports revisions.
|
91
|
*
|
92
|
* Has the value FALSE if this entity does not use revisions.
|
93
|
*
|
94
|
* @var string
|
95
|
*/
|
96
|
protected $revisionKey;
|
97
|
|
98
|
/**
|
99
|
* The table that stores revisions, if the entity supports revisions.
|
100
|
*
|
101
|
* @var string
|
102
|
*/
|
103
|
protected $revisionTable;
|
104
|
|
105
|
/**
|
106
|
* Whether this entity type should use the static cache.
|
107
|
*
|
108
|
* Set by entity info.
|
109
|
*
|
110
|
* @var boolean
|
111
|
*/
|
112
|
protected $cache;
|
113
|
|
114
|
/**
|
115
|
* Constructor: sets basic variables.
|
116
|
*
|
117
|
* @param $entityType
|
118
|
* The entity type for which the instance is created.
|
119
|
*/
|
120
|
public function __construct($entityType) {
|
121
|
$this->entityType = $entityType;
|
122
|
$this->entityInfo = entity_get_info($entityType);
|
123
|
$this->entityCache = array();
|
124
|
$this->hookLoadArguments = array();
|
125
|
$this->idKey = $this->entityInfo['entity keys']['id'];
|
126
|
|
127
|
// Check if the entity type supports revisions.
|
128
|
if (!empty($this->entityInfo['entity keys']['revision'])) {
|
129
|
$this->revisionKey = $this->entityInfo['entity keys']['revision'];
|
130
|
$this->revisionTable = $this->entityInfo['revision table'];
|
131
|
}
|
132
|
else {
|
133
|
$this->revisionKey = FALSE;
|
134
|
}
|
135
|
|
136
|
// Check if the entity type supports static caching of loaded entities.
|
137
|
$this->cache = !empty($this->entityInfo['static cache']);
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Implements DrupalEntityControllerInterface::resetCache().
|
142
|
*/
|
143
|
public function resetCache(array $ids = NULL) {
|
144
|
if (isset($ids)) {
|
145
|
foreach ($ids as $id) {
|
146
|
unset($this->entityCache[$id]);
|
147
|
}
|
148
|
}
|
149
|
else {
|
150
|
$this->entityCache = array();
|
151
|
}
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Implements DrupalEntityControllerInterface::load().
|
156
|
*/
|
157
|
public function load($ids = array(), $conditions = array()) {
|
158
|
$entities = array();
|
159
|
|
160
|
// Revisions are not statically cached, and require a different query to
|
161
|
// other conditions, so separate the revision id into its own variable.
|
162
|
if ($this->revisionKey && isset($conditions[$this->revisionKey])) {
|
163
|
$revision_id = $conditions[$this->revisionKey];
|
164
|
unset($conditions[$this->revisionKey]);
|
165
|
}
|
166
|
else {
|
167
|
$revision_id = FALSE;
|
168
|
}
|
169
|
|
170
|
// Create a new variable which is either a prepared version of the $ids
|
171
|
// array for later comparison with the entity cache, or FALSE if no $ids
|
172
|
// were passed. The $ids array is reduced as items are loaded from cache,
|
173
|
// and we need to know if it's empty for this reason to avoid querying the
|
174
|
// database when all requested entities are loaded from cache.
|
175
|
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
|
176
|
// Try to load entities from the static cache, if the entity type supports
|
177
|
// static caching.
|
178
|
if ($this->cache && !$revision_id) {
|
179
|
$entities += $this->cacheGet($ids, $conditions);
|
180
|
// If any entities were loaded, remove them from the ids still to load.
|
181
|
if ($passed_ids) {
|
182
|
$ids = array_keys(array_diff_key($passed_ids, $entities));
|
183
|
}
|
184
|
}
|
185
|
|
186
|
// Load any remaining entities from the database. This is the case if $ids
|
187
|
// is set to FALSE (so we load all entities), if there are any ids left to
|
188
|
// load, if loading a revision, or if $conditions was passed without $ids.
|
189
|
if ($ids === FALSE || $ids || $revision_id || ($conditions && !$passed_ids)) {
|
190
|
// Build the query.
|
191
|
$query = $this->buildQuery($ids, $conditions, $revision_id);
|
192
|
$queried_entities = $query
|
193
|
->execute()
|
194
|
->fetchAllAssoc($this->idKey);
|
195
|
}
|
196
|
|
197
|
// Pass all entities loaded from the database through $this->attachLoad(),
|
198
|
// which attaches fields (if supported by the entity type) and calls the
|
199
|
// entity type specific load callback, for example hook_node_load().
|
200
|
if (!empty($queried_entities)) {
|
201
|
$this->attachLoad($queried_entities, $revision_id);
|
202
|
$entities += $queried_entities;
|
203
|
}
|
204
|
|
205
|
if ($this->cache) {
|
206
|
// Add entities to the cache if we are not loading a revision.
|
207
|
if (!empty($queried_entities) && !$revision_id) {
|
208
|
$this->cacheSet($queried_entities);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
// Ensure that the returned array is ordered the same as the original
|
213
|
// $ids array if this was passed in and remove any invalid ids.
|
214
|
if ($passed_ids) {
|
215
|
// Remove any invalid ids from the array.
|
216
|
$passed_ids = array_intersect_key($passed_ids, $entities);
|
217
|
foreach ($entities as $entity) {
|
218
|
$passed_ids[$entity->{$this->idKey}] = $entity;
|
219
|
}
|
220
|
$entities = $passed_ids;
|
221
|
}
|
222
|
|
223
|
return $entities;
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Builds the query to load the entity.
|
228
|
*
|
229
|
* This has full revision support. For entities requiring special queries,
|
230
|
* the class can be extended, and the default query can be constructed by
|
231
|
* calling parent::buildQuery(). This is usually necessary when the object
|
232
|
* being loaded needs to be augmented with additional data from another
|
233
|
* table, such as loading node type into comments or vocabulary machine name
|
234
|
* into terms, however it can also support $conditions on different tables.
|
235
|
* See CommentController::buildQuery() or TaxonomyTermController::buildQuery()
|
236
|
* for examples.
|
237
|
*
|
238
|
* @param $ids
|
239
|
* An array of entity IDs, or FALSE to load all entities.
|
240
|
* @param $conditions
|
241
|
* An array of conditions. Keys are field names on the entity's base table.
|
242
|
* Values will be compared for equality. All the comparisons will be ANDed
|
243
|
* together. This parameter is deprecated; use an EntityFieldQuery instead.
|
244
|
* @param $revision_id
|
245
|
* The ID of the revision to load, or FALSE if this query is asking for the
|
246
|
* most current revision(s).
|
247
|
*
|
248
|
* @return SelectQuery
|
249
|
* A SelectQuery object for loading the entity.
|
250
|
*/
|
251
|
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
|
252
|
$query = db_select($this->entityInfo['base table'], 'base');
|
253
|
|
254
|
$query->addTag($this->entityType . '_load_multiple');
|
255
|
|
256
|
if ($revision_id) {
|
257
|
$query->join($this->revisionTable, 'revision', "revision.{$this->idKey} = base.{$this->idKey} AND revision.{$this->revisionKey} = :revisionId", array(':revisionId' => $revision_id));
|
258
|
}
|
259
|
elseif ($this->revisionKey) {
|
260
|
$query->join($this->revisionTable, 'revision', "revision.{$this->revisionKey} = base.{$this->revisionKey}");
|
261
|
}
|
262
|
|
263
|
// Add fields from the {entity} table.
|
264
|
$entity_fields = $this->entityInfo['schema_fields_sql']['base table'];
|
265
|
|
266
|
if ($this->revisionKey) {
|
267
|
// Add all fields from the {entity_revision} table.
|
268
|
$entity_revision_fields = drupal_map_assoc($this->entityInfo['schema_fields_sql']['revision table']);
|
269
|
// The id field is provided by entity, so remove it.
|
270
|
unset($entity_revision_fields[$this->idKey]);
|
271
|
|
272
|
// Remove all fields from the base table that are also fields by the same
|
273
|
// name in the revision table.
|
274
|
$entity_field_keys = array_flip($entity_fields);
|
275
|
foreach ($entity_revision_fields as $key => $name) {
|
276
|
if (isset($entity_field_keys[$name])) {
|
277
|
unset($entity_fields[$entity_field_keys[$name]]);
|
278
|
}
|
279
|
}
|
280
|
$query->fields('revision', $entity_revision_fields);
|
281
|
}
|
282
|
|
283
|
$query->fields('base', $entity_fields);
|
284
|
|
285
|
if ($ids) {
|
286
|
$query->condition("base.{$this->idKey}", $ids, 'IN');
|
287
|
}
|
288
|
if ($conditions) {
|
289
|
foreach ($conditions as $field => $value) {
|
290
|
$query->condition('base.' . $field, $value);
|
291
|
}
|
292
|
}
|
293
|
return $query;
|
294
|
}
|
295
|
|
296
|
/**
|
297
|
* Attaches data to entities upon loading.
|
298
|
*
|
299
|
* This will attach fields, if the entity is fieldable. It calls
|
300
|
* hook_entity_load() for modules which need to add data to all entities.
|
301
|
* It also calls hook_TYPE_load() on the loaded entities. For example
|
302
|
* hook_node_load() or hook_user_load(). If your hook_TYPE_load()
|
303
|
* expects special parameters apart from the queried entities, you can set
|
304
|
* $this->hookLoadArguments prior to calling the method.
|
305
|
* See NodeController::attachLoad() for an example.
|
306
|
*
|
307
|
* @param $queried_entities
|
308
|
* Associative array of query results, keyed on the entity ID.
|
309
|
* @param $revision_id
|
310
|
* ID of the revision that was loaded, or FALSE if the most current revision
|
311
|
* was loaded.
|
312
|
*/
|
313
|
protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
|
314
|
// Attach fields.
|
315
|
if ($this->entityInfo['fieldable']) {
|
316
|
if ($revision_id) {
|
317
|
field_attach_load_revision($this->entityType, $queried_entities);
|
318
|
}
|
319
|
else {
|
320
|
field_attach_load($this->entityType, $queried_entities);
|
321
|
}
|
322
|
}
|
323
|
|
324
|
// Call hook_entity_load().
|
325
|
foreach (module_implements('entity_load') as $module) {
|
326
|
$function = $module . '_entity_load';
|
327
|
$function($queried_entities, $this->entityType);
|
328
|
}
|
329
|
// Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
|
330
|
// always the queried entities, followed by additional arguments set in
|
331
|
// $this->hookLoadArguments.
|
332
|
$args = array_merge(array($queried_entities), $this->hookLoadArguments);
|
333
|
foreach (module_implements($this->entityInfo['load hook']) as $module) {
|
334
|
call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
|
335
|
}
|
336
|
}
|
337
|
|
338
|
/**
|
339
|
* Gets entities from the static cache.
|
340
|
*
|
341
|
* @param $ids
|
342
|
* If not empty, return entities that match these IDs.
|
343
|
* @param $conditions
|
344
|
* If set, return entities that match all of these conditions.
|
345
|
*
|
346
|
* @return
|
347
|
* Array of entities from the entity cache.
|
348
|
*/
|
349
|
protected function cacheGet($ids, $conditions = array()) {
|
350
|
$entities = array();
|
351
|
// Load any available entities from the internal cache.
|
352
|
if (!empty($this->entityCache)) {
|
353
|
if ($ids) {
|
354
|
$entities += array_intersect_key($this->entityCache, array_flip($ids));
|
355
|
}
|
356
|
// If loading entities only by conditions, fetch all available entities
|
357
|
// from the cache. Entities which don't match are removed later.
|
358
|
elseif ($conditions) {
|
359
|
$entities = $this->entityCache;
|
360
|
}
|
361
|
}
|
362
|
|
363
|
// Exclude any entities loaded from cache if they don't match $conditions.
|
364
|
// This ensures the same behavior whether loading from memory or database.
|
365
|
if ($conditions) {
|
366
|
foreach ($entities as $entity) {
|
367
|
// Iterate over all conditions and compare them to the entity
|
368
|
// properties. We cannot use array_diff_assoc() here since the
|
369
|
// conditions can be nested arrays, too.
|
370
|
foreach ($conditions as $property_name => $condition) {
|
371
|
if (is_array($condition)) {
|
372
|
// Multiple condition values for one property are treated as OR
|
373
|
// operation: only if the value is not at all in the condition array
|
374
|
// we remove the entity.
|
375
|
if (!in_array($entity->{$property_name}, $condition)) {
|
376
|
unset($entities[$entity->{$this->idKey}]);
|
377
|
continue 2;
|
378
|
}
|
379
|
}
|
380
|
elseif ($condition != $entity->{$property_name}) {
|
381
|
unset($entities[$entity->{$this->idKey}]);
|
382
|
continue 2;
|
383
|
}
|
384
|
}
|
385
|
}
|
386
|
}
|
387
|
return $entities;
|
388
|
}
|
389
|
|
390
|
/**
|
391
|
* Stores entities in the static entity cache.
|
392
|
*
|
393
|
* @param $entities
|
394
|
* Entities to store in the cache.
|
395
|
*/
|
396
|
protected function cacheSet($entities) {
|
397
|
$this->entityCache += $entities;
|
398
|
}
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Exception thrown by EntityFieldQuery() on unsupported query syntax.
|
403
|
*
|
404
|
* Some storage modules might not support the full range of the syntax for
|
405
|
* conditions, and will raise an EntityFieldQueryException when an unsupported
|
406
|
* condition was specified.
|
407
|
*/
|
408
|
class EntityFieldQueryException extends Exception {}
|
409
|
|
410
|
/**
|
411
|
* Retrieves entities matching a given set of conditions.
|
412
|
*
|
413
|
* This class allows finding entities based on entity properties (for example,
|
414
|
* node->changed), field values, and generic entity meta data (bundle,
|
415
|
* entity type, entity id, and revision ID). It is not possible to query across
|
416
|
* multiple entity types. For example, there is no facility to find published
|
417
|
* nodes written by users created in the last hour, as this would require
|
418
|
* querying both node->status and user->created.
|
419
|
*
|
420
|
* Normally we would not want to have public properties on the object, as that
|
421
|
* allows the object's state to become inconsistent too easily. However, this
|
422
|
* class's standard use case involves primarily code that does need to have
|
423
|
* direct access to the collected properties in order to handle alternate
|
424
|
* execution routines. We therefore use public properties for simplicity. Note
|
425
|
* that code that is simply creating and running a field query should still use
|
426
|
* the appropriate methods to add conditions on the query.
|
427
|
*
|
428
|
* Storage engines are not required to support every type of query. By default,
|
429
|
* an EntityFieldQueryException will be raised if an unsupported condition is
|
430
|
* specified or if the query has field conditions or sorts that are stored in
|
431
|
* different field storage engines. However, this logic can be overridden in
|
432
|
* hook_entity_query_alter().
|
433
|
*
|
434
|
* Also note that this query does not automatically respect entity access
|
435
|
* restrictions. Node access control is performed by the SQL storage engine but
|
436
|
* other storage engines might not do this.
|
437
|
*/
|
438
|
class EntityFieldQuery {
|
439
|
|
440
|
/**
|
441
|
* Indicates that both deleted and non-deleted fields should be returned.
|
442
|
*
|
443
|
* @see EntityFieldQuery::deleted()
|
444
|
*/
|
445
|
const RETURN_ALL = NULL;
|
446
|
|
447
|
/**
|
448
|
* TRUE if the query has already been altered, FALSE if it hasn't.
|
449
|
*
|
450
|
* Used in alter hooks to check for cloned queries that have already been
|
451
|
* altered prior to the clone (for example, the pager count query).
|
452
|
*
|
453
|
* @var boolean
|
454
|
*/
|
455
|
public $altered = FALSE;
|
456
|
|
457
|
/**
|
458
|
* Associative array of entity-generic metadata conditions.
|
459
|
*
|
460
|
* @var array
|
461
|
*
|
462
|
* @see EntityFieldQuery::entityCondition()
|
463
|
*/
|
464
|
public $entityConditions = array();
|
465
|
|
466
|
/**
|
467
|
* List of field conditions.
|
468
|
*
|
469
|
* @var array
|
470
|
*
|
471
|
* @see EntityFieldQuery::fieldCondition()
|
472
|
*/
|
473
|
public $fieldConditions = array();
|
474
|
|
475
|
/**
|
476
|
* List of field meta conditions (language and delta).
|
477
|
*
|
478
|
* Field conditions operate on columns specified by hook_field_schema(),
|
479
|
* the meta conditions operate on columns added by the system: delta
|
480
|
* and language. These can not be mixed with the field conditions because
|
481
|
* field columns can have any name including delta and language.
|
482
|
*
|
483
|
* @var array
|
484
|
*
|
485
|
* @see EntityFieldQuery::fieldLanguageCondition()
|
486
|
* @see EntityFieldQuery::fieldDeltaCondition()
|
487
|
*/
|
488
|
public $fieldMetaConditions = array();
|
489
|
|
490
|
/**
|
491
|
* List of property conditions.
|
492
|
*
|
493
|
* @var array
|
494
|
*
|
495
|
* @see EntityFieldQuery::propertyCondition()
|
496
|
*/
|
497
|
public $propertyConditions = array();
|
498
|
|
499
|
/**
|
500
|
* List of order clauses.
|
501
|
*
|
502
|
* @var array
|
503
|
*/
|
504
|
public $order = array();
|
505
|
|
506
|
/**
|
507
|
* The query range.
|
508
|
*
|
509
|
* @var array
|
510
|
*
|
511
|
* @see EntityFieldQuery::range()
|
512
|
*/
|
513
|
public $range = array();
|
514
|
|
515
|
/**
|
516
|
* The query pager data.
|
517
|
*
|
518
|
* @var array
|
519
|
*
|
520
|
* @see EntityFieldQuery::pager()
|
521
|
*/
|
522
|
public $pager = array();
|
523
|
|
524
|
/**
|
525
|
* Query behavior for deleted data.
|
526
|
*
|
527
|
* TRUE to return only deleted data, FALSE to return only non-deleted data,
|
528
|
* EntityFieldQuery::RETURN_ALL to return everything.
|
529
|
*
|
530
|
* @see EntityFieldQuery::deleted()
|
531
|
*/
|
532
|
public $deleted = FALSE;
|
533
|
|
534
|
/**
|
535
|
* A list of field arrays used.
|
536
|
*
|
537
|
* Field names passed to EntityFieldQuery::fieldCondition() and
|
538
|
* EntityFieldQuery::fieldOrderBy() are run through field_info_field() before
|
539
|
* stored in this array. This way, the elements of this array are field
|
540
|
* arrays.
|
541
|
*
|
542
|
* @var array
|
543
|
*/
|
544
|
public $fields = array();
|
545
|
|
546
|
/**
|
547
|
* TRUE if this is a count query, FALSE if it isn't.
|
548
|
*
|
549
|
* @var boolean
|
550
|
*/
|
551
|
public $count = FALSE;
|
552
|
|
553
|
/**
|
554
|
* Flag indicating whether this is querying current or all revisions.
|
555
|
*
|
556
|
* @var int
|
557
|
*
|
558
|
* @see EntityFieldQuery::age()
|
559
|
*/
|
560
|
public $age = FIELD_LOAD_CURRENT;
|
561
|
|
562
|
/**
|
563
|
* A list of the tags added to this query.
|
564
|
*
|
565
|
* @var array
|
566
|
*
|
567
|
* @see EntityFieldQuery::addTag()
|
568
|
*/
|
569
|
public $tags = array();
|
570
|
|
571
|
/**
|
572
|
* A list of metadata added to this query.
|
573
|
*
|
574
|
* @var array
|
575
|
*
|
576
|
* @see EntityFieldQuery::addMetaData()
|
577
|
*/
|
578
|
public $metaData = array();
|
579
|
|
580
|
/**
|
581
|
* The ordered results.
|
582
|
*
|
583
|
* @var array
|
584
|
*
|
585
|
* @see EntityFieldQuery::execute().
|
586
|
*/
|
587
|
public $orderedResults = array();
|
588
|
|
589
|
/**
|
590
|
* The method executing the query, if it is overriding the default.
|
591
|
*
|
592
|
* @var string
|
593
|
*
|
594
|
* @see EntityFieldQuery::execute().
|
595
|
*/
|
596
|
public $executeCallback = '';
|
597
|
|
598
|
/**
|
599
|
* Adds a condition on entity-generic metadata.
|
600
|
*
|
601
|
* If the overall query contains only entity conditions or ordering, or if
|
602
|
* there are property conditions, then specifying the entity type is
|
603
|
* mandatory. If there are field conditions or ordering but no property
|
604
|
* conditions or ordering, then specifying an entity type is optional. While
|
605
|
* the field storage engine might support field conditions on more than one
|
606
|
* entity type, there is no way to query across multiple entity base tables by
|
607
|
* default. To specify the entity type, pass in 'entity_type' for $name,
|
608
|
* the type as a string for $value, and no $operator (it's disregarded).
|
609
|
*
|
610
|
* 'bundle', 'revision_id' and 'entity_id' have no such restrictions.
|
611
|
*
|
612
|
* Note: The "comment" entity type does not support bundle conditions.
|
613
|
*
|
614
|
* @param $name
|
615
|
* 'entity_type', 'bundle', 'revision_id' or 'entity_id'.
|
616
|
* @param $value
|
617
|
* The value for $name. In most cases, this is a scalar. For more complex
|
618
|
* options, it is an array. The meaning of each element in the array is
|
619
|
* dependent on $operator.
|
620
|
* @param $operator
|
621
|
* Possible values:
|
622
|
* - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
|
623
|
* operators expect $value to be a literal of the same type as the
|
624
|
* column.
|
625
|
* - 'IN', 'NOT IN': These operators expect $value to be an array of
|
626
|
* literals of the same type as the column.
|
627
|
* - 'BETWEEN': This operator expects $value to be an array of two literals
|
628
|
* of the same type as the column.
|
629
|
* The operator can be omitted, and will default to 'IN' if the value is an
|
630
|
* array, or to '=' otherwise.
|
631
|
*
|
632
|
* @return EntityFieldQuery
|
633
|
* The called object.
|
634
|
*/
|
635
|
public function entityCondition($name, $value, $operator = NULL) {
|
636
|
// The '!=' operator is deprecated in favour of the '<>' operator since the
|
637
|
// latter is ANSI SQL compatible.
|
638
|
if ($operator == '!=') {
|
639
|
$operator = '<>';
|
640
|
}
|
641
|
$this->entityConditions[$name] = array(
|
642
|
'value' => $value,
|
643
|
'operator' => $operator,
|
644
|
);
|
645
|
return $this;
|
646
|
}
|
647
|
|
648
|
/**
|
649
|
* Adds a condition on field values.
|
650
|
*
|
651
|
* Note that entities with empty field values will be excluded from the
|
652
|
* EntityFieldQuery results when using this method.
|
653
|
*
|
654
|
* @param $field
|
655
|
* Either a field name or a field array.
|
656
|
* @param $column
|
657
|
* The column that should hold the value to be matched.
|
658
|
* @param $value
|
659
|
* The value to test the column value against.
|
660
|
* @param $operator
|
661
|
* The operator to be used to test the given value.
|
662
|
* @param $delta_group
|
663
|
* An arbitrary identifier: conditions in the same group must have the same
|
664
|
* $delta_group.
|
665
|
* @param $language_group
|
666
|
* An arbitrary identifier: conditions in the same group must have the same
|
667
|
* $language_group.
|
668
|
*
|
669
|
* @return EntityFieldQuery
|
670
|
* The called object.
|
671
|
*
|
672
|
* @see EntityFieldQuery::addFieldCondition
|
673
|
* @see EntityFieldQuery::deleted
|
674
|
*/
|
675
|
public function fieldCondition($field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
|
676
|
return $this->addFieldCondition($this->fieldConditions, $field, $column, $value, $operator, $delta_group, $language_group);
|
677
|
}
|
678
|
|
679
|
/**
|
680
|
* Adds a condition on the field language column.
|
681
|
*
|
682
|
* @param $field
|
683
|
* Either a field name or a field array.
|
684
|
* @param $value
|
685
|
* The value to test the column value against.
|
686
|
* @param $operator
|
687
|
* The operator to be used to test the given value.
|
688
|
* @param $delta_group
|
689
|
* An arbitrary identifier: conditions in the same group must have the same
|
690
|
* $delta_group.
|
691
|
* @param $language_group
|
692
|
* An arbitrary identifier: conditions in the same group must have the same
|
693
|
* $language_group.
|
694
|
*
|
695
|
* @return EntityFieldQuery
|
696
|
* The called object.
|
697
|
*
|
698
|
* @see EntityFieldQuery::addFieldCondition
|
699
|
* @see EntityFieldQuery::deleted
|
700
|
*/
|
701
|
public function fieldLanguageCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
|
702
|
return $this->addFieldCondition($this->fieldMetaConditions, $field, 'language', $value, $operator, $delta_group, $language_group);
|
703
|
}
|
704
|
|
705
|
/**
|
706
|
* Adds a condition on the field delta column.
|
707
|
*
|
708
|
* @param $field
|
709
|
* Either a field name or a field array.
|
710
|
* @param $value
|
711
|
* The value to test the column value against.
|
712
|
* @param $operator
|
713
|
* The operator to be used to test the given value.
|
714
|
* @param $delta_group
|
715
|
* An arbitrary identifier: conditions in the same group must have the same
|
716
|
* $delta_group.
|
717
|
* @param $language_group
|
718
|
* An arbitrary identifier: conditions in the same group must have the same
|
719
|
* $language_group.
|
720
|
*
|
721
|
* @return EntityFieldQuery
|
722
|
* The called object.
|
723
|
*
|
724
|
* @see EntityFieldQuery::addFieldCondition
|
725
|
* @see EntityFieldQuery::deleted
|
726
|
*/
|
727
|
public function fieldDeltaCondition($field, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
|
728
|
return $this->addFieldCondition($this->fieldMetaConditions, $field, 'delta', $value, $operator, $delta_group, $language_group);
|
729
|
}
|
730
|
|
731
|
/**
|
732
|
* Adds the given condition to the proper condition array.
|
733
|
*
|
734
|
* @param $conditions
|
735
|
* A reference to an array of conditions.
|
736
|
* @param $field
|
737
|
* Either a field name or a field array.
|
738
|
* @param $column
|
739
|
* A column defined in the hook_field_schema() of this field. If this is
|
740
|
* omitted then the query will find only entities that have data in this
|
741
|
* field, using the entity and property conditions if there are any.
|
742
|
* @param $value
|
743
|
* The value to test the column value against. In most cases, this is a
|
744
|
* scalar. For more complex options, it is an array. The meaning of each
|
745
|
* element in the array is dependent on $operator.
|
746
|
* @param $operator
|
747
|
* Possible values:
|
748
|
* - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
|
749
|
* operators expect $value to be a literal of the same type as the
|
750
|
* column.
|
751
|
* - 'IN', 'NOT IN': These operators expect $value to be an array of
|
752
|
* literals of the same type as the column.
|
753
|
* - 'BETWEEN': This operator expects $value to be an array of two literals
|
754
|
* of the same type as the column.
|
755
|
* The operator can be omitted, and will default to 'IN' if the value is an
|
756
|
* array, or to '=' otherwise.
|
757
|
* @param $delta_group
|
758
|
* An arbitrary identifier: conditions in the same group must have the same
|
759
|
* $delta_group. For example, let's presume a multivalue field which has
|
760
|
* two columns, 'color' and 'shape', and for entity id 1, there are two
|
761
|
* values: red/square and blue/circle. Entity ID 1 does not have values
|
762
|
* corresponding to 'red circle', however if you pass 'red' and 'circle' as
|
763
|
* conditions, it will appear in the results - by default queries will run
|
764
|
* against any combination of deltas. By passing the conditions with the
|
765
|
* same $delta_group it will ensure that only values attached to the same
|
766
|
* delta are matched, and entity 1 would then be excluded from the results.
|
767
|
* @param $language_group
|
768
|
* An arbitrary identifier: conditions in the same group must have the same
|
769
|
* $language_group.
|
770
|
*
|
771
|
* @return EntityFieldQuery
|
772
|
* The called object.
|
773
|
*/
|
774
|
protected function addFieldCondition(&$conditions, $field, $column = NULL, $value = NULL, $operator = NULL, $delta_group = NULL, $language_group = NULL) {
|
775
|
// The '!=' operator is deprecated in favour of the '<>' operator since the
|
776
|
// latter is ANSI SQL compatible.
|
777
|
if ($operator == '!=') {
|
778
|
$operator = '<>';
|
779
|
}
|
780
|
if (is_scalar($field)) {
|
781
|
$field_definition = field_info_field($field);
|
782
|
if (empty($field_definition)) {
|
783
|
throw new EntityFieldQueryException(t('Unknown field: @field_name', array('@field_name' => $field)));
|
784
|
}
|
785
|
$field = $field_definition;
|
786
|
}
|
787
|
// Ensure the same index is used for field conditions as for fields.
|
788
|
$index = count($this->fields);
|
789
|
$this->fields[$index] = $field;
|
790
|
if (isset($column)) {
|
791
|
$conditions[$index] = array(
|
792
|
'field' => $field,
|
793
|
'column' => $column,
|
794
|
'value' => $value,
|
795
|
'operator' => $operator,
|
796
|
'delta_group' => $delta_group,
|
797
|
'language_group' => $language_group,
|
798
|
);
|
799
|
}
|
800
|
return $this;
|
801
|
}
|
802
|
|
803
|
/**
|
804
|
* Adds a condition on an entity-specific property.
|
805
|
*
|
806
|
* An $entity_type must be specified by calling
|
807
|
* EntityFieldCondition::entityCondition('entity_type', $entity_type) before
|
808
|
* executing the query. Also, by default only entities stored in SQL are
|
809
|
* supported; however, EntityFieldQuery::executeCallback can be set to handle
|
810
|
* different entity storage.
|
811
|
*
|
812
|
* @param $column
|
813
|
* A column defined in the hook_schema() of the base table of the entity.
|
814
|
* @param $value
|
815
|
* The value to test the field against. In most cases, this is a scalar. For
|
816
|
* more complex options, it is an array. The meaning of each element in the
|
817
|
* array is dependent on $operator.
|
818
|
* @param $operator
|
819
|
* Possible values:
|
820
|
* - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS': These
|
821
|
* operators expect $value to be a literal of the same type as the
|
822
|
* column.
|
823
|
* - 'IN', 'NOT IN': These operators expect $value to be an array of
|
824
|
* literals of the same type as the column.
|
825
|
* - 'BETWEEN': This operator expects $value to be an array of two literals
|
826
|
* of the same type as the column.
|
827
|
* The operator can be omitted, and will default to 'IN' if the value is an
|
828
|
* array, or to '=' otherwise.
|
829
|
*
|
830
|
* @return EntityFieldQuery
|
831
|
* The called object.
|
832
|
*/
|
833
|
public function propertyCondition($column, $value, $operator = NULL) {
|
834
|
// The '!=' operator is deprecated in favour of the '<>' operator since the
|
835
|
// latter is ANSI SQL compatible.
|
836
|
if ($operator == '!=') {
|
837
|
$operator = '<>';
|
838
|
}
|
839
|
$this->propertyConditions[] = array(
|
840
|
'column' => $column,
|
841
|
'value' => $value,
|
842
|
'operator' => $operator,
|
843
|
);
|
844
|
return $this;
|
845
|
}
|
846
|
|
847
|
/**
|
848
|
* Orders the result set by entity-generic metadata.
|
849
|
*
|
850
|
* If called multiple times, the query will order by each specified column in
|
851
|
* the order this method is called.
|
852
|
*
|
853
|
* Note: The "comment" and "taxonomy_term" entity types don't support ordering
|
854
|
* by bundle. For "taxonomy_term", propertyOrderBy('vid') can be used instead.
|
855
|
*
|
856
|
* @param $name
|
857
|
* 'entity_type', 'bundle', 'revision_id' or 'entity_id'.
|
858
|
* @param $direction
|
859
|
* The direction to sort. Legal values are "ASC" and "DESC".
|
860
|
*
|
861
|
* @return EntityFieldQuery
|
862
|
* The called object.
|
863
|
*/
|
864
|
public function entityOrderBy($name, $direction = 'ASC') {
|
865
|
$this->order[] = array(
|
866
|
'type' => 'entity',
|
867
|
'specifier' => $name,
|
868
|
'direction' => $direction,
|
869
|
);
|
870
|
return $this;
|
871
|
}
|
872
|
|
873
|
/**
|
874
|
* Orders the result set by a given field column.
|
875
|
*
|
876
|
* If called multiple times, the query will order by each specified column in
|
877
|
* the order this method is called. Note that entities with empty field
|
878
|
* values will be excluded from the EntityFieldQuery results when using this
|
879
|
* method.
|
880
|
*
|
881
|
* @param $field
|
882
|
* Either a field name or a field array.
|
883
|
* @param $column
|
884
|
* A column defined in the hook_field_schema() of this field. entity_id and
|
885
|
* bundle can also be used.
|
886
|
* @param $direction
|
887
|
* The direction to sort. Legal values are "ASC" and "DESC".
|
888
|
*
|
889
|
* @return EntityFieldQuery
|
890
|
* The called object.
|
891
|
*/
|
892
|
public function fieldOrderBy($field, $column, $direction = 'ASC') {
|
893
|
if (is_scalar($field)) {
|
894
|
$field_definition = field_info_field($field);
|
895
|
if (empty($field_definition)) {
|
896
|
throw new EntityFieldQueryException(t('Unknown field: @field_name', array('@field_name' => $field)));
|
897
|
}
|
898
|
$field = $field_definition;
|
899
|
}
|
900
|
// Save the index used for the new field, for later use in field storage.
|
901
|
$index = count($this->fields);
|
902
|
$this->fields[$index] = $field;
|
903
|
$this->order[] = array(
|
904
|
'type' => 'field',
|
905
|
'specifier' => array(
|
906
|
'field' => $field,
|
907
|
'index' => $index,
|
908
|
'column' => $column,
|
909
|
),
|
910
|
'direction' => $direction,
|
911
|
);
|
912
|
return $this;
|
913
|
}
|
914
|
|
915
|
/**
|
916
|
* Orders the result set by an entity-specific property.
|
917
|
*
|
918
|
* An $entity_type must be specified by calling
|
919
|
* EntityFieldCondition::entityCondition('entity_type', $entity_type) before
|
920
|
* executing the query.
|
921
|
*
|
922
|
* If called multiple times, the query will order by each specified column in
|
923
|
* the order this method is called.
|
924
|
*
|
925
|
* @param $column
|
926
|
* The column on which to order.
|
927
|
* @param $direction
|
928
|
* The direction to sort. Legal values are "ASC" and "DESC".
|
929
|
*
|
930
|
* @return EntityFieldQuery
|
931
|
* The called object.
|
932
|
*/
|
933
|
public function propertyOrderBy($column, $direction = 'ASC') {
|
934
|
$this->order[] = array(
|
935
|
'type' => 'property',
|
936
|
'specifier' => $column,
|
937
|
'direction' => $direction,
|
938
|
);
|
939
|
return $this;
|
940
|
}
|
941
|
|
942
|
/**
|
943
|
* Sets the query to be a count query only.
|
944
|
*
|
945
|
* @return EntityFieldQuery
|
946
|
* The called object.
|
947
|
*/
|
948
|
public function count() {
|
949
|
$this->count = TRUE;
|
950
|
return $this;
|
951
|
}
|
952
|
|
953
|
/**
|
954
|
* Restricts a query to a given range in the result set.
|
955
|
*
|
956
|
* @param $start
|
957
|
* The first entity from the result set to return. If NULL, removes any
|
958
|
* range directives that are set.
|
959
|
* @param $length
|
960
|
* The number of entities to return from the result set.
|
961
|
*
|
962
|
* @return EntityFieldQuery
|
963
|
* The called object.
|
964
|
*/
|
965
|
public function range($start = NULL, $length = NULL) {
|
966
|
$this->range = array(
|
967
|
'start' => $start,
|
968
|
'length' => $length,
|
969
|
);
|
970
|
return $this;
|
971
|
}
|
972
|
|
973
|
/**
|
974
|
* Enables a pager for the query.
|
975
|
*
|
976
|
* @param $limit
|
977
|
* An integer specifying the number of elements per page. If passed a false
|
978
|
* value (FALSE, 0, NULL), the pager is disabled.
|
979
|
* @param $element
|
980
|
* An optional integer to distinguish between multiple pagers on one page.
|
981
|
* If not provided, one is automatically calculated.
|
982
|
*
|
983
|
* @return EntityFieldQuery
|
984
|
* The called object.
|
985
|
*/
|
986
|
public function pager($limit = 10, $element = NULL) {
|
987
|
if (!isset($element)) {
|
988
|
$element = PagerDefault::$maxElement++;
|
989
|
}
|
990
|
elseif ($element >= PagerDefault::$maxElement) {
|
991
|
PagerDefault::$maxElement = $element + 1;
|
992
|
}
|
993
|
|
994
|
$this->pager = array(
|
995
|
'limit' => $limit,
|
996
|
'element' => $element,
|
997
|
);
|
998
|
return $this;
|
999
|
}
|
1000
|
|
1001
|
/**
|
1002
|
* Enables sortable tables for this query.
|
1003
|
*
|
1004
|
* @param $headers
|
1005
|
* An EFQ Header array based on which the order clause is added to the
|
1006
|
* query.
|
1007
|
*
|
1008
|
* @return EntityFieldQuery
|
1009
|
* The called object.
|
1010
|
*/
|
1011
|
public function tableSort(&$headers) {
|
1012
|
// If 'field' is not initialized, the header columns aren't clickable
|
1013
|
foreach ($headers as $key =>$header) {
|
1014
|
if (is_array($header) && isset($header['specifier'])) {
|
1015
|
$headers[$key]['field'] = '';
|
1016
|
}
|
1017
|
}
|
1018
|
|
1019
|
$order = tablesort_get_order($headers);
|
1020
|
$direction = tablesort_get_sort($headers);
|
1021
|
foreach ($headers as $header) {
|
1022
|
if (is_array($header) && ($header['data'] == $order['name'])) {
|
1023
|
if ($header['type'] == 'field') {
|
1024
|
$this->fieldOrderBy($header['specifier']['field'], $header['specifier']['column'], $direction);
|
1025
|
}
|
1026
|
else {
|
1027
|
$header['direction'] = $direction;
|
1028
|
$this->order[] = $header;
|
1029
|
}
|
1030
|
}
|
1031
|
}
|
1032
|
|
1033
|
return $this;
|
1034
|
}
|
1035
|
|
1036
|
/**
|
1037
|
* Filters on the data being deleted.
|
1038
|
*
|
1039
|
* @param $deleted
|
1040
|
* TRUE to only return deleted data, FALSE to return non-deleted data,
|
1041
|
* EntityFieldQuery::RETURN_ALL to return everything. Defaults to FALSE.
|
1042
|
*
|
1043
|
* @return EntityFieldQuery
|
1044
|
* The called object.
|
1045
|
*/
|
1046
|
public function deleted($deleted = TRUE) {
|
1047
|
$this->deleted = $deleted;
|
1048
|
return $this;
|
1049
|
}
|
1050
|
|
1051
|
/**
|
1052
|
* Queries the current or every revision.
|
1053
|
*
|
1054
|
* Note that this only affects field conditions. Property conditions always
|
1055
|
* apply to the current revision.
|
1056
|
* @TODO: Once revision tables have been cleaned up, revisit this.
|
1057
|
*
|
1058
|
* @param $age
|
1059
|
* - FIELD_LOAD_CURRENT (default): Query the most recent revisions for all
|
1060
|
* entities. The results will be keyed by entity type and entity ID.
|
1061
|
* - FIELD_LOAD_REVISION: Query all revisions. The results will be keyed by
|
1062
|
* entity type and entity revision ID.
|
1063
|
*
|
1064
|
* @return EntityFieldQuery
|
1065
|
* The called object.
|
1066
|
*/
|
1067
|
public function age($age) {
|
1068
|
$this->age = $age;
|
1069
|
return $this;
|
1070
|
}
|
1071
|
|
1072
|
/**
|
1073
|
* Adds a tag to the query.
|
1074
|
*
|
1075
|
* Tags are strings that mark a query so that hook_query_alter() and
|
1076
|
* hook_query_TAG_alter() implementations may decide if they wish to alter
|
1077
|
* the query. A query may have any number of tags, and they must be valid PHP
|
1078
|
* identifiers (composed of letters, numbers, and underscores). For example,
|
1079
|
* queries involving nodes that will be displayed for a user need to add the
|
1080
|
* tag 'node_access', so that the node module can add access restrictions to
|
1081
|
* the query.
|
1082
|
*
|
1083
|
* If an entity field query has tags, it must also have an entity type
|
1084
|
* specified, because the alter hook will need the entity base table.
|
1085
|
*
|
1086
|
* @param string $tag
|
1087
|
* The tag to add.
|
1088
|
*
|
1089
|
* @return EntityFieldQuery
|
1090
|
* The called object.
|
1091
|
*/
|
1092
|
public function addTag($tag) {
|
1093
|
$this->tags[$tag] = $tag;
|
1094
|
return $this;
|
1095
|
}
|
1096
|
|
1097
|
/**
|
1098
|
* Adds additional metadata to the query.
|
1099
|
*
|
1100
|
* Sometimes a query may need to provide additional contextual data for the
|
1101
|
* alter hook. The alter hook implementations may then use that information
|
1102
|
* to decide if and how to take action.
|
1103
|
*
|
1104
|
* @param $key
|
1105
|
* The unique identifier for this piece of metadata. Must be a string that
|
1106
|
* follows the same rules as any other PHP identifier.
|
1107
|
* @param $object
|
1108
|
* The additional data to add to the query. May be any valid PHP variable.
|
1109
|
*
|
1110
|
* @return EntityFieldQuery
|
1111
|
* The called object.
|
1112
|
*/
|
1113
|
public function addMetaData($key, $object) {
|
1114
|
$this->metaData[$key] = $object;
|
1115
|
return $this;
|
1116
|
}
|
1117
|
|
1118
|
/**
|
1119
|
* Executes the query.
|
1120
|
*
|
1121
|
* After executing the query, $this->orderedResults will contain a list of
|
1122
|
* the same stub entities in the order returned by the query. This is only
|
1123
|
* relevant if there are multiple entity types in the returned value and
|
1124
|
* a field ordering was requested. In every other case, the returned value
|
1125
|
* contains everything necessary for processing.
|
1126
|
*
|
1127
|
* @return
|
1128
|
* Either a number if count() was called or an array of associative arrays
|
1129
|
* of stub entities. The outer array keys are entity types, and the inner
|
1130
|
* array keys are the relevant ID. (In most cases this will be the entity
|
1131
|
* ID. The only exception is when age=FIELD_LOAD_REVISION is used and field
|
1132
|
* conditions or sorts are present -- in this case, the key will be the
|
1133
|
* revision ID.) The entity type will only exist in the outer array if
|
1134
|
* results were found. The inner array values are always stub entities, as
|
1135
|
* returned by entity_create_stub_entity(). To traverse the returned array:
|
1136
|
* @code
|
1137
|
* foreach ($query->execute() as $entity_type => $entities) {
|
1138
|
* foreach ($entities as $entity_id => $entity) {
|
1139
|
* @endcode
|
1140
|
* Note if the entity type is known, then the following snippet will load
|
1141
|
* the entities found:
|
1142
|
* @code
|
1143
|
* $result = $query->execute();
|
1144
|
* if (!empty($result[$my_type])) {
|
1145
|
* $entities = entity_load($my_type, array_keys($result[$my_type]));
|
1146
|
* }
|
1147
|
* @endcode
|
1148
|
*/
|
1149
|
public function execute() {
|
1150
|
// Give a chance to other modules to alter the query.
|
1151
|
drupal_alter('entity_query', $this);
|
1152
|
$this->altered = TRUE;
|
1153
|
|
1154
|
// Initialize the pager.
|
1155
|
$this->initializePager();
|
1156
|
|
1157
|
// Execute the query using the correct callback.
|
1158
|
$result = call_user_func($this->queryCallback(), $this);
|
1159
|
|
1160
|
return $result;
|
1161
|
}
|
1162
|
|
1163
|
/**
|
1164
|
* Determines the query callback to use for this entity query.
|
1165
|
*
|
1166
|
* @return
|
1167
|
* A callback that can be used with call_user_func().
|
1168
|
*/
|
1169
|
public function queryCallback() {
|
1170
|
// Use the override from $this->executeCallback. It can be set either
|
1171
|
// while building the query, or using hook_entity_query_alter().
|
1172
|
if (function_exists($this->executeCallback)) {
|
1173
|
return $this->executeCallback;
|
1174
|
}
|
1175
|
// If there are no field conditions and sorts, and no execute callback
|
1176
|
// then we default to querying entity tables in SQL.
|
1177
|
if (empty($this->fields)) {
|
1178
|
return array($this, 'propertyQuery');
|
1179
|
}
|
1180
|
// If no override, find the storage engine to be used.
|
1181
|
foreach ($this->fields as $field) {
|
1182
|
if (!isset($storage)) {
|
1183
|
$storage = $field['storage']['module'];
|
1184
|
}
|
1185
|
elseif ($storage != $field['storage']['module']) {
|
1186
|
throw new EntityFieldQueryException(t("Can't handle more than one field storage engine"));
|
1187
|
}
|
1188
|
}
|
1189
|
if ($storage) {
|
1190
|
// Use hook_field_storage_query() from the field storage.
|
1191
|
return $storage . '_field_storage_query';
|
1192
|
}
|
1193
|
else {
|
1194
|
throw new EntityFieldQueryException(t("Field storage engine not found."));
|
1195
|
}
|
1196
|
}
|
1197
|
|
1198
|
/**
|
1199
|
* Queries entity tables in SQL for property conditions and sorts.
|
1200
|
*
|
1201
|
* This method is only used if there are no field conditions and sorts.
|
1202
|
*
|
1203
|
* @return
|
1204
|
* See EntityFieldQuery::execute().
|
1205
|
*/
|
1206
|
protected function propertyQuery() {
|
1207
|
if (empty($this->entityConditions['entity_type'])) {
|
1208
|
throw new EntityFieldQueryException(t('For this query an entity type must be specified.'));
|
1209
|
}
|
1210
|
$entity_type = $this->entityConditions['entity_type']['value'];
|
1211
|
$entity_info = entity_get_info($entity_type);
|
1212
|
if (empty($entity_info['base table'])) {
|
1213
|
throw new EntityFieldQueryException(t('Entity %entity has no base table.', array('%entity' => $entity_type)));
|
1214
|
}
|
1215
|
$base_table = $entity_info['base table'];
|
1216
|
$base_table_schema = drupal_get_schema($base_table);
|
1217
|
$select_query = db_select($base_table);
|
1218
|
$select_query->addExpression(':entity_type', 'entity_type', array(':entity_type' => $entity_type));
|
1219
|
// Process the property conditions.
|
1220
|
foreach ($this->propertyConditions as $property_condition) {
|
1221
|
$this->addCondition($select_query, $base_table . '.' . $property_condition['column'], $property_condition);
|
1222
|
}
|
1223
|
// Process the four possible entity condition.
|
1224
|
// The id field is always present in entity keys.
|
1225
|
$sql_field = $entity_info['entity keys']['id'];
|
1226
|
$id_map['entity_id'] = $sql_field;
|
1227
|
$select_query->addField($base_table, $sql_field, 'entity_id');
|
1228
|
if (isset($this->entityConditions['entity_id'])) {
|
1229
|
$this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['entity_id']);
|
1230
|
}
|
1231
|
|
1232
|
// If there is a revision key defined, use it.
|
1233
|
if (!empty($entity_info['entity keys']['revision'])) {
|
1234
|
$sql_field = $entity_info['entity keys']['revision'];
|
1235
|
$select_query->addField($base_table, $sql_field, 'revision_id');
|
1236
|
if (isset($this->entityConditions['revision_id'])) {
|
1237
|
$this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['revision_id']);
|
1238
|
}
|
1239
|
}
|
1240
|
else {
|
1241
|
$sql_field = 'revision_id';
|
1242
|
$select_query->addExpression('NULL', 'revision_id');
|
1243
|
}
|
1244
|
$id_map['revision_id'] = $sql_field;
|
1245
|
|
1246
|
// Handle bundles.
|
1247
|
if (!empty($entity_info['entity keys']['bundle'])) {
|
1248
|
$sql_field = $entity_info['entity keys']['bundle'];
|
1249
|
$having = FALSE;
|
1250
|
|
1251
|
if (!empty($base_table_schema['fields'][$sql_field])) {
|
1252
|
$select_query->addField($base_table, $sql_field, 'bundle');
|
1253
|
}
|
1254
|
}
|
1255
|
else {
|
1256
|
$sql_field = 'bundle';
|
1257
|
$select_query->addExpression(':bundle', 'bundle', array(':bundle' => $entity_type));
|
1258
|
$having = TRUE;
|
1259
|
}
|
1260
|
$id_map['bundle'] = $sql_field;
|
1261
|
if (isset($this->entityConditions['bundle'])) {
|
1262
|
if (!empty($entity_info['entity keys']['bundle'])) {
|
1263
|
$this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['bundle'], $having);
|
1264
|
}
|
1265
|
else {
|
1266
|
// This entity has no bundle, so invalidate the query.
|
1267
|
$select_query->where('1 = 0');
|
1268
|
}
|
1269
|
}
|
1270
|
|
1271
|
// Order the query.
|
1272
|
foreach ($this->order as $order) {
|
1273
|
if ($order['type'] == 'entity') {
|
1274
|
$key = $order['specifier'];
|
1275
|
if (!isset($id_map[$key])) {
|
1276
|
throw new EntityFieldQueryException(t('Do not know how to order on @key for @entity_type', array('@key' => $key, '@entity_type' => $entity_type)));
|
1277
|
}
|
1278
|
$select_query->orderBy($id_map[$key], $order['direction']);
|
1279
|
}
|
1280
|
elseif ($order['type'] == 'property') {
|
1281
|
$select_query->orderBy($base_table . '.' . $order['specifier'], $order['direction']);
|
1282
|
}
|
1283
|
}
|
1284
|
|
1285
|
return $this->finishQuery($select_query);
|
1286
|
}
|
1287
|
|
1288
|
/**
|
1289
|
* Gets the total number of results and initializes a pager for the query.
|
1290
|
*
|
1291
|
* The pager can be disabled by either setting the pager limit to 0, or by
|
1292
|
* setting this query to be a count query.
|
1293
|
*/
|
1294
|
function initializePager() {
|
1295
|
if ($this->pager && !empty($this->pager['limit']) && !$this->count) {
|
1296
|
$page = pager_find_page($this->pager['element']);
|
1297
|
$count_query = clone $this;
|
1298
|
$this->pager['total'] = $count_query->count()->execute();
|
1299
|
$this->pager['start'] = $page * $this->pager['limit'];
|
1300
|
pager_default_initialize($this->pager['total'], $this->pager['limit'], $this->pager['element']);
|
1301
|
$this->range($this->pager['start'], $this->pager['limit']);
|
1302
|
}
|
1303
|
}
|
1304
|
|
1305
|
/**
|
1306
|
* Finishes the query.
|
1307
|
*
|
1308
|
* Adds tags, metaData, range and returns the requested list or count.
|
1309
|
*
|
1310
|
* @param SelectQuery $select_query
|
1311
|
* A SelectQuery which has entity_type, entity_id, revision_id and bundle
|
1312
|
* fields added.
|
1313
|
* @param $id_key
|
1314
|
* Which field's values to use as the returned array keys.
|
1315
|
*
|
1316
|
* @return
|
1317
|
* See EntityFieldQuery::execute().
|
1318
|
*/
|
1319
|
function finishQuery($select_query, $id_key = 'entity_id') {
|
1320
|
foreach ($this->tags as $tag) {
|
1321
|
$select_query->addTag($tag);
|
1322
|
}
|
1323
|
foreach ($this->metaData as $key => $object) {
|
1324
|
$select_query->addMetaData($key, $object);
|
1325
|
}
|
1326
|
$select_query->addMetaData('entity_field_query', $this);
|
1327
|
if ($this->range) {
|
1328
|
$select_query->range($this->range['start'], $this->range['length']);
|
1329
|
}
|
1330
|
if ($this->count) {
|
1331
|
return $select_query->countQuery()->execute()->fetchField();
|
1332
|
}
|
1333
|
$return = array();
|
1334
|
foreach ($select_query->execute() as $partial_entity) {
|
1335
|
$bundle = isset($partial_entity->bundle) ? $partial_entity->bundle : NULL;
|
1336
|
$entity = entity_create_stub_entity($partial_entity->entity_type, array($partial_entity->entity_id, $partial_entity->revision_id, $bundle));
|
1337
|
$return[$partial_entity->entity_type][$partial_entity->$id_key] = $entity;
|
1338
|
$this->ordered_results[] = $partial_entity;
|
1339
|
}
|
1340
|
return $return;
|
1341
|
}
|
1342
|
|
1343
|
/**
|
1344
|
* Adds a condition to an already built SelectQuery (internal function).
|
1345
|
*
|
1346
|
* This is a helper for hook_entity_query() and hook_field_storage_query().
|
1347
|
*
|
1348
|
* @param SelectQuery $select_query
|
1349
|
* A SelectQuery object.
|
1350
|
* @param $sql_field
|
1351
|
* The name of the field.
|
1352
|
* @param $condition
|
1353
|
* A condition as described in EntityFieldQuery::fieldCondition() and
|
1354
|
* EntityFieldQuery::entityCondition().
|
1355
|
* @param $having
|
1356
|
* HAVING or WHERE. This is necessary because SQL can't handle WHERE
|
1357
|
* conditions on aliased columns.
|
1358
|
*/
|
1359
|
public function addCondition(SelectQuery $select_query, $sql_field, $condition, $having = FALSE) {
|
1360
|
$method = $having ? 'havingCondition' : 'condition';
|
1361
|
$like_prefix = '';
|
1362
|
switch ($condition['operator']) {
|
1363
|
case 'CONTAINS':
|
1364
|
$like_prefix = '%';
|
1365
|
case 'STARTS_WITH':
|
1366
|
$select_query->$method($sql_field, $like_prefix . db_like($condition['value']) . '%', 'LIKE');
|
1367
|
break;
|
1368
|
default:
|
1369
|
$select_query->$method($sql_field, $condition['value'], $condition['operator']);
|
1370
|
}
|
1371
|
}
|
1372
|
|
1373
|
}
|
1374
|
|
1375
|
/**
|
1376
|
* Defines an exception thrown when a malformed entity is passed.
|
1377
|
*/
|
1378
|
class EntityMalformedException extends Exception { }
|