1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide an argument handler for all entity IDs.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* CTools UUID entity context plugin definition.
|
10
|
*/
|
11
|
$plugin = array(
|
12
|
'title' => t("Entity: UUID"),
|
13
|
'description' => t('Creates an entity context from an entity UUID argument.'),
|
14
|
'context' => 'uuid_entity_uuid_context',
|
15
|
'get child' => 'uuid_entity_uuid_get_child',
|
16
|
'get children' => 'uuid_entity_uuid_get_children',
|
17
|
);
|
18
|
|
19
|
/**
|
20
|
* Fetches the "child" information for a given parent entity.
|
21
|
*
|
22
|
* @todo document me properly.
|
23
|
*
|
24
|
* @return array
|
25
|
* The children.
|
26
|
*/
|
27
|
function uuid_entity_uuid_get_child($plugin, $parent, $child) {
|
28
|
$plugins = uuid_entity_uuid_get_children($plugin, $parent);
|
29
|
return $plugins[$parent . ':' . $child];
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Fetches all children types for a given parent entity.
|
34
|
*
|
35
|
* @todo document me properly.
|
36
|
*
|
37
|
* @return array
|
38
|
* All the children.
|
39
|
*/
|
40
|
function uuid_entity_uuid_get_children($original_plugin, $parent) {
|
41
|
$entities = entity_get_info();
|
42
|
$plugins = array();
|
43
|
foreach ($entities as $entity_type => $entity) {
|
44
|
$plugin = $original_plugin;
|
45
|
$plugin['title'] = t('@entity: UUID', array('@entity' => $entity['label']));
|
46
|
$plugin['keyword'] = $entity_type;
|
47
|
$plugin['description'] = t('Creates @entity context from an UUID argument.', array('@entity' => $entity_type));
|
48
|
$plugin['name'] = $parent . ':' . $entity_type;
|
49
|
$plugin_id = $parent . ':' . $entity_type;
|
50
|
drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
|
51
|
$plugins[$plugin_id] = $plugin;
|
52
|
}
|
53
|
drupal_alter('ctools_entity_contexts', $plugins);
|
54
|
return $plugins;
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Discover if this argument gives us the entity we crave.
|
59
|
*/
|
60
|
function uuid_entity_uuid_context($arg = NULL, $conf = NULL, $empty = FALSE) {
|
61
|
$entity_type = explode(':', $conf['name']);
|
62
|
$entity_type = $entity_type[1];
|
63
|
// If unset it wants a generic, unfilled context.
|
64
|
if ($empty) {
|
65
|
return ctools_context_create_empty('entity:' . $entity_type);
|
66
|
}
|
67
|
|
68
|
// We can accept either an entity object or a pure id.
|
69
|
if (is_object($arg)) {
|
70
|
return ctools_context_create('entity:' . $entity_type, $arg);
|
71
|
}
|
72
|
|
73
|
if (!is_string($arg)) {
|
74
|
return FALSE;
|
75
|
}
|
76
|
$entity_ids = entity_get_id_by_uuid($entity_type, array($arg));
|
77
|
if (!isset($entity_ids[$arg])) {
|
78
|
return FALSE;
|
79
|
}
|
80
|
$entity_id = $entity_ids[$arg];
|
81
|
$entities = entity_load($entity_type, array($entity_id));
|
82
|
if (!$entities) {
|
83
|
return FALSE;
|
84
|
}
|
85
|
|
86
|
return ctools_context_create('entity:' . $entity_type, $entities[$entity_id]);
|
87
|
}
|