Mercurial > hg > rr-repo
comparison sites/all/modules/ctools/plugins/relationships/entity_from_schema.inc @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Plugin to provide an relationship handler for an entity from a field. | |
6 */ | |
7 | |
8 /** | |
9 * Plugins are described by creating a $plugin array which will be used | |
10 * by the system that includes this file. | |
11 */ | |
12 $plugin = array( | |
13 'title' => t('Entity'), | |
14 'description' => t('Creates an entity context from a foreign key on a field.'), | |
15 'context' => 'ctools_entity_from_schema_context', | |
16 'get child' => 'ctools_entity_from_schema_get_child', | |
17 'get children' => 'ctools_entity_from_schema_get_children', | |
18 ); | |
19 | |
20 function ctools_entity_from_schema_get_child($plugin, $parent, $child) { | |
21 $plugins = ctools_entity_from_schema_get_children($plugin, $parent); | |
22 return $plugins[$parent . ':' . $child]; | |
23 } | |
24 | |
25 function ctools_entity_from_schema_get_children($parent_plugin, $parent) { | |
26 $entities = entity_get_info(); | |
27 $plugins = array(); | |
28 | |
29 foreach (module_implements('entity_info') as $module) { | |
30 module_load_install($module); | |
31 $schemas = drupal_get_schema(); | |
32 | |
33 foreach ($entities as $from_entity => $from_entity_info) { | |
34 if (empty($from_entity_info['base table'])) { | |
35 continue; | |
36 } | |
37 | |
38 $table = $from_entity_info['base table']; | |
39 if (isset($schemas[$table]['foreign keys'])) { | |
40 foreach ($schemas[$table]['foreign keys'] as $relationship => $info) { | |
41 foreach ($entities as $to_entity => $to_entity_info) { | |
42 if (empty($info['table'])) { | |
43 continue; | |
44 } | |
45 | |
46 if (isset($to_entity_info['base table']) && $info['table'] == $to_entity_info['base table'] && in_array($to_entity_info['entity keys']['id'], $info['columns'])) { | |
47 $this_col = ctools_entity_from_schema_columns_filter($info['columns'], $to_entity_info['entity keys']['id']); | |
48 | |
49 // Set up our t() replacements as we reuse these. | |
50 $replacements = array( | |
51 '@relationship' => $relationship, | |
52 '@base_table' => $table, | |
53 '@to_entity' => $to_entity_info['label'], | |
54 '@from_entity' => $from_entity_info['label'], | |
55 ); | |
56 | |
57 $name = $this_col . '-' . $from_entity . '-' . $to_entity; | |
58 $plugin_id = $parent . ':' . $name; | |
59 $plugin = $parent_plugin; | |
60 | |
61 $plugin['title'] = t('@to_entity from @from_entity (on @base_table.@relationship)', $replacements); | |
62 $plugin['keyword'] = $to_entity; | |
63 $plugin['context name'] = $name; | |
64 $plugin['name'] = $plugin_id; | |
65 $plugin['description'] = t('Builds a relationship from a @from_entity to a @to_entity using the @base_table.@relationship field.', $replacements); | |
66 | |
67 $plugin['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity); | |
68 | |
69 $plugin_entities = array('to' => $to_entity_info, 'from' => $from_entity_info); | |
70 $plugin_entities = array('to' => array($to_entity => $to_entity_info), 'from' => array($from_entity => $from_entity_info)); | |
71 | |
72 drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id); | |
73 $plugins[$plugin_id] = $plugin; | |
74 | |
75 // Add the relation in the reverse direction. | |
76 $name = $this_col . '-' . $to_entity . '-' . $from_entity; | |
77 $plugin_id = $parent . ':' . $name; | |
78 $plugin = $parent_plugin; | |
79 | |
80 $plugin['title'] = t('@from_entity from @to_entity (on @base_table.@relationship)', $replacements); | |
81 $plugin['keyword'] = $to_entity; | |
82 $plugin['context name'] = $name; | |
83 $plugin['name'] = $plugin_id; | |
84 $plugin['description'] = t('Builds a relationship from a @to_entity to a @from_entity using the @base_table.@relationship field.', $replacements); | |
85 | |
86 $plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity); | |
87 | |
88 $plugin_entities = array('to' => $from_entity_info, 'from' => $to_entity_info); | |
89 $plugin_entities = array('to' => array($from_entity => $from_entity_info), 'from' => array($to_entity => $to_entity_info)); | |
90 drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id); | |
91 $plugins[$plugin_id] = $plugin; | |
92 | |
93 } | |
94 } | |
95 } | |
96 } | |
97 } | |
98 } | |
99 drupal_alter('ctools_entity_contexts', $plugins); | |
100 return $plugins; | |
101 } | |
102 | |
103 function ctools_entity_from_schema_columns_filter($columns, $value) { | |
104 foreach ($columns as $this_col => $that_col) { | |
105 if ($value == $that_col) { | |
106 return $this_col; | |
107 } | |
108 } | |
109 } | |
110 | |
111 /** | |
112 * Return a new context based on an existing context. | |
113 */ | |
114 function ctools_entity_from_schema_context($context, $conf) { | |
115 $plugin = $conf['name']; | |
116 list($plugin, $plugin_name) = explode(':', $plugin); | |
117 list($this_col, $from_entity, $to_entity) = explode('-', $plugin_name); | |
118 // If unset it wants a generic, unfilled context, which is just NULL. | |
119 $entity_info = entity_get_info($from_entity); | |
120 if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) { | |
121 return ctools_context_create_empty('entity:' . $to_entity, NULL); | |
122 } | |
123 | |
124 if (isset($context->data->{$entity_info['entity keys']['id']})) { | |
125 // Load the entity. | |
126 $id = $context->data->{$entity_info['entity keys']['id']}; | |
127 $entity = entity_load($from_entity, array($id)); | |
128 $entity = $entity[$id]; | |
129 if (isset($entity->$this_col)) { | |
130 $to_entity_id = $entity->$this_col; | |
131 | |
132 // Send it to ctools. | |
133 return ctools_context_create('entity:' . $to_entity, $to_entity_id); | |
134 } | |
135 } | |
136 } |