summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/plugins/access/entity_bundle.inc
blob: e07a048decc74700dacb03306a3f9ef81d63fc9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * @file
 * Plugin to provide access control based upon entity bundle.
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' => t("Entity: bundle"),
  'description' => t('Control access by entity bundle.'),
  'callback' => 'ctools_entity_bundle_ctools_access_check',
  'default' => array('type' => array()),
  'settings form' => 'ctools_entity_bundle_ctools_access_settings',
  'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit',
  'summary' => 'ctools_entity_bundle_ctools_access_summary',
  'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions',
  'get child' => 'ctools_entity_bundle_ctools_access_get_child',
  'get children' => 'ctools_entity_bundle_ctools_access_get_children',
);

function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) {
  $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent);
  return $plugins[$parent . ':' . $child];
}

function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {
  $entities = entity_get_info();
  $plugins = array();
  foreach ($entities as $entity_type => $entity) {
    $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label']));
    $plugin['keyword'] = $entity_type;
    $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
    $plugin['name'] = $parent . ':' . $entity_type;
    $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
    $plugins[$parent . ':' . $entity_type] = $plugin;
  }

  return $plugins;
}

/**
 * Settings form for the 'by entity_bundle' access plugin
 */
function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) {
  $plugin = $form_state['plugin'];
  $entity_type = explode(':', $plugin['name']);
  $entity_type = $entity_type[1];
  $entity = entity_get_info($entity_type);
  foreach ($entity['bundles'] as $type => $info) {
    $options[$type] = check_plain($info['label']);
  }

  $form['settings']['type'] = array(
    '#title' => t('Entity Bundle'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#description' => t('Only the checked entity bundles will be valid.'),
    '#default_value' => $conf['type'],
  );
  return $form;
}

/**
 * Compress the entity bundles allowed to the minimum.
 */
function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) {
  $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
}

/**
 * Check for access.
 */
function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {
  list($plugin_name, $entity_type) = explode(':', $plugin['name']);
  if (!$entity_type) {
    return FALSE;
  };

  $entity = entity_get_info($entity_type);
  // As far as I know there should always be a context at this point, but this
  // is safe.
  if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {
    return FALSE;
  }

  if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {
    return FALSE;
  }

  return TRUE;
}

/**
 * Inform the UI that we've eliminated a bunch of possibilities for this
 * context.
 */
function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) {
  if (isset($context->restrictions['type'])) {
    $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
  }
  else {
    $context->restrictions['type'] = array_keys(array_filter($conf['type']));
  }
}

/**
 * Provide a summary description based upon the checked entity_bundle.
 */
function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {
  if (!isset($conf['type'])) {
    $conf['type'] = array();
  }

  list($plugin_name, $entity_type) = explode(':', $plugin['name']);
  if (!$entity_type) {
    return t('Error, misconfigured entity_bundle access plugin');
  };

  $entity = entity_get_info($entity_type);

  $names = array();
  foreach (array_filter($conf['type']) as $type) {
    $names[] = check_plain($entity['bundles'][$type]['label']);
  }

  if (empty($names)) {
    return t('@identifier is any bundle', array('@identifier' => $context->identifier));
  }

  return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
}