views_plugin_argument_validate_node.inc

  1. 3.x modules/node/views_plugin_argument_validate_node.inc
  2. 2.x modules/node/views_plugin_argument_validate_node.inc

Contains the 'node' argument validator plugin.

File

modules/node/views_plugin_argument_validate_node.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the 'node' argument validator plugin.
  5. */
  6. /**
  7. * Validate whether an argument is an acceptable node.
  8. */
  9. class views_plugin_argument_validate_node extends views_plugin_argument_validate {
  10. function option_definition() {
  11. $options = parent::option_definition();
  12. $options['types'] = array('default' => array());
  13. $options['access'] = array('default' => FALSE, 'bool' => TRUE);
  14. $options['access_op'] = array('default' => 'view');
  15. $options['nid_type'] = array('default' => 'nid');
  16. return $options;
  17. }
  18. function options_form(&$form, &$form_state) {
  19. $types = node_type_get_types();
  20. $options = array();
  21. foreach ($types as $type => $info) {
  22. $options[$type] = check_plain(t($info->name));
  23. }
  24. $form['types'] = array(
  25. '#type' => 'checkboxes',
  26. '#title' => t('Content types'),
  27. '#options' => $options,
  28. '#default_value' => $this->options['types'],
  29. '#description' => t('Choose one or more content types to validate with.'),
  30. );
  31. $form['access'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t('Validate user has access to the content'),
  34. '#default_value' => $this->options['access'],
  35. );
  36. $form['access_op'] = array(
  37. '#type' => 'radios',
  38. '#title' => t('Access operation to check'),
  39. '#options' => array('view' => t('View'), 'update' => t('Edit'), 'delete' => t('Delete')),
  40. '#default_value' => $this->options['access_op'],
  41. '#dependency' => array('edit-options-validate-options-node-access' => array(TRUE)),
  42. );
  43. $form['nid_type'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Filter value format'),
  46. '#options' => array(
  47. 'nid' => t('Node ID'),
  48. 'nids' => t('Node IDs separated by , or +'),
  49. ),
  50. '#default_value' => $this->options['nid_type'],
  51. );
  52. }
  53. function options_submit(&$form, &$form_state, &$options = array()) {
  54. // filter trash out of the options so we don't store giant unnecessary arrays
  55. $options['types'] = array_filter($options['types']);
  56. }
  57. function convert_options(&$options) {
  58. if (!isset($options['types']) && !empty($this->argument->options['validate_argument_node_type'])) {
  59. $options['types'] = isset($this->argument->options['validate_argument_node_type']) ? $this->argument->options['validate_argument_node_type'] : array();
  60. $options['access'] = !empty($this->argument->options['validate_argument_node_access']);
  61. $options['access_op'] = isset($this->argument->options['validate_argument_node_access_op']) ? $this->argument->options['validate_argument_node_access_op'] : 'view';
  62. $options['nid_type'] = isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : array();
  63. }
  64. }
  65. function validate_argument($argument) {
  66. $types = $this->options['types'];
  67. switch ($this->options['nid_type']) {
  68. case 'nid':
  69. if (!is_numeric($argument)) {
  70. return FALSE;
  71. }
  72. $node = node_load($argument);
  73. if (!$node) {
  74. return FALSE;
  75. }
  76. if (!empty($this->options['access'])) {
  77. if (!node_access($this->options['access_op'], $node)) {
  78. return FALSE;
  79. }
  80. }
  81. // Save the title() handlers some work.
  82. $this->argument->validated_title = check_plain($node->title);
  83. if (empty($types)) {
  84. return TRUE;
  85. }
  86. return isset($types[$node->type]);
  87. break;
  88. case 'nids':
  89. $nids = new stdClass();
  90. $nids->value = array($argument);
  91. $nids = views_break_phrase($argument, $nids);
  92. if ($nids->value == array(-1)) {
  93. return FALSE;
  94. }
  95. $test = drupal_map_assoc($nids->value);
  96. $titles = array();
  97. $result = db_query("SELECT * FROM {node} WHERE nid IN (:nids)", array(':nids' => $nids->value));
  98. foreach ($result as $node) {
  99. if ($types && empty($types[$node->type])) {
  100. return FALSE;
  101. }
  102. if (!empty($this->options['access'])) {
  103. if (!node_access($this->options['access_op'], $node)) {
  104. return FALSE;
  105. }
  106. }
  107. $titles[] = check_plain($node->title);
  108. unset($test[$node->nid]);
  109. }
  110. $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);
  111. // If this is not empty, we did not find a nid.
  112. return empty($test);
  113. }
  114. }
  115. }