5 * Plugin to provide access control based upon term vocabulary
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: vocabulary"),
14 'description' => t('Control access by vocabulary.'),
15 'callback' => 'ctools_term_vocabulary_ctools_access_check',
16 'default' => array('vids' => array()),
17 'settings form' => 'ctools_term_vocabulary_ctools_access_settings',
18 'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit',
19 'summary' => 'ctools_term_vocabulary_ctools_access_summary',
20 'required context' => new ctools_context_required(t('Vocabulary'), array(
28 * Settings form for the 'by term_vocabulary' access plugin
30 function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
32 $vocabularies = taxonomy_get_vocabularies();
33 foreach ($vocabularies as $voc) {
34 $options[$voc->machine_name] = check_plain($voc->name);
37 _ctools_term_vocabulary_ctools_access_map_vids($conf);
39 $form['settings']['machine_name'] = array(
40 '#type' => 'checkboxes',
41 '#title' => t('Vocabularies'),
42 '#options' => $options,
43 '#description' => t('Only the checked vocabularies will be valid.'),
44 '#default_value' => $conf['machine_name'],
50 * Compress the term_vocabularys allowed to the minimum.
52 function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
53 $form_state['values']['settings']['machine_name'] = array_filter($form_state['values']['settings']['machine_name']);
59 function ctools_term_vocabulary_ctools_access_check($conf, $context) {
60 // As far as I know there should always be a context at this point, but this
62 if (empty($context) || empty($context->data) || empty($context->data->vocabulary_machine_name)) {
66 _ctools_term_vocabulary_ctools_access_map_vids($conf);
68 if (array_filter($conf['machine_name']) && empty($conf['machine_name'][$context->data->vocabulary_machine_name])) {
76 * Provide a summary description based upon the checked term_vocabularys.
78 function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
79 if (!isset($conf['type'])) {
80 $conf['type'] = array();
82 $vocabularies = taxonomy_get_vocabularies();
84 _ctools_term_vocabulary_ctools_access_map_vids($conf);
87 if (!empty($conf['machine_name'])) {
88 foreach (array_filter($conf['machine_name']) as $machine_name) {
89 foreach ($vocabularies as $vocabulary) {
90 if ($vocabulary->machine_name === $machine_name) {
91 $names[] = check_plain($vocabulary->name);
99 return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
102 return format_plural(count($names), '@identifier vocabulary is "@machine_names"', '@identifier vocabulary is one of "@machine_names"', array(
103 '@machine_names' => implode(', ', $names),
104 '@identifier' => $context->identifier
109 * Helper function to map the vids from old features to the new machine_name.
111 * Add the machine_name key to $conf if the vids key exist.
114 * The configuration of this plugin.
116 function _ctools_term_vocabulary_ctools_access_map_vids(&$conf) {
117 if (!empty($conf['vids'])) {
118 $conf['machine_name'] = array();
119 $vocabularies = taxonomy_get_vocabularies();
120 foreach ($conf['vids'] as $vid) {
121 $machine_name = $vocabularies[$vid]->machine_name;
122 $conf['machine_name'][$machine_name] = $vocabularies[$vid]->machine_name;