5 * Plugin to provide access control based upon entity bundle.
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: 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',
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];
30 function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {
31 $entities = entity_get_info();
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;
46 * Settings form for the 'by entity_bundle' access plugin
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']);
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'],
68 * Compress the entity bundles allowed to the minimum.
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']);
77 function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {
78 list($plugin_name, $entity_type) = explode(':', $plugin['name']);
83 $entity = entity_get_info($entity_type);
84 // As far as I know there should always be a context at this point, but this
86 if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {
90 if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {
98 * Inform the UI that we've eliminated a bunch of possibilities for this
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']))));
106 $context->restrictions['type'] = array_keys(array_filter($conf['type']));
111 * Provide a summary description based upon the checked entity_bundle.
113 function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {
114 if (!isset($conf['type'])) {
115 $conf['type'] = array();
118 list($plugin_name, $entity_type) = explode(':', $plugin['name']);
120 return t('Error, misconfigured entity_bundle access plugin');
123 $entity = entity_get_info($entity_type);
126 foreach (array_filter($conf['type']) as $type) {
127 $names[] = check_plain($entity['bundles'][$type]['label']);
131 return t('@identifier is any bundle', array('@identifier' => $context->identifier));
134 return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));