1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* Contains the entity_views_handler_relationship_by_bundle class.
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
/**
|
8 |
|
|
* Relationship handler for entity relationships that may limit the join to one or more bundles.
|
9 |
|
|
*
|
10 |
|
|
* This handler is only applicable for entities that are using bundle entities,
|
11 |
|
|
* i.e. entities having the 'bundle of' entity info key set.
|
12 |
|
|
*
|
13 |
|
|
* For example, this allows a relationship from users to profiles of a certain
|
14 |
|
|
* profile type.
|
15 |
|
|
*
|
16 |
|
|
* @see entity_crud_hook_entity_info()
|
17 |
|
|
* @ingroup views_field_handlers
|
18 |
|
|
*/
|
19 |
|
|
class entity_views_handler_relationship_by_bundle extends views_handler_relationship {
|
20 |
|
|
function option_definition() {
|
21 |
|
|
$options = parent::option_definition();
|
22 |
|
|
$options['bundle_types'] = array('default' => array());
|
23 |
|
|
|
24 |
|
|
return $options;
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* Add an entity type option.
|
29 |
|
|
*/
|
30 |
|
|
function options_form(&$form, &$form_state) {
|
31 |
|
|
parent::options_form($form, $form_state);
|
32 |
|
|
|
33 |
|
|
// Get the entity type and info from the table data for the base on the
|
34 |
|
|
// right hand side of the relationship join.
|
35 |
|
|
$table_data = views_fetch_data($this->definition['base']);
|
36 |
|
|
$entity_type = $table_data['table']['entity type'];
|
37 |
|
|
$entity_info = entity_get_info($entity_type);
|
38 |
|
|
|
39 |
|
|
// Get the info of the bundle entity.
|
40 |
|
|
foreach (entity_get_info() as $type => $info) {
|
41 |
|
|
if (isset($info['bundle of']) && $info['bundle of'] == $entity_type) {
|
42 |
|
|
$entity_bundle_info = $info;
|
43 |
|
|
break;
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
$plural_label = isset($entity_bundle_info['plural label']) ? $entity_bundle_info['plural label'] : $entity_bundle_info['label'] . 's';
|
48 |
|
|
$bundle_options = array();
|
49 |
|
|
foreach ($entity_info['bundles'] as $name => $info) {
|
50 |
|
|
$bundle_options[$name] = $info['label'];
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
$form['bundle_types'] = array(
|
54 |
|
|
'#title' => $plural_label,
|
55 |
|
|
'#type' => 'checkboxes',
|
56 |
|
|
'#description' => t('Restrict this relationship to one or more @bundles.', array('@bundles' => strtolower($entity_bundle_info['plural label']))),
|
57 |
|
|
'#options' => $bundle_options,
|
58 |
|
|
'#default_value' => $this->options['bundle_types'],
|
59 |
|
|
);
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* Make sure only checked bundle types are left.
|
64 |
|
|
*/
|
65 |
|
|
function options_submit(&$form, &$form_state) {
|
66 |
|
|
$form_state['values']['options']['bundle_types'] = array_filter($form_state['values']['options']['bundle_types']);
|
67 |
|
|
parent::options_submit($form, $form_state);
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Called to implement a relationship in a query.
|
72 |
|
|
*
|
73 |
|
|
* Mostly the same as the parent method, except we add an extra clause to
|
74 |
|
|
* the join.
|
75 |
|
|
*/
|
76 |
|
|
function query() {
|
77 |
|
|
$table_data = views_fetch_data($this->definition['base']);
|
78 |
|
|
$base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
|
79 |
|
|
$this->ensure_my_table();
|
80 |
|
|
|
81 |
|
|
$def = $this->definition;
|
82 |
|
|
$def['table'] = $this->definition['base'];
|
83 |
|
|
$def['field'] = $base_field;
|
84 |
|
|
$def['left_table'] = $this->table_alias;
|
85 |
|
|
$def['left_field'] = $this->field;
|
86 |
|
|
if (!empty($this->options['required'])) {
|
87 |
|
|
$def['type'] = 'INNER';
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
// Add an extra clause to the join if there are bundle types selected.
|
91 |
|
|
if ($this->options['bundle_types']) {
|
92 |
|
|
$entity_info = entity_get_info($table_data['table']['entity type']);
|
93 |
|
|
$def['extra'] = array(
|
94 |
|
|
array(
|
95 |
|
|
// The table and the IN operator are implicit.
|
96 |
7d7b5830
|
Assos Assos
|
'field' => $entity_info['entity keys']['bundle'],
|
97 |
85ad3d82
|
Assos Assos
|
'value' => $this->options['bundle_types'],
|
98 |
|
|
),
|
99 |
|
|
);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
|
103 |
|
|
$join = new $def['join_handler'];
|
104 |
|
|
}
|
105 |
|
|
else {
|
106 |
|
|
$join = new views_join();
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
$join->definition = $def;
|
110 |
|
|
$join->construct();
|
111 |
|
|
$join->adjusted = TRUE;
|
112 |
|
|
|
113 |
|
|
// Use a short alias for this.
|
114 |
|
|
$alias = $def['table'] . '_' . $this->table;
|
115 |
|
|
$this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
|
116 |
|
|
}
|
117 |
|
|
} |