5 * Plugin to provide an relationship handler for an entity from a field.
9 * Plugins are described by creating a $plugin array which will be used
10 * by the system that includes this file.
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',
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];
25 function ctools_entity_from_schema_get_children($parent_plugin, $parent) {
26 $entities = entity_get_info();
29 foreach (module_implements('entity_info') as $module) {
30 module_load_install($module);
31 $schemas = drupal_get_schema();
33 foreach ($entities as $from_entity => $from_entity_info) {
34 if (empty($from_entity_info['base table'])) {
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'])) {
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']);
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'],
57 $name = $this_col . '-' . $from_entity . '-' . $to_entity;
58 $plugin_id = $parent . ':' . $name;
59 $plugin = $parent_plugin;
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);
67 $plugin['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity);
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));
72 drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
73 $plugins[$plugin_id] = $plugin;
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;
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);
86 $plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity);
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;
99 drupal_alter('ctools_entity_contexts', $plugins);
103 function ctools_entity_from_schema_columns_filter($columns, $value) {
104 foreach ($columns as $this_col => $that_col) {
105 if ($value == $that_col) {
112 * Return a new context based on an existing context.
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);
124 if (isset($context->data->{$entity_info['entity keys']['id']})) {
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;
132 // Send it to ctools.
133 return ctools_context_create('entity:' . $to_entity, $to_entity_id);