I have a view that requires a nid as an argument.
When the argument is not provided, I'm using custom php code to set it up.

My code:

if (isnumeric(arg(4)){
  $id = arg(4);
}
elseif (arg(0) == 'node' && isnumeric(arg(1))){
  $id = _myModule_custom_function(arg(1));
}
return $id;

Explanation:
I use this view on 2 pages, on the first one the nid is in arg(4), on the second page, it is a different node and the custom function finds the appropriate nid for the view.

My question:
can I use custom functions in this php block to determine the return variable?
If so, anyone sees an error in my code? (the custom function returns the right nid)

Comments

merlinofchaos’s picture

You can use custom functions, but if you're going to do this you probably should just create a full plugin and not split your code up like this. It's only a little extra work to create a plugin and I think will be worth it.

thijsvdanker’s picture

Thanks works like a charm!

As this is part of the todo on the advanced help page for views I figured it might be worth sharing my solution.

in myModule_views.inc:

function myModule_views_plugins() {
  $plugins = array(
    'argument default' => array(
      'node' => array(
        'title' => t('Argument Title'),
        'handler' => 'myModule_plugin_argument_default_node',
        'path' => drupal_get_path('module', 'myModule') . '/views', // not necessary for most modules
        'parent' => 'fixed', // so that the parent class is included
      ),
  ),
  );
  return $plugins;
}

in myModule_plugin_argument_default_node.inc

class myModule_plugin_argument_default_node extends views_plugin_argument_default {
  function argument_form(&$form, &$form_state) {
  }

  function get_argument() {
    if (is_numeric(arg(4))) {
      $id = arg(4);
    }
    elseif (arg(0) == 'node' && is_numeric(arg(1))) {
      $node = node_load(arg(1));
      if($node->type == 'customType) {
          $id = _myModule_custom_function(arg(1));
      }
    }
    else {
        $id = 0; 
      }
    return $id;
  }
}
dawehner’s picture

Status: Active » Fixed

so thx! for providing your solutionn.

this can be marked as fixed now!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.