context_exists.inc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control/visibility based on existence of a specified context
  5. */
  6. $plugin = array(
  7. 'title' => t("Context exists"),
  8. 'description' => t('Control access by whether or not a context exists and contains data.'),
  9. 'callback' => 'ctools_context_exists_ctools_access_check',
  10. 'settings form' => 'ctools_context_exists_ctools_access_settings',
  11. 'summary' => 'ctools_context_exists_ctools_access_summary',
  12. 'required context' => new ctools_context_required(t('Context'), 'any', TRUE),
  13. 'defaults' => array('exists' => TRUE),
  14. );
  15. /**
  16. * Settings form
  17. */
  18. function ctools_context_exists_ctools_access_settings($form, &$form_state, $conf) {
  19. $form['settings']['exists'] = array(
  20. '#type' => 'radios',
  21. '#description' => t("Check to see if the context exists (contains data) or does not exist (contains no data). For example, if a context is optional and the path does not contain an argument for that context, it will not exist."),
  22. '#options' => array(TRUE => t('Exists'), FALSE => t("Doesn't exist")),
  23. '#default_value' => $conf['exists'],
  24. );
  25. return $form;
  26. }
  27. /**
  28. * Check for access
  29. */
  30. function ctools_context_exists_ctools_access_check($conf, $context) {
  31. // xor returns false if the two bools are the same, and true if they are not.
  32. // i.e, if we asked for context_exists and it does, return true.
  33. // If we asked for context does not exist and it does, return false.
  34. return (empty($context->data) xor !empty($conf['exists']));
  35. }
  36. /**
  37. * Provide a summary description based upon the specified context
  38. */
  39. function ctools_context_exists_ctools_access_summary($conf, $context) {
  40. if (!empty($conf['exists'])) {
  41. return t('@identifier exists', array('@identifier' => $context->identifier));
  42. }
  43. else {
  44. return t('@identifier does not exist', array('@identifier' => $context->identifier));
  45. }
  46. }