5 * Plugin to provide access control based upon node type.
9 * Plugins are described by creating a $plugin array which will be used
10 * by the system that includes this file.
13 'title' => t("Node: type"),
14 'description' => t('Control access by node_type.'),
15 'callback' => 'ctools_node_type_ctools_access_check',
16 'default' => array('type' => array()),
17 'settings form' => 'ctools_node_type_ctools_access_settings',
18 'settings form submit' => 'ctools_node_type_ctools_access_settings_submit',
19 'summary' => 'ctools_node_type_ctools_access_summary',
20 'required context' => new ctools_context_required(t('Node'), 'node'),
21 'restrictions' => 'ctools_node_type_ctools_access_restrictions',
25 * Settings form for the 'by node_type' access plugin
27 function ctools_node_type_ctools_access_settings($form, &$form_state, $conf) {
28 $types = node_type_get_types();
29 foreach ($types as $type => $info) {
30 $options[$type] = check_plain($info->name);
33 $form['settings']['type'] = array(
34 '#title' => t('Node type'),
35 '#type' => 'checkboxes',
36 '#options' => $options,
37 '#description' => t('Only the checked node types will be valid.'),
38 '#default_value' => $conf['type'],
44 * Compress the node_types allowed to the minimum.
46 function ctools_node_type_ctools_access_settings_submit($form, &$form_state) {
47 $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
53 function ctools_node_type_ctools_access_check($conf, $context) {
54 // As far as I know there should always be a context at this point, but this
56 if (empty($context) || empty($context->data) || empty($context->data->type)) {
60 if (array_filter($conf['type']) && empty($conf['type'][$context->data->type])) {
68 * Inform the UI that we've eliminated a bunch of possibilities for this
71 function ctools_node_type_ctools_access_restrictions($conf, &$context) {
72 if (isset($context->restrictions['type'])) {
73 $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
76 $context->restrictions['type'] = array_keys(array_filter($conf['type']));
81 * Provide a summary description based upon the checked node_types.
83 function ctools_node_type_ctools_access_summary($conf, $context) {
84 if (!isset($conf['type'])) {
85 $conf['type'] = array();
87 $types = node_type_get_types();
90 // If a node type doesn't exist, let the user know, but prevent a notice.
91 $missing_types = array();
93 foreach (array_filter($conf['type']) as $type) {
94 if (!empty($types[$type])) {
95 $names[] = check_plain($types[$type]->name);
98 $missing_types[] = check_plain($type);
102 if (empty($names) && empty($missing_types)) {
103 return t('@identifier is any node type', array('@identifier' => $context->identifier));
106 if (!empty($missing_types)) {
108 if (!empty($names)) {
109 $output[] = format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
111 $output[] = format_plural(count($missing_types), 'Missing/ deleted type "@types"', 'Missing/ deleted type is one of "@types"', array('@types' => implode(', ', $missing_types)));
112 return implode(' | ', $output);
115 return format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));