Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | ||
3 | /** | |
4 | * @file | |
5 | * Plugin to provide access control based upon entity bundle. | |
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: bundle"), | |
14 | 'description' => t('Control access by entity bundle.'), | |
15 | 'callback' => 'ctools_entity_bundle_ctools_access_check', | |
16 | 'default' => array('type' => array()), | |
17 | 'settings form' => 'ctools_entity_bundle_ctools_access_settings', | |
18 | 'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit', | |
19 | 'summary' => 'ctools_entity_bundle_ctools_access_summary', | |
20 | 'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions', | |
21 | 'get child' => 'ctools_entity_bundle_ctools_access_get_child', | |
22 | 'get children' => 'ctools_entity_bundle_ctools_access_get_children', | |
23 | ); | |
24 | ||
25 | function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) { | |
26 | $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent); | |
27 | return $plugins[$parent . ':' . $child]; | |
28 | } | |
29 | ||
30 | function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) { | |
31 | $entities = entity_get_info(); | |
32 | $plugins = array(); | |
33 | foreach ($entities as $entity_type => $entity) { | |
34 | $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label'])); | |
35 | $plugin['keyword'] = $entity_type; | |
36 | $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type)); | |
37 | $plugin['name'] = $parent . ':' . $entity_type; | |
38 | $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type); | |
39 | $plugins[$parent . ':' . $entity_type] = $plugin; | |
40 | } | |
41 | ||
42 | return $plugins; | |
43 | } | |
44 | ||
45 | /** | |
46 | * Settings form for the 'by entity_bundle' access plugin | |
47 | */ | |
48 | function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) { | |
49 | $plugin = $form_state['plugin']; | |
50 | $entity_type = explode(':', $plugin['name']); | |
51 | $entity_type = $entity_type[1]; | |
52 | $entity = entity_get_info($entity_type); | |
53 | foreach ($entity['bundles'] as $type => $info) { | |
54 | $options[$type] = check_plain($info['label']); | |
55 | } | |
56 | ||
57 | $form['settings']['type'] = array( | |
58 | '#title' => t('Entity Bundle'), | |
59 | '#type' => 'checkboxes', | |
60 | '#options' => $options, | |
61 | '#description' => t('Only the checked entity bundles will be valid.'), | |
62 | '#default_value' => $conf['type'], | |
63 | ); | |
64 | return $form; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Compress the entity bundles allowed to the minimum. | |
69 | */ | |
70 | function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) { | |
71 | $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Check for access. | |
76 | */ | |
77 | function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) { | |
78 | list($plugin_name, $entity_type) = explode(':', $plugin['name']); | |
79 | if (!$entity_type) { | |
80 | return FALSE; | |
81 | }; | |
82 | ||
83 | $entity = entity_get_info($entity_type); | |
84 | // As far as I know there should always be a context at this point, but this | |
85 | // is safe. | |
86 | if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) { | |
87 | return FALSE; | |
88 | } | |
89 | ||
90 | if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) { | |
91 | return FALSE; | |
92 | } | |
93 | ||
94 | return TRUE; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Inform the UI that we've eliminated a bunch of possibilities for this | |
99 | * context. | |
100 | */ | |
101 | function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) { | |
102 | if (isset($context->restrictions['type'])) { | |
103 | $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type'])))); | |
104 | } | |
105 | else { | |
106 | $context->restrictions['type'] = array_keys(array_filter($conf['type'])); | |
107 | } | |
108 | } | |
109 | ||
110 | /** | |
111 | * Provide a summary description based upon the checked entity_bundle. | |
112 | */ | |
113 | function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) { | |
114 | if (!isset($conf['type'])) { | |
115 | $conf['type'] = array(); | |
116 | } | |
117 | ||
118 | list($plugin_name, $entity_type) = explode(':', $plugin['name']); | |
119 | if (!$entity_type) { | |
120 | return t('Error, misconfigured entity_bundle access plugin'); | |
121 | }; | |
122 | ||
123 | $entity = entity_get_info($entity_type); | |
124 | ||
125 | $names = array(); | |
126 | foreach (array_filter($conf['type']) as $type) { | |
127 | $names[] = check_plain($entity['bundles'][$type]['label']); | |
128 | } | |
129 | ||
130 | if (empty($names)) { | |
131 | return t('@identifier is any bundle', array('@identifier' => $context->identifier)); | |
132 | } | |
133 | ||
134 | return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier)); | |
135 | } | |
136 |