I have a problem with the views argument PHP-Validation:

I want the PHP-Code be "TRUE" if the argument/USER-ID is the same as the value of the CCK-Field "Userreference" (I named it "field_privacy" in my examples).

(What i expect: every user should see all nodes which are marked by the Userreference with its name/uid. And later a bit more complex ...)

I have set up a Views-argument with "Content: Userreference".

In a Drupal-Block i have this working PHP-Code:

global $user;
if (arg(0) == 'node' && is_numeric(arg(1))) {$node = node_load(arg(1));}
if ($user->uid == $node->field_privacy[0]['uid']) {print '<p>TRUE</p>';} else {print '<p>FALSE</p>';};

If i paste this similar(?) code into the views-argument PHP-Validation it won't work any more:

global $user;
if (arg(0) == 'node' && is_numeric(arg(1))) {$node = node_load(arg(1));}
return $user->uid == $node->field_privacy[0]['uid'];

or with the User-ID als argument:
return $argument == $node->field_privacy[0]['uid'];

If i take a number/Uid, for example:
return $user->uid == 12;
or
return $argument == 12;
everything is working fine. Of course, "12" should be the value/uid of the nodes CCK-Field "Userreference" ...

(If i use the "Basic Validation" instead of the "PHP-Code", everything is working as expected ... so what's the similar PHP-Code to the Basic-Validation? I want a bit more complex validation later on ... )

What's wrong? How to get the value of the CCK-Field into my PHP-Code?

Thank you for any help!

Tobias

Comments

WorldFallz’s picture

You can do print_r($node->field_privacy[0]); to see the actual structure of the userreference field (i don't remember it off the top of my head).

Also-- be aware that this is not real access control. Whether or not nodes get listed in a view does not affect their 'viewability' anywhere else. That is, people will still be able to random browse nodes by entering their URLs directly into the browser (ie site.com/node/5, site.com/node/1000, etc).

tfranz’s picture

Thank you for your reply!
The following code is working in a block (showing me the Uid of the nodes userreference-field):

if (arg(0) == 'node' && is_numeric(arg(1))) {$node = node_load(arg(1));}
print '<p>'.$node->field_privacy[0]['uid'].'</p>';

So i expected $node->field_privacy[0]['uid'] to work in views, too ... but it seems it does not?!

(The real access control is another problem i solved already ... :-)

WorldFallz’s picture

hmmm, maybe try:

global $user;
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  if ($user->uid == $node->field_privacy[0]['uid']) {
    return TRUE;
  } else {
    return FALSE;
  }
}
tfranz’s picture

... no, it's not working either ... :-(

WorldFallz’s picture

Can you paste your views export into the thread?

tfranz’s picture

"field_privacy" is the name of my CCK-field "userreference" ...

$view = new view;
$view->name = 'downloads2';
$view->description = 'Downloads2';
$view->tag = 'downloads2';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '<h3>[title]</h3>',
      'make_link' => 0,
      'path' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
    ),
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Übersteuern',
    ),
  ),
));
$handler->override_option('arguments', array(
  'field_privacy_uid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => '',
    'wildcard_substitution' => '',
    'title' => '',
    'default_argument_type' => 'current_user',
    'default_argument' => '',
    'validate_type' => 'php',
    'validate_fail' => 'empty',
    'break_phrase' => 0,
    'not' => 0,
    'id' => 'field_privacy_uid',
    'table' => 'node_data_field_privacy',
    'field' => 'field_privacy_uid',
    'override' => array(
      'button' => 'Übersteuern',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '1',
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'downloads' => 0,
      'page' => 0,
      'story' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(),
    'validate_argument_type' => 'tid',
    'user_argument_type' => 'uid',
    'restrict_user_roles' => 0,
    'user_roles' => array(),
    'validate_argument_php' => 'global $user;
        if (arg(0) == \'node\' && is_numeric(arg(1))) {
          $node = node_load(arg(1));
          if ($user->uid == $node->field_privacy[0][\'uid\']) {
            return TRUE;
          } else {
            return FALSE;
          }
        }',
  ),
));
$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'downloads' => 'downloads',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('empty', 'Es liegen zur Zeit keine Downloads für Sie vor.');
$handler->override_option('empty_format', '1');
$handler->override_option('items_per_page', 5);
$handler->override_option('use_pager', 'mini');
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'downloads2');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
));
Sansui’s picture

Did you ever get this working, with being able to access your CCK field in your views argument validation?

tfranz’s picture

No, i'm sorry .. i dropped this thing after that and looked for other solutions not similar to this ...