path_visibility.inc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control/visibility based on path.
  5. */
  6. $plugin = array(
  7. 'title' => t('String: URL path'),
  8. 'description' => t('Control access by the current path.'),
  9. 'callback' => 'ctools_path_visibility_ctools_access_check',
  10. 'settings form' => 'ctools_path_visibility_ctools_access_settings',
  11. 'summary' => 'ctools_path_visibility_ctools_access_summary',
  12. 'required context' => new ctools_context_optional(t('Path'), 'string'),
  13. 'default' => array('visibility_setting' => 1, 'paths' => ''),
  14. );
  15. /**
  16. * Settings form
  17. */
  18. function ctools_path_visibility_ctools_access_settings($form, &$form_state, $conf) {
  19. $form['settings']['note'] = array(
  20. '#value' => '<div class="description">' . t('Note: if no context is chosen, the current page path will be used.') . '</div>',
  21. );
  22. $form['settings']['visibility_setting'] = array(
  23. '#type' => 'radios',
  24. '#options' => array(
  25. 1 => t('Allow access on the following pages'),
  26. 0 => t('Allow access on all pages except the following pages'),
  27. ),
  28. '#default_value' => $conf['visibility_setting'],
  29. );
  30. $form['settings']['paths'] = array(
  31. '#type' => 'textarea',
  32. '#title' => t('Paths'),
  33. '#default_value' => $conf['paths'],
  34. '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
  35. );
  36. return $form;
  37. }
  38. /**
  39. * Check for access.
  40. */
  41. function ctools_path_visibility_ctools_access_check($conf, $context) {
  42. if (isset($context->data)) {
  43. $base_path = $context->data;
  44. }
  45. else {
  46. $base_path = $_GET['q'];
  47. }
  48. $path = drupal_get_path_alias($base_path);
  49. $page_match = drupal_match_path($path, $conf['paths']);
  50. // If there's a path alias, we may still be at the un-aliased path
  51. // so check that as well.
  52. if (!isset($context->data) && $path != $base_path) {
  53. $page_match = $page_match || drupal_match_path($base_path, $conf['paths']);
  54. }
  55. // When $conf['visibility_setting'] has a value of 0, the block is displayed
  56. // on all pages except those listed in $block->pages. When set to 1, it
  57. // is displayed only on those pages listed in $block->pages.
  58. $page_match = !($conf['visibility_setting'] xor $page_match);
  59. return $page_match;
  60. }
  61. /**
  62. * Provide a summary description.
  63. */
  64. function ctools_path_visibility_ctools_access_summary($conf, $context) {
  65. $paths = array();
  66. foreach (explode("\n", $conf['paths']) as $path) {
  67. $paths[] = check_plain($path);
  68. }
  69. $identifier = $context->type == 'any' ? t('Current path') : $context->identifier;
  70. if ($conf['visibility_setting']) {
  71. return format_plural(count($paths), '@identifier is "@paths"', '@identifier type is one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier));
  72. }
  73. else {
  74. return format_plural(count($paths), '@identifier is not "@paths"', '@identifier type is not one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier));
  75. }
  76. }