Project

General

Profile

Paste
Download (3.71 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / ctools / plugins / access / node_type.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control based upon node type.
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("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',
22
);
23

    
24
/**
25
 * Settings form for the 'by node_type' access plugin.
26
 */
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);
31
  }
32

    
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'],
39
  );
40
  return $form;
41
}
42

    
43
/**
44
 * Compress the node_types allowed to the minimum.
45
 */
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']);
48
}
49

    
50
/**
51
 * Check for access.
52
 */
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
55
  // is safe.
56
  if (empty($context) || empty($context->data) || empty($context->data->type)) {
57
    return FALSE;
58
  }
59

    
60
  if (array_filter($conf['type']) && empty($conf['type'][$context->data->type])) {
61
    return FALSE;
62
  }
63

    
64
  return TRUE;
65
}
66

    
67
/**
68
 * Inform the UI that we've eliminated a bunch of possibilities for this
69
 * context.
70
 */
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']))));
74
  }
75
  else {
76
    $context->restrictions['type'] = array_keys(array_filter($conf['type']));
77
  }
78
}
79

    
80
/**
81
 * Provide a summary description based upon the checked node_types.
82
 */
83
function ctools_node_type_ctools_access_summary($conf, $context) {
84
  if (!isset($conf['type'])) {
85
    $conf['type'] = array();
86
  }
87
  $types = node_type_get_types();
88

    
89
  $names = array();
90
  // If a node type doesn't exist, let the user know, but prevent a notice.
91
  $missing_types = array();
92

    
93
  foreach (array_filter($conf['type']) as $type) {
94
    if (!empty($types[$type])) {
95
      $names[] = check_plain($types[$type]->name);
96
    }
97
    else {
98
      $missing_types[] = check_plain($type);
99
    }
100
  }
101

    
102
  if (empty($names) && empty($missing_types)) {
103
    return t('@identifier is any node type', array('@identifier' => $context->identifier));
104
  }
105

    
106
  if (!empty($missing_types)) {
107
    $output = array();
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));
110
    }
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);
113
  }
114

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