From 60044db6509400df482f53aa46f56c8833e9ec5e Mon Sep 17 00:00:00 2001 From: Earl Miles <merlin@logrus.com> Date: Tue, 13 Jan 2009 18:32:56 +0000 Subject: [PATCH] Add access/selector by node language. --- plugins/access/node_access.inc | 4 +- plugins/access/node_language.inc | 106 +++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 plugins/access/node_language.inc diff --git a/plugins/access/node_access.inc b/plugins/access/node_access.inc index bf5393ce..6b0c0502 100644 --- a/plugins/access/node_access.inc +++ b/plugins/access/node_access.inc @@ -11,7 +11,7 @@ */ function ctools_node_access_ctools_access() { $args['node_access'] = array( - 'title' => t("Operation"), + 'title' => t("Node access by node security"), 'description' => t('Control access with built in Drupal node access test.'), 'callback' => 'ctools_node_access_ctools_access_check', 'default' => array('type' => 'view'), @@ -32,7 +32,7 @@ function ctools_node_access_ctools_access() { */ function ctools_node_access_ctools_access_settings(&$form, &$form_state, $conf) { $form['settings']['type'] = array( - '#title' => t('Access type'), + '#title' => t('Operation'), '#type' => 'radios', '#options' => array( 'view' => t('View'), diff --git a/plugins/access/node_language.inc b/plugins/access/node_language.inc new file mode 100644 index 00000000..7ce2254e --- /dev/null +++ b/plugins/access/node_language.inc @@ -0,0 +1,106 @@ +<?php +// $Id$ + +/** + * @file + * Plugin to provide access control based upon node type. + */ + +/** + * Implementation of specially named hook_ctools_arguments(). + */ +function ctools_node_language_ctools_access() { + if (module_exists('locale')) { + $args['node_language'] = array( + 'title' => t("Node access by language"), + 'description' => t('Control access by node language.'), + 'callback' => 'ctools_node_language_ctools_access_check', + 'default' => array('language' => array()), + 'settings form' => 'ctools_node_language_ctools_access_settings', + 'settings form submit' => 'ctools_node_language_ctools_access_settings_submit', + 'summary' => 'ctools_node_language_ctools_acesss_summary', + 'required context' => new ctools_context_required(t('Node'), 'node'), + ); + } + + return $args; +} + +/** + * Settings form for the 'by node_language' access plugin + */ +function ctools_node_language_ctools_access_settings(&$form, &$form_state, $conf) { + $options = array( + 'current' => t('Current site language'), + 'default' => t('Default site language'), + ); + $options = array_merge($options, locale_language_list()); + $form['settings']['language'] = array( + '#title' => t('Access type'), + '#type' => 'checkboxes', + '#options' => $options, + '#description' => t('Pass only if the node is in one of the selected languages.'), + '#default_value' => $conf['language'], + ); +} + +/** + * Check for access. + */ +function ctools_node_language_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || !isset($context->data->language)) { + return FALSE; + } + + global $language; + + // Specialcase: if 'current' is checked, return TRUE if the current site language + // matches the node language. + if (!empty($conf['language']['current'])) { + if ($context->data->language == $language->language) { + return TRUE; + } + } + + // Specialcase: If 'default' is checked, return TRUE if the default site language + // matches the node language. + if (!empty($conf['language']['default'])) { + if ($context->data->language == language_default('language')) { + return TRUE; + } + } + + if (array_filter($conf['language']) && empty($conf['language'][$context->data->language])) { + return FALSE; + } + + return TRUE; +} + +/** + * Provide a summary description based upon the checked node_languages. + */ +function ctools_node_language_ctools_acesss_summary($conf, $context) { + $languages = array( + 'current' => t('Current site language'), + 'default' => t('Default site language'), + ); + $languages = array_merge($languages, locale_language_list()); + + if (!isset($conf['language'])) { + $conf['language'] = array(); + } + + $names = array(); + foreach (array_filter($conf['language']) as $language) { + $names[] = $languages[$language]; + } + + if (empty($names)) { + return t('@identifier can be in any language', array('@identifier' => $context->identifier)); + } + + return format_plural(count($names), '@identifier can be in languages "@languages"', '@identifier can be in language "@languages"', array('@languages' => implode(', ', $names), '@identifier' => $context->identifier)); +} \ No newline at end of file -- GitLab