5 * Plugin to provide access control based upon specific terms.
9 * Plugins are described by creating a $plugin array which will be used
10 * by the system that includes this file.
13 'title' => t("Taxonomy: term"),
14 'description' => t('Control access by a specific term.'),
15 'callback' => 'ctools_term_ctools_access_check',
16 'default' => array('vids' => array()),
17 'settings form' => 'ctools_term_ctools_access_settings',
18 'settings form validation' => 'ctools_term_ctools_access_settings_validate',
19 'settings form submit' => 'ctools_term_ctools_access_settings_submit',
20 'summary' => 'ctools_term_ctools_access_summary',
21 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
25 * Settings form for the 'by term' access plugin
27 function ctools_term_ctools_access_settings($form, &$form_state, $conf) {
28 // If no configuration was saved before, set some defaults.
34 if (!isset($conf['vid'])) {
38 $form['settings']['vid'] = array(
39 '#title' => t('Vocabulary'),
41 '#options' => array(),
42 '#description' => t('Select the vocabulary for this form.'),
43 '#id' => 'ctools-select-vid',
44 '#default_value' => $conf['vid'],
48 ctools_include('dependent');
51 // A note: Dependency works strangely on these forms as they have never been
52 // updated to a more modern system so they are not individual forms of their
53 // own like the content types.
55 $form['settings']['#tree'] = TRUE;
57 // Loop over each of the configured vocabularies.
58 foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
59 $options[$vid] = $vocabulary->name;
60 $form['settings'][$vocabulary->vid] = array(
61 '#title' => t('Terms'),
62 '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description,
63 '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
64 '#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '',
69 foreach (taxonomy_get_tree($vocabulary->vid) as $tid => $term) {
70 $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
72 $form['settings'][$vocabulary->vid]['#type'] = 'select';
73 $form['settings'][$vocabulary->vid]['#options'] = $terms;
76 $form['settings']['vid']['#options'] = $options;
83 function ctools_term_ctools_access_check($conf, $context) {
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->vid) || empty($context->data->tid)) {
91 if (!isset($conf['vid'])) {
97 if (!isset($conf[$vid])) {
103 $terms = array_filter($conf[$vid]);
104 // For multi-term if any terms coincide, let's call that good enough:
105 if (isset($context->tids)) {
106 return (bool) array_intersect($terms, $context->tids);
109 return in_array($context->data->tid, $terms);
114 * Provide a summary description based upon the checked terms.
116 function ctools_term_ctools_access_summary($conf, $context) {
119 foreach ($conf[$vid] as $tid) {
120 $term = taxonomy_term_load($tid);
121 $terms[] = $term->name;
124 return format_plural(count($terms),
125 '@term can be the term "@terms"',
126 '@term can be one of these terms: @terms',
127 array('@terms' => implode(', ', $terms),
128 '@term' => $context->identifier));