let the arguments section of a view have some php that runs and optionally returns an aray of arguments. this is helpful for blocks which otherwise have no way to acept arguments. might be useful for views of type 'page'. not sure.

this example is for a block which shows 'other recent '. will be sensitive to the type of node thats shown.
$node = node_load(arg(1));
return array($node->type));

Comments

eaton’s picture

Title: php code feeds arguments » Views: provide a place for php snippets to feed block args
Assigned: Unassigned » eaton

I need this for a client's site, and the more that can be done with views the better. I'll take a stab at it over the next day or two.

eaton’s picture

Status: Active » Needs review
StatusFileSize
new5.76 KB

Annnnd here's the patch. It adds a new db column for each view (block_args), and thus requires a trip to /update.php. A PHP snippet in that field should return an array of args for the view to use -- it will allow the block to display properly, and will also relay those args along in its [more] link if said link is enabled.

I had to poke around in parts of views.module that I'm unfamiliar with, though the changes aren't too drastic. Review would be much appreciated.

eaton’s picture

Title: Views: provide a place for php snippets to feed block args » Views: provide a place for php snippets to alter args
StatusFileSize
new6.42 KB

Three cheers for feature creep! The re-rolled version of this patch includes a couple tweaks:

  1. No longer block-specific. The view_args_php property now stores a snippet of PHP that can rewrite the view arguments in any mode -- embedded, page, block, etc. The snippet has access to the surrounding variables that it's executed with, so it can check if ($type == 'block'), or return an unmolested copy of the $args collection in cases where it doesn't need to do anything special.
  2. The php field is wrapped in a 'use PHP to control block visibility' access check.
  3. Uses output buffering when eval'ing to avoid ugliness.
  4. Properly updates pgsql databases.

The 'Argument Handling Code' field is now in its own formgroup, at the bottom of the form. I'd add it to the Arguments section, but I couldn't figure out how to do that given the crafty, funky ways that views_ui builds the fields/filters/sorts/arguments sections of its form. The functionality is working cleanly as far as I can tell.

eaton’s picture

StatusFileSize
new6.87 KB

Whoops. Export wasn't picking up view_args_php. This fixes it.

An example view that demonstrates how it can be done:

  $view = new stdClass();
  $view->name = 'test block';
  $view->description = 'blocky block testing';
  $view->access = array ();
  $view->view_args_php = 'if ($type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  $args[] = $node->uid;
}
return $args;';
  $view->page = TRUE;
  $view->page_title = 'Block Test';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_type = 'teaser';
  $view->url = 'blocktest';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '20';
  $view->block = TRUE;
  $view->block_title = 'Block Test';
  $view->block_header = '';
  $view->block_header_format = '1';
  $view->block_type = 'list';
  $view->nodes_per_block = '5';
  $view->block_more = '1';
  $view->block_use_page_header = FALSE;
  $view->sort = array (
  );
  $view->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '1',
      'title' => '',
      'options' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink',
    ),
  );
  $view->filter = array (
  );
  $view->requires = array(node);
  $views[$view->name] = $view;
moshe weitzman’s picture

views_args_php needs some kind of escaping on export. if you look at your paste, you see that it isn't valid php because of the ''

eaton’s picture

Is there a standard way to do this in views? I double-checked, and the page_header and page_footer fields suffer from the same problem. Putting single-quotes in them yields malformed PHP...

I suppose the fields could be serialized...

merlinofchaos’s picture

I think check_plain ought to be fine. I think I'd identified that once but must've forgot to fix it, and then thought I had.

eaton’s picture

StatusFileSize
new10.13 KB

New version of the patch. Uses db_escape_string, and also includes the footer and empty fields that were recently added.

merlinofchaos’s picture

Status: Needs review » Fixed

Committed. I likely didn't adequately test this, but the code looks pretty solid.

Anonymous’s picture

Status: Fixed » Closed (fixed)