5 * Contains the 'node' argument validator plugin.
9 * Validate whether an argument is an acceptable node.
11 class views_plugin_argument_validate_node extends views_plugin_argument_validate {
12 function option_definition() {
13 $options = parent::option_definition();
14 $options['types'] = array('default' => array());
15 $options['access'] = array('default' => FALSE, 'bool' => TRUE);
16 $options['access_op'] = array('default' => 'view');
17 $options['nid_type'] = array('default' => 'nid');
22 function options_form(&$form, &$form_state) {
23 $types = node_type_get_types();
25 foreach ($types as $type => $info) {
26 $options[$type] = check_plain(t($info->name));
29 $form['types'] = array(
30 '#type' => 'checkboxes',
31 '#title' => t('Content types'),
32 '#options' => $options,
33 '#default_value' => $this->options['types'],
34 '#description' => t('Choose one or more content types to validate with.'),
37 $form['access'] = array(
38 '#type' => 'checkbox',
39 '#title' => t('Validate user has access to the content'),
40 '#default_value' => $this->options['access'],
42 $form['access_op'] = array(
44 '#title' => t('Access operation to check'),
45 '#options' => array('view' => t('View'), 'update' => t('Edit'), 'delete' => t('Delete')),
46 '#default_value' => $this->options['access_op'],
47 '#dependency' => array('edit-options-validate-options-node-access' => array(TRUE)),
50 $form['nid_type'] = array(
52 '#title' => t('Filter value format'),
54 'nid' => t('Node ID'),
55 'nids' => t('Node IDs separated by , or +'),
57 '#default_value' => $this->options['nid_type'],
61 function options_submit(&$form, &$form_state, &$options = array()) {
62 // filter trash out of the options so we don't store giant unnecessary arrays
63 $options['types'] = array_filter($options['types']);
66 function convert_options(&$options) {
67 if (!isset($options['types']) && !empty($this->argument->options['validate_argument_node_type'])) {
68 $options['types'] = isset($this->argument->options['validate_argument_node_type']) ? $this->argument->options['validate_argument_node_type'] : array();
69 $options['access'] = !empty($this->argument->options['validate_argument_node_access']);
70 $options['access_op'] = isset($this->argument->options['validate_argument_node_access_op']) ? $this->argument->options['validate_argument_node_access_op'] : 'view';
71 $options['nid_type'] = isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : array();
75 function validate_argument($argument) {
76 $types = $this->options['types'];
78 switch ($this->options['nid_type']) {
80 if (!is_numeric($argument)) {
83 $node = node_load($argument);
88 if (!empty($this->options['access'])) {
89 if (!node_access($this->options['access_op'], $node)) {
94 // Save the title() handlers some work.
95 $this->argument->validated_title = check_plain($node->title);
101 return isset($types[$node->type]);
104 $nids = new stdClass();
105 $nids->value = array($argument);
106 $nids = views_break_phrase($argument, $nids);
107 if ($nids->value == array(-1)) {
111 $test = drupal_map_assoc($nids->value);
114 $result = db_query("SELECT * FROM {node} WHERE nid IN (:nids)", array(':nids' => $nids->value));
115 foreach ($result as $node) {
116 if ($types && empty($types[$node->type])) {
120 if (!empty($this->options['access'])) {
121 if (!node_access($this->options['access_op'], $node)) {
126 $titles[] = check_plain($node->title);
127 unset($test[$node->nid]);
130 $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);
131 // If this is not empty, we did not find a nid.