1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide access control based upon entity bundle.
|
6
|
*/
|
7
|
|
8
|
$plugin = array(
|
9
|
'title' => t("(Custom) Entity: Field Value"),
|
10
|
'description' => t('Control access by entity field value.'),
|
11
|
'callback' => 'ctools_entity_field_value_ctools_access_check',
|
12
|
'default' => array('type' => array()),
|
13
|
'settings form' => 'ctools_entity_field_value_ctools_access_settings',
|
14
|
'settings form submit' => 'ctools_entity_field_value_ctools_access_settings_submit',
|
15
|
'summary' => 'ctools_entity_field_value_ctools_access_summary',
|
16
|
'get child' => 'ctools_entity_field_value_ctools_access_get_child',
|
17
|
'get children' => 'ctools_entity_field_value_ctools_access_get_children',
|
18
|
);
|
19
|
function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) {
|
20
|
|
21
|
$plugins = &drupal_static(__FUNCTION__, array());
|
22
|
if (empty($plugins[$parent . ':' . $child])) {
|
23
|
list($entity_type, $bundle_type, $field_name) = explode(':', $child);
|
24
|
$plugins[$parent . ':' . $child] = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name);
|
25
|
}
|
26
|
|
27
|
return $plugins[$parent . ':' . $child];
|
28
|
}
|
29
|
|
30
|
function ctools_entity_field_value_ctools_access_get_children($plugin, $parent) {
|
31
|
$plugins = &drupal_static(__FUNCTION__, array());
|
32
|
if (!empty($plugins)) {
|
33
|
return $plugins;
|
34
|
}
|
35
|
$entities = entity_get_info();
|
36
|
foreach ($entities as $entity_type => $entity) {
|
37
|
foreach ($entity['bundles'] as $bundle_type => $bundle) {
|
38
|
foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) {
|
39
|
if (!isset($plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name])) {
|
40
|
$plugin = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field);
|
41
|
$plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name] = $plugin;
|
42
|
}
|
43
|
}
|
44
|
}
|
45
|
}
|
46
|
|
47
|
return $plugins;
|
48
|
}
|
49
|
|
50
|
function _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity = NULL, $bundle = NULL, $field = NULL) {
|
51
|
|
52
|
// check that the entity, bundle and field arrays have a value.
|
53
|
// If not, load theme using machine names.
|
54
|
if (empty($entity)) {
|
55
|
$entity = entity_get_info($entity_type);
|
56
|
}
|
57
|
|
58
|
if (empty($bundle)) {
|
59
|
$bundle = $entity['bundles'][$bundle_type];
|
60
|
}
|
61
|
|
62
|
if (empty($field)) {
|
63
|
$field_instances = field_info_instances($entity_type, $bundle_type);
|
64
|
$field = $field_instances[$field_name];
|
65
|
}
|
66
|
|
67
|
$plugin['title'] = t('@entity @type: @field Field', array('@entity' => $entity['label'], '@type' => $bundle_type, '@field' => $field['label']));
|
68
|
$plugin['keyword'] = $entity_type;
|
69
|
$plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
|
70
|
$plugin['name'] = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name;
|
71
|
$plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
|
72
|
'type' => $bundle_type,
|
73
|
));
|
74
|
|
75
|
return $plugin;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Settings form for the 'by entity_bundle' access plugin
|
80
|
*/
|
81
|
function ctools_entity_field_value_ctools_access_settings($form, &$form_state, $conf) {
|
82
|
$plugin = $form_state['plugin'];
|
83
|
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
|
84
|
$entity_info = entity_get_info($entity_type);
|
85
|
$instances = field_info_instances($entity_type, $bundle_type);
|
86
|
$instance = $instances[$field_name];
|
87
|
$field = field_info_field_by_id($instance['field_id']);
|
88
|
foreach ($field['columns'] as $column => $attributes) {
|
89
|
$columns[] = _field_sql_storage_columnname($field_name, $column);
|
90
|
}
|
91
|
ctools_include('fields');
|
92
|
$entity = (object)array(
|
93
|
$entity_info['entity keys']['bundle'] => $bundle_type,
|
94
|
);
|
95
|
$langcode = field_valid_language(NULL);
|
96
|
$form['settings'] += (array) ctools_field_invoke_field($instance, 'form', $entity_type, $entity, $form, $form_state, array('default' => TRUE, 'language' => $langcode));
|
97
|
// weight is really not important once this is populated and will only interfere with the form layout.
|
98
|
foreach (element_children($form['settings']) as $element) {
|
99
|
unset($form['settings'][$element]['#weight']);
|
100
|
}
|
101
|
|
102
|
// Need more logic here to handle compound fields.
|
103
|
foreach ($columns as $column) {
|
104
|
if (isset($conf[$column]) && is_array($conf[$column])) {
|
105
|
foreach ($conf[$column] as $delta => $conf_value) {
|
106
|
if (is_numeric($delta) && is_array($conf_value)) {
|
107
|
$form['settings'][$field_name][LANGUAGE_NONE][$delta]['value']['#default_value'] = $conf_value['value'];
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
else {
|
112
|
$form['settings'][$field_name][LANGUAGE_NONE]['#default_value'] = $conf[$column];
|
113
|
}
|
114
|
}
|
115
|
|
116
|
return $form;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Compress the entity bundles allowed to the minimum.
|
121
|
*/
|
122
|
function ctools_entity_field_value_ctools_access_settings_submit($form, &$form_state) {
|
123
|
$plugin = $form_state['plugin'];
|
124
|
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
|
125
|
$langcode = field_valid_language(NULL);
|
126
|
$langcode = isset($form_state['input']['settings'][$field_name][$langcode]) ? $langcode : LANGUAGE_NONE;
|
127
|
$instances = field_info_instances($entity_type, $bundle_type);
|
128
|
$instance = $instances[$field_name];
|
129
|
$field = field_info_field_by_id($instance['field_id']);
|
130
|
foreach ($field['columns'] as $column => $attributes) {
|
131
|
$columns[] = _field_sql_storage_columnname($field_name, $column);
|
132
|
}
|
133
|
foreach ($columns as $column) {
|
134
|
$form_state['values']['settings'][$column] = $form_state['input']['settings'][$field_name][$langcode];
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Check for access.
|
140
|
*/
|
141
|
function ctools_entity_field_value_ctools_access_check($conf, $context, $plugin) {
|
142
|
if (!isset($context->data)) {
|
143
|
// If the context doesn't exist -- for example, a newly added entity
|
144
|
// reference is used as a pane visibility criteria -- we deny access.
|
145
|
return FALSE;
|
146
|
}
|
147
|
|
148
|
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
|
149
|
|
150
|
if ($field_items = field_get_items($entity_type, $context->data, $field_name)) {
|
151
|
$langcode = field_language($entity_type, $context->data, $field_name);
|
152
|
// Get field storage columns.
|
153
|
$instance = field_info_instance($entity_type, $field_name, $bundle_type);
|
154
|
$field = field_info_field_by_id($instance['field_id']);
|
155
|
$columns = array();
|
156
|
foreach ($field['columns'] as $column => $attributes) {
|
157
|
$columns[$column] = _field_sql_storage_columnname($field_name, $column);
|
158
|
}
|
159
|
|
160
|
if (isset($conf[$field_name])) {
|
161
|
// We have settings for this field.
|
162
|
$conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($conf[$field_name], $langcode);
|
163
|
if (empty($conf_value_array)) {
|
164
|
return FALSE;
|
165
|
}
|
166
|
|
167
|
// Check field value.
|
168
|
foreach ($field_items as $field_value) {
|
169
|
foreach ($field_value as $field_column => $value) {
|
170
|
// Iterate through config values.
|
171
|
foreach ($conf_value_array as $conf_value) {
|
172
|
// Check access only for stored in config column values.
|
173
|
if (isset($conf_value[$field_column]) && $value == $conf_value[$field_column]) {
|
174
|
return TRUE;
|
175
|
}
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
|
182
|
return FALSE;
|
183
|
}
|
184
|
|
185
|
function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) {
|
186
|
if (!is_array($values) || !isset($values[$langcode])) {
|
187
|
return;
|
188
|
}
|
189
|
$conf_values = array();
|
190
|
|
191
|
foreach ($values[$langcode] as $delta => $value) {
|
192
|
$conf_values[$delta] = $value;
|
193
|
}
|
194
|
|
195
|
return $conf_values;
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Provide a summary description based upon the checked entity_bundle.
|
200
|
*/
|
201
|
function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) {
|
202
|
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
|
203
|
$instances = field_info_instances($entity_type, $bundle_type);
|
204
|
$instance = $instances[$field_name];
|
205
|
$field = field_info_field_by_id($instance['field_id']);
|
206
|
$entity_info = entity_get_info($entity_type);
|
207
|
$entity = (object)array(
|
208
|
$entity_info['entity keys']['bundle'] => $bundle_type,
|
209
|
);
|
210
|
$string = '';
|
211
|
$keys = array();
|
212
|
$values = array();
|
213
|
foreach ($field['columns'] as $column => $attributes) {
|
214
|
$conf_key = _field_sql_storage_columnname($field_name, $column);
|
215
|
if (count($field['columns']) > 1) {
|
216
|
// Add some sort of handling for compound fields
|
217
|
}
|
218
|
else {
|
219
|
if (isset($conf[$conf_key])) {
|
220
|
$entity->{$field_name}[LANGUAGE_NONE][] = array($column => $conf[$conf_key]);
|
221
|
}
|
222
|
}
|
223
|
$string .= " @{$column} equals @{$column}_value";
|
224
|
$keys['@' . $column] = $column;
|
225
|
$values["@{$column}_value"] = $conf[$conf_key];
|
226
|
}
|
227
|
$view_mode = 'full';
|
228
|
$null = NULL;
|
229
|
$options = array('language' => LANGUAGE_NONE);
|
230
|
ctools_include('fields');
|
231
|
$display = field_get_display($instance, $view_mode, $entity);
|
232
|
if (isset($display['module'])) {
|
233
|
$display['type'] = 'list_default';
|
234
|
$function = $display['module'] . '_field_formatter_view';
|
235
|
$items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array();
|
236
|
if (function_exists($function)) {
|
237
|
$elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $items, $display);
|
238
|
}
|
239
|
$value_keys = array_keys($values);
|
240
|
foreach ($value_keys as $key => $value) {
|
241
|
$values[$value] = isset($elements[$key]['#markup']) ? $elements[$key]['#markup'] : '';
|
242
|
}
|
243
|
}
|
244
|
$values = array_merge($keys, $values);
|
245
|
return t($string, $values);
|
246
|
}
|
247
|
|