I'm trying to do this with the story node type.

www.example.com/story should list stories submitted by all users

www.example.com/story/uid should list story by the uid

Can I achieve both using a single view rather than creating two separate views??

Thanks!

Comments

simul’s picture

views create a web page (in your case) and each web page should have a unique url, which makes it impossible to create one page for displaying different information
hth

As If’s picture

...if you use the arguments feature. The uid would be the arg.

See here:
http://drupal.org/node/54455

And here if you wanna get tricky:
http://drupal.org/node/70145

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

drupalzack’s picture

Thanks 'As If'. I tried using argument handling, the problem is the argument handling code is executed only *if* an argument is provided.

So this will work:

example.com/story/all
example.com/story/5

But this will not work

example.com/story

If argument handling worked even if no args are supplied then I can do an if check for no args and assign args[0]='all' to get a list of all stories by all users

The other solution is to map story to /story/all ... I'll see how that goes! :) Meanwhile if others have any ideas, do share! :)

As If’s picture

In the edit view page, make the URL like this:
story/$arg

Then in the argument handling code, put this:

if (!$args[1]) {
  $args[1] = 'all';
}
return $args;

It's args[1] you want to set, not args[0]. args[0] is "story".

LVX
TF
-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

drupalzack’s picture

That didn't work either. The arguments code only gets executed when the view detects an argument.

www.example.com/4 works
www.example.com/all works (of course I've setup all as wildcard)

www.example.com doesn't, since the arguments code is not even touched.

Is my views module broken, is this a bug or is this the default behaviour?

zoon_unit’s picture

Look at the "default" drop down box in arguments. It allows you to set the behavior of the argument when no argument value is passed. It defaults to "Return Page Not Found," but you can easily change it to "Return All Values."

drupalzack’s picture

thanks for taking the time to reply. I tried that too... here's my view

  $view = new stdClass();
  $view->name = 'test';
  $view->description = '';
  $view->access = array (
  0 => '1',
  1 => '2',
  2 => '3',
);
  $view->view_args_php = '$args[1]='all';
return $args;';
  $view->page = TRUE;
  $view->page_title = 'test';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'teaser';
  $view->url = 'test/$arg';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->sort = array (
  );
  $view->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '2',
      'title' => '%1's stories',
      'options' => '',
      'wildcard' => 'all',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'storylink',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node);
  $views[$view->name] = $view;

for this test I'm forcing args[1] to always return 'all'. that part of the arg. code never gets executed if the url doesn't have a parameter. But if I type in exampe.com/test/8 then the arg code gets executed. without the paramete I always get 'page not found' even after changing the type to return all rows.

As If’s picture

Zack, I did some testing and this general approach works on my 5.1 installation. HOWEVER, assigning the $arg in the URL field (of the edit view page) was problematic, as you have discovered. I have found 2 ways to get around this:

  • create an URL alias for the URL without any arguments, then you can send it anywhere you want (probably to "test/all")
  • or REMOVE the "/$arg" from the URL field. This prevents the arg from being required, but the argument handler still does its thing.
  • Other stuff...
    I also changed my argument handler so I could filter by multiple taxonomy terms (up to 4). In your case this might be something like "stories by userID" AND "murder mysteries" or whatever. Of course I also had to set up an argument (Taxonomy: Term ID) for each of those terms. It works even if some args are missing, it works with no args at all. Most interestingly, it doesn't even care what order the args are typed in. For instance, I didn't expect this URL to work, but it did... example.com/story/3///18

    If you need this kind of capability, do something like this to your arg handler. (I used * instead of all but I really doubt that makes any difference)...

    $i = 0;
    while($i < 4) {
      if (!$args[$i]) {
        $args[$i] = '*';
      }
      $i++;
    }  
    return $args;
    

    It works. Don't give up.

    -------------------------------------------
    Interactive Worlds and Immersive Obsessions
    http://www.asifproductions.com

    -------------------------------------------
    Interactive Worlds and Immersive Obsessions
    http://www.asifproductions.com

    drupalzack’s picture

    Thank you 'As if'! that was a very detailed explanation and gives a very clear understanding of args with views. It is because of users like you, Drupal is such an exciting platform to work on.

    Your experiment with $args is very interesting. Using aliases will be taking the easy way out! :) I'll use views without explicitly specifying $arg. That should solve my problem.