comparison sites/all/modules/ctools/plugins/access/entity_field_value.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 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 list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
143
144 if ($field_items = field_get_items($entity_type, $context->data, $field_name)) {
145 $langcode = field_language($entity_type, $context->data, $field_name);
146 // Get field storage columns.
147 $instance = field_info_instance($entity_type, $field_name, $bundle_type);
148 $field = field_info_field_by_id($instance['field_id']);
149 $columns = array();
150 foreach ($field['columns'] as $column => $attributes) {
151 $columns[$column] = _field_sql_storage_columnname($field_name, $column);
152 }
153
154 if (isset($conf[$field_name])) {
155 // We have settings for this field.
156 $conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($conf[$field_name], $langcode);
157 if (empty($conf_value_array)) {
158 return FALSE;
159 }
160
161 // Check field value.
162 foreach ($field_items as $field_value) {
163 foreach ($field_value as $field_column => $value) {
164 // Iterate through config values.
165 foreach ($conf_value_array as $conf_value) {
166 // Check access only for stored in config column values.
167 if (isset($conf_value[$field_column]) && $value == $conf_value[$field_column]) {
168 return TRUE;
169 }
170 }
171 }
172 }
173 }
174 }
175
176 return FALSE;
177 }
178
179 function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) {
180 if (!is_array($values) || !isset($values[$langcode])) {
181 return;
182 }
183 $conf_values = array();
184
185 foreach ($values[$langcode] as $delta => $value) {
186 $conf_values[$delta] = $value;
187 }
188
189 return $conf_values;
190 }
191
192 /**
193 * Provide a summary description based upon the checked entity_bundle.
194 */
195 function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) {
196 list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
197 $instances = field_info_instances($entity_type, $bundle_type);
198 $instance = $instances[$field_name];
199 $field = field_info_field_by_id($instance['field_id']);
200 $entity_info = entity_get_info($entity_type);
201 $entity = (object)array(
202 $entity_info['entity keys']['bundle'] => $bundle_type,
203 );
204 $string = '';
205 $keys = array();
206 $values = array();
207 foreach ($field['columns'] as $column => $attributes) {
208 $conf_key = _field_sql_storage_columnname($field_name, $column);
209 if (count($field['columns']) > 1) {
210 // Add some sort of handling for compound fields
211 }
212 else {
213 if (isset($conf[$conf_key])) {
214 $entity->{$field_name}[LANGUAGE_NONE][] = array($column => $conf[$conf_key]);
215 }
216 }
217 $string .= " @{$column} equals @{$column}_value";
218 $keys['@' . $column] = $column;
219 $values["@{$column}_value"] = $conf[$conf_key];
220 }
221 $view_mode = 'full';
222 $null = NULL;
223 $options = array('language' => LANGUAGE_NONE);
224 ctools_include('fields');
225 $display = field_get_display($instance, $view_mode, $entity);
226 if (isset($display['module'])) {
227 $display['type'] = 'list_default';
228 $function = $display['module'] . '_field_formatter_view';
229 $items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array();
230 if (function_exists($function)) {
231 $elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $items, $display);
232 }
233 $value_keys = array_keys($values);
234 foreach ($value_keys as $key => $value) {
235 $values[$value] = isset($elements[$key]['#markup']) ? $elements[$key]['#markup'] : '';
236 }
237 }
238 $values = array_merge($keys, $values);
239 return t($string, $values);
240 }
241