entity_from_schema.inc
5.44 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
<?php
/**
* @file
* Plugin to provide an relationship handler for an entity from a field.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t('Entity'),
'description' => t('Creates an entity context from a foreign key on a field.'),
'context' => 'ctools_entity_from_schema_context',
'get child' => 'ctools_entity_from_schema_get_child',
'get children' => 'ctools_entity_from_schema_get_children',
);
function ctools_entity_from_schema_get_child($plugin, $parent, $child) {
$plugins = ctools_entity_from_schema_get_children($plugin, $parent);
return $plugins[$parent . ':' . $child];
}
function ctools_entity_from_schema_get_children($parent_plugin, $parent) {
$entities = entity_get_info();
$plugins = array();
foreach (module_implements('entity_info') as $module) {
module_load_install($module);
$schemas = drupal_get_schema();
foreach ($entities as $from_entity => $from_entity_info) {
if (empty($from_entity_info['base table'])) {
continue;
}
$table = $from_entity_info['base table'];
if (isset($schemas[$table]['foreign keys'])) {
foreach ($schemas[$table]['foreign keys'] as $relationship => $info) {
foreach ($entities as $to_entity => $to_entity_info) {
if (empty($info['table'])) {
continue;
}
if (isset($to_entity_info['base table']) && $info['table'] == $to_entity_info['base table'] && in_array($to_entity_info['entity keys']['id'], $info['columns'])) {
$this_col = ctools_entity_from_schema_columns_filter($info['columns'], $to_entity_info['entity keys']['id']);
// Set up our t() replacements as we reuse these.
$replacements = array(
'@relationship' => $relationship,
'@base_table' => $table,
'@to_entity' => $to_entity_info['label'],
'@from_entity' => $from_entity_info['label'],
);
$name = $this_col . '-' . $from_entity . '-' . $to_entity;
$plugin_id = $parent . ':' . $name;
$plugin = $parent_plugin;
$plugin['title'] = t('@to_entity from @from_entity (on @base_table.@relationship)', $replacements);
$plugin['keyword'] = $to_entity;
$plugin['context name'] = $name;
$plugin['name'] = $plugin_id;
$plugin['description'] = t('Builds a relationship from a @from_entity to a @to_entity using the @base_table.@relationship field.', $replacements);
$plugin['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity);
$plugin_entities = array('to' => $to_entity_info, 'from' => $from_entity_info);
$plugin_entities = array('to' => array($to_entity => $to_entity_info), 'from' => array($from_entity => $from_entity_info));
drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
$plugins[$plugin_id] = $plugin;
// Add the relation in the reverse direction.
$name = $this_col . '-' . $to_entity . '-' . $from_entity;
$plugin_id = $parent . ':' . $name;
$plugin = $parent_plugin;
$plugin['title'] = t('@from_entity from @to_entity (on @base_table.@relationship)', $replacements);
$plugin['keyword'] = $to_entity;
$plugin['context name'] = $name;
$plugin['name'] = $plugin_id;
$plugin['description'] = t('Builds a relationship from a @to_entity to a @from_entity using the @base_table.@relationship field.', $replacements);
$plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity);
$plugin_entities = array('to' => $from_entity_info, 'from' => $to_entity_info);
$plugin_entities = array('to' => array($from_entity => $from_entity_info), 'from' => array($to_entity => $to_entity_info));
drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
$plugins[$plugin_id] = $plugin;
}
}
}
}
}
}
drupal_alter('ctools_entity_contexts', $plugins);
return $plugins;
}
function ctools_entity_from_schema_columns_filter($columns, $value) {
foreach ($columns as $this_col => $that_col) {
if ($value == $that_col) {
return $this_col;
}
}
}
/**
* Return a new context based on an existing context.
*/
function ctools_entity_from_schema_context($context, $conf) {
$plugin = $conf['name'];
list($plugin, $plugin_name) = explode(':', $plugin);
list($this_col, $from_entity, $to_entity) = explode('-', $plugin_name);
// If unset it wants a generic, unfilled context, which is just NULL.
$entity_info = entity_get_info($from_entity);
if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) {
return ctools_context_create_empty('entity:' . $to_entity, NULL);
}
if (isset($context->data->{$entity_info['entity keys']['id']})) {
// Load the entity.
$id = $context->data->{$entity_info['entity keys']['id']};
$entity = entity_load($from_entity, array($id));
$entity = $entity[$id];
if (isset($entity->$this_col)) {
$to_entity_id = $entity->$this_col;
// Send it to ctools.
return ctools_context_create('entity:' . $to_entity, $to_entity_id);
}
}
}