node_language.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon node type.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. if (module_exists('locale')) {
  11. $plugin = array(
  12. 'title' => t("Node: language"),
  13. 'description' => t('Control access by node language.'),
  14. 'callback' => 'ctools_node_language_ctools_access_check',
  15. 'default' => array('language' => array()),
  16. 'settings form' => 'ctools_node_language_ctools_access_settings',
  17. 'settings form submit' => 'ctools_node_language_ctools_access_settings_submit',
  18. 'summary' => 'ctools_node_language_ctools_access_summary',
  19. 'required context' => new ctools_context_required(t('Node'), 'node'),
  20. );
  21. }
  22. /**
  23. * Settings form for the 'by node_language' access plugin
  24. */
  25. function ctools_node_language_ctools_access_settings($form, &$form_state, $conf) {
  26. $options = array(
  27. 'current' => t('Current site language'),
  28. 'default' => t('Default site language'),
  29. 'no_language' => t('No language'),
  30. );
  31. $options = array_merge($options, locale_language_list());
  32. $form['settings']['language'] = array(
  33. '#title' => t('Language'),
  34. '#type' => 'checkboxes',
  35. '#options' => $options,
  36. '#description' => t('Pass only if the node is in one of the selected languages.'),
  37. '#default_value' => $conf['language'],
  38. );
  39. return $form;
  40. }
  41. /**
  42. * Check for access.
  43. */
  44. function ctools_node_language_ctools_access_check($conf, $context) {
  45. // As far as I know there should always be a context at this point, but this
  46. // is safe.
  47. if (empty($context) || empty($context->data) || !isset($context->data->language)) {
  48. return FALSE;
  49. }
  50. global $language;
  51. // Specialcase: if 'no language' is checked, return TRUE if the language field is
  52. // empty.
  53. if (!empty($conf['language']['no_language'])) {
  54. if (empty($context->data->language)) {
  55. return TRUE;
  56. }
  57. }
  58. // Specialcase: if 'current' is checked, return TRUE if the current site language
  59. // matches the node language.
  60. if (!empty($conf['language']['current'])) {
  61. if ($context->data->language == $language->language) {
  62. return TRUE;
  63. }
  64. }
  65. // Specialcase: If 'default' is checked, return TRUE if the default site language
  66. // matches the node language.
  67. if (!empty($conf['language']['default'])) {
  68. if ($context->data->language == language_default('language')) {
  69. return TRUE;
  70. }
  71. }
  72. if (array_filter($conf['language']) && empty($conf['language'][$context->data->language])) {
  73. return FALSE;
  74. }
  75. return TRUE;
  76. }
  77. /**
  78. * Provide a summary description based upon the checked node_languages.
  79. */
  80. function ctools_node_language_ctools_access_summary($conf, $context) {
  81. $languages = array(
  82. 'current' => t('Current site language'),
  83. 'default' => t('Default site language'),
  84. 'no_language' => t('No language'),
  85. );
  86. $languages = array_merge($languages, locale_language_list());
  87. if (!isset($conf['language'])) {
  88. $conf['language'] = array();
  89. }
  90. $names = array();
  91. foreach (array_filter($conf['language']) as $language) {
  92. $names[] = $languages[$language];
  93. }
  94. if (empty($names)) {
  95. return t('@identifier is in any language', array('@identifier' => $context->identifier));
  96. }
  97. return format_plural(count($names), '@identifier language is "@languages"', '@identifier language is one of "@languages"', array('@languages' => implode(', ', $names), '@identifier' => $context->identifier));
  98. }