I have a view set up for a page it uses an argument from the url (user name) it lists posts from that user. i would like to use another view to make a block for the /main page that this appears on.

/main/[name]

i created a view and connected it to a block, it appears on the correct page and shows the correct information. Any time I try to restrict the results in the block with teh argument [name] i get no results in the block. I am assuming that argument grabs a value from the url, thats how it seem to work with my page view. any idea why i will not work for my block on that page ?

Comments

nevets’s picture

You need to set the default value for the argument, because it's your own path, you will probably need to use the PHP option and code like

return arg(1);
rosborn’s picture

It's a pity that the "Advanced Help" for the Views module (at least in the version I have installed) doesn't make this clear if it's as easy as you say. Instead, it says that "Block displays have no source of arguments at all; they cannot pull arguments from the URL, and often require use of the default argument PHP code in order to get arguments" but it doesn't give an example of what the PHP code would look like. I've actually never understood why a block can't use arguments. Surely "return arg(1)" is all that the main Views content is using.

squeeky’s picture

i am looking for a place to add this code.

nevets’s picture

Click "Advanced", far right column and add a "Contextual filter".

squeeky’s picture

i can not find it i search Google and the tutorial that I see with the contextual filters does not look anything like my views UI. i am using 7.x-3.0-alpha1 for views.

squeeky’s picture

I removed the username from the URL and switched it to URL. Then set the argument to use default and set it to get UID from the URL and it still did not work. so I need to find some way to get this to work.

nevets’s picture

Since the path is not a system path you probably need to use the "PHP code" option to return the proper argument.

squeeky’s picture

not an option for the version I have. if you could tell my what version would have it i can try

nevets’s picture

Under the "Advanced" section (click to open) you add arguments under "Contextual filters"

For the argument under "When the filter value is NOT in the URL" select "Provide default value". For type you should have "PHP code"

I am using views 7.x-3.x-dev (though I am pretty sure it is there for the alpha release), you may need the "PHP filter" enabled.

squeeky’s picture

what will happen if i delete my views mod and install the one you have? i have no advanced tab

Relationships
None defined
RearrangeAdd
Arguments
None defined
RearrangeAdd
Sort criteria
RearrangeAdd
Filters

that is everything in the left hand side

nevets’s picture

That version has the older ui, you add arguments under "arguments".

squeeky’s picture

and it has no php option, can i upgrade without loosing my views

squeeky’s picture

i will try with the new versions. got it installed i like the old UI better this is going to take me some time.

1800collect’s picture

Is this new for Drupal 7? I could have sworn a block would have just known what page it was on and be able to get it from the URL. But it seems now we need to inject PHP to make it work?

I was banging my head against this for an hour! Finally my google-fu brought me to this page. Thank you nevets for clarifying. For me it said "WHEN THE FILTER VALUE IS NOT AVAILABLE" I also used the Content: NID to filter per the node.

nevets’s picture

This thread and the suggestion to use PHP is based on the block being on a custom page/path. For paths like node/{nid} and user/{uid} there are options to pull the default value from the path.

m_i_c_h_a_e_l’s picture

I suggest using the User: Current filter instead of an argument.

That way, anyone who visits the view page (or block) will see their own posts. No need to fool around with the url.

Your filters should be:

Node: Type whatever
Node: Published yes
User: Current yes

Any any other filters you want.

squeeky’s picture

the information is not being sorted by the current logged in user, it is sorted by the author of the information you are viewing. the main view is sent an argument through the url by clicking a link with teh users name.

m_i_c_h_a_e_l’s picture

One more thing.

I notice that you said "I would like to use another view to make a block".

Maybe you already know this, but you don't need to create a second view for this.

Create the default view, then add a page display, a block display, etc. Only one view, but different ways to display it. Just remember to do all your editing in the default display.

Just in case you didn't know that.

squeeky’s picture

the view is displaying different content types from teh same user as the page view. page view is using one content type while the block is using another.