context_condition_node.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Trigger context on node view only.
  4. */
  5. define('CONTEXT_NODE_VIEW', 0);
  6. /**
  7. * Trigger context on node view and node form.
  8. */
  9. define('CONTEXT_NODE_FORM', 1);
  10. /**
  11. * Trigger context on node form only.
  12. */
  13. define('CONTEXT_NODE_FORM_ONLY', 2);
  14. /**
  15. * Expose node views/node forms of specific node types as a context condition.
  16. */
  17. class context_condition_node extends context_condition {
  18. function condition_values() {
  19. $values = array();
  20. foreach (node_type_get_types() as $type) {
  21. $values[$type->type] = check_plain($type->name);
  22. }
  23. return $values;
  24. }
  25. function options_form($context) {
  26. $defaults = $this->fetch_from_context($context, 'options');
  27. return array(
  28. 'node_form' => array(
  29. '#title' => t('Set on node form'),
  30. '#type' => 'select',
  31. '#options' => array(
  32. CONTEXT_NODE_VIEW => t('No'),
  33. CONTEXT_NODE_FORM => t('Yes'),
  34. CONTEXT_NODE_FORM_ONLY => t('Only on node form')
  35. ),
  36. '#description' => t('Set this context on node forms'),
  37. '#default_value' => isset($defaults['node_form']) ? $defaults['node_form'] : TRUE,
  38. ),
  39. );
  40. }
  41. function execute($node, $op) {
  42. foreach ($this->get_contexts($node->type) as $context) {
  43. // Check the node form option.
  44. $options = $this->fetch_from_context($context, 'options');
  45. if ($op === 'form') {
  46. $options = $this->fetch_from_context($context, 'options');
  47. if (!empty($options['node_form']) && in_array($options['node_form'], array(CONTEXT_NODE_FORM, CONTEXT_NODE_FORM_ONLY))) {
  48. $this->condition_met($context, $node->type);
  49. }
  50. }
  51. elseif (empty($options['node_form']) || $options['node_form'] != CONTEXT_NODE_FORM_ONLY) {
  52. $this->condition_met($context, $node->type);
  53. }
  54. }
  55. }
  56. }