I have a view that shows vehicles in a sidebar block based on the category page the user is currently on. For instance, if the user is on the Bloodmobiles page, bloodmobiles should display in the sidebar. As each new vehicle record is entered, the user selects what category of vehicle it belongs to and what series # it belongs to.
When I first set up the site, I ended up creating a view for each category. I set one of the filters to look for that specific category. But I had to restrict the views to their individual category pages. This meant that I had to have a view for each and every category.
Not the ideal solution but it worked as a temporary measure.
Since then, I have created a sidebar view that uses an argument to determine what vehicles to display. I figured that if I could get the view to take the category from the URL I would only need the one category sidebar block instead of 10+. I tested the view to make sure it was delivering the results I wanted but once I add it to the sidebar block and checked the page that is supposed to display it, nothing shows up.
Here is an example of the old view I created. This is not using an argument but rather is just set to display all vehicles that have Bloodmobiles as the category. This will at least demonstrate what I am trying to achieve.
http://msvehicles.com/category/bloodmobiles
Here is the Export for the Category Sidebar I created that needs the argument passed from the URL:
$view = new view;
$view->name = 'category_sidebar';
$view->description = 'Category Sidebar';
$view->tag = '';
$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('relationships', array(
'field_vehicles_nid' => array(
'label' => '',
'required' => 0,
'delta' => '-1',
'id' => 'field_vehicles_nid',
'table' => 'node_data_field_vehicles',
'field' => 'field_vehicles_nid',
'relationship' => 'none',
),
));
$handler->override_option('fields', array(
'title' => array(
'label' => '',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'target' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'html' => 0,
'strip_tags' => 0,
),
'empty' => '',
'hide_empty' => 0,
'empty_zero' => 0,
'link_to_node' => 1,
'exclude' => 1,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
),
'field_main_vehicle_image_fid' => array(
'label' => '',
'alter' => array(
'alter_text' => 1,
'text' => '<h3>[title]</h3>
[field_main_vehicle_image_fid]',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'target' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'html' => 0,
'strip_tags' => 0,
),
'empty' => '',
'hide_empty' => 0,
'empty_zero' => 0,
'link_to_node' => 1,
'label_type' => 'none',
'format' => 'sidebar_image_linked',
'multiple' => array(
'group' => TRUE,
'multiple_number' => '',
'multiple_from' => '',
'multiple_reversed' => FALSE,
),
'exclude' => 0,
'id' => 'field_main_vehicle_image_fid',
'table' => 'node_data_field_main_vehicle_image',
'field' => 'field_main_vehicle_image_fid',
'relationship' => 'none',
),
));
$handler->override_option('sorts', array(
'title' => array(
'order' => 'DESC',
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'name' => array(
'default_action' => 'not found',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'breadcrumb' => '',
'default_argument_type' => 'fixed',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'glossary' => 0,
'limit' => '0',
'case' => 'lower',
'path_case' => 'lower',
'transform_dash' => 1,
'add_table' => 0,
'require_value' => 0,
'id' => 'name',
'table' => 'term_data',
'field' => 'name',
'validate_user_argument_type' => 'uid',
'validate_user_roles' => array(
'2' => 0,
'3' => 0,
'4' => 0,
),
'relationship' => 'none',
'default_options_div_prefix' => '',
'default_argument_fixed' => '',
'default_argument_user' => 0,
'default_argument_php' => '',
'validate_argument_node_type' => array(
'webform' => 0,
'blog' => 0,
'poll' => 0,
'faq' => 0,
'panel' => 0,
'book' => 0,
'image' => 0,
'page' => 0,
'story' => 0,
'vehicle' => 0,
'vehicle_series' => 0,
'vehicle_type' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'2' => 0,
'3' => 0,
'1' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_transform' => 0,
'validate_user_restrict_roles' => 0,
'validate_argument_php' => '',
'override' => array(
'button' => 'Override',
),
),
));
$handler->override_option('filters', array(
'status' => array(
'operator' => '=',
'value' => '1',
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'status',
'table' => 'node',
'field' => 'status',
'relationship' => 'none',
),
'type' => array(
'operator' => 'in',
'value' => array(
'vehicle' => 'vehicle',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('cache', array(
'type' => 'none',
));
$handler->override_option('items_per_page', 8);
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);
Comments
Click on the Argument,
Click on the Argument, there's a textbox for what to set the title to if the argument is present. Enter "ARG: %1" and see if the title is changed when viewing the block on the main site. If not, it's not getting through.
Usually when this happens to me, it's because I haven't set "Provide Default Argument" -> "Get Node ID from URL" in the Argument settings - that would explain why you can directly pass the arg in the views builder and it works fine, but then it doesn't display on the main site.
HTH.
Orlando, FL Web Developer | http://www.garethsprice.com/
Billy, Can you think about
Billy,
Can you think about this problem a different way? For the specific page you linked to, it looks like just some text and then a standard content list. I might use something like Views + Attachments for this.
Now the URL of the view is /category/% with % being your argument. The content you need could be a node of a different content type (not a vehicle) categorized with the same taxonomy term.
Now you have the view (vehicles, with the URL /category) and the Attachment view (the content, attached to the view, displayed "above").
On your Vehicle pages, it would be a different system.
Honestly, you might want to look at something like Context or Panels in order to get the taxonomy term context you need. We also wrote an article about this years ago - the comments have a lot of useful links to modules.
http://drupaleasy.com/blogs/ryanprice/2008/06/using-views-2-drupal-6-cre...
Ryan Price
DrupalEasy Podcast
I have tried panels before
I have tried panels before with some success. I am going to look through the link and the comments and see if it gets me going in the right direction. Thanks.
I used the Views Attachment
I used the Views Attachment method with some success. The biggest drawback was if I needed to edit the content of the page I had to get to the original node. There is no way (at least that I am aware of) to edit the content when it is part of a view other than to go back to the original node.
The second problem was that not all of the vehicle category pages were showing the vehicles in the list that they were supposed to. I believe that what might have caused that was the way the Taxonomy was set up. I had terms with more than one word or with a backslash in it. So some of the terms would return an empty set when I know they had vehicles assigned to them. I researched online and this seems to be a quirk when using a view in a block. Using PHP as both you and Mike suggested seems to be the cleanest way to do it but with inconsistent results. I had the sidebar showing up with the list of vehicles like it was supposed to for a while but I was unable to get it to work outside of the page display of the view.
I took another look at the DrupalEasy.com link you posted and tried the code at the bottom of the tutorial. I like how that used the termID as opposed to the term itself. Most of the vehicle category nodes only have one term selected but I have 5 -6 that need two. With the code you supplied in that tutorial, does it use both termID's or just the first one that is returned?
I tried a different approach using the views_embed_view() method. I keep getting errors when I test a block with the code in it.
I created a custom block with the following code in it:
<?php
foreach($node->taxonomy as $taxonomy){
$arg = $taxonomy[nid]'
return $arg;
}
print views_embed_view("vt_category_sidebar", "default", $arg) ;
?>
I get this error:
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/msvehicl/public_html/includes/common.inc(1695) : eval()'d code on line 4
Parse error: syntax error, unexpected T_RETURN in /home/msvehicl/public_html/includes/common.inc(1695) : eval()'d code on line 5
If I could get the argument passed consistently with the termID I would use that method in a heartbeat. Is there something I could check to figure out why its not working properly? The only other alternative I have is to use the node-reference field I created to link individual vehicles to the vehicle-type page. I am using that method now. It works perfectly except that I don't have an image to display along with it.
Here is a page using the node reference to generate the list:
http://msvehicles.com/vehicle-type/law-enforcement-homeland-security
Not sure what else I can check to test the argument being passed to the block correctly or if there is a way, using the node-reference, to pull the images from the vehicle record to display along with the link in the sidebar.
What do you suggest?
Alternative code for rendering a view
Hi Pocket Geek,
I'm working on something very similar to what your doing. I need to have my sidebar views magically "know" the content/context of the page they are appearing in so that they can show related information. I actually talked to Ryan a few months ago about this and his recommendation to use Context or Panels is a good one. Either module will help you get the termID argument into your views. I'm using Panel's myself.
The main reason I'm responding, though, is because I had similar problems with the views_embed_view() function. I was receiving some crazy error messages just like you are. I dug around in the Views module and found some code that worked great. Disclaimer: I really don't know if we have experienced the same views error or if this is coincidence, but here is an example based off of your code:
<?php
foreach($node->taxonomy as $taxonomy){
$arg = $taxonomy[nid]
return $arg;
}
$view = views_get_view("vt_category_sidebar");
if (!empty($view)) {
print $view->preview("default", array($arg)); //The arg parameter to this function has to be an array of arguments.
}
?>
Good luck!
--Ed
I tried the code you
I tried the code you suggested but I still got the same error. I wanted to try using a mini panel to place the view. When creating the panel, after you select the View you want to show up in the panel, are there any settings you need to implement in order to get the argument to the View inside the panel?
(in this case the taxonomy termID for the node that is being loaded)
You're missing a semicolon in
You're missing a semicolon in the second line.
Ryan Price
DrupalEasy Podcast
Thanks for catching that for
Thanks for catching that for me. I added the semi-colon but get a warning now:
warning: Invalid argument supplied for foreach() in /home/msvehicl/public_html/includes/common.inc(1695) : eval()'d code on line 2.
If I am reading this right, line 2 would be the line that contains "foreach($node->taxonomy as $taxonomy){". So is it not getting a value for $node->taxonomy?
EDIT:
I have left the error on a page that is not linked to from the rest of the site so you could see it.
http://msvehicles.com/vehicle-type/bookmobiles
Have you done a node_load?
Have you done a node_load? The object that's available may be incomplete.
Ryan Price
DrupalEasy Podcast
I will try that. Thanks for
I will try that. Thanks for all your help today. You guys kept me from mashing my head against the desk. LOL
I was trying to figure out
I was trying to figure out why I was getting the warning so I pared down the code to a simple
<?php print_r($node); ?>But nothing pops up. Which makes me wonder, can you print node variables like that from within a block?
If you look at the code, I
If you look at the code, I load the term ids into an array, then return them imploded with commas between them. In this way, you can get results from several terms.
Ryan Price
DrupalEasy Podcast
Is there any reason why a
Is there any reason why a View would work in preview mode but when I try to see it on the live site it doesn't work? I have had this happen a couple of times and it seems like after a few hours the data will show up. I am wondering if there is something else besides the view cache and the site cache that resets before it will show up. I am using the same set up as I did for the other categories on the last site I was working on.
I gotta tell you, nothing helps you understand views like force feeding them when you are building a site. I have learned a lot but realize that I know just enough to be dangerous at this point.
Here is what I am doing. I created a new version of a blog node ( a custom content type I am calling "article") I am trying to theme the layout by creating a view with the fields I need and have them display in the order I need them.
Simple enough.
I have the view showing the fields in the order I want, rewriting the output to look like I want. I am using the node title as my argument. Under Provide default argument > PHP CodeI have the following:
if(arg(0) == 'article' && arg(1) !=''){return arg(1);
}
Mike helped me with that bit of code earlier and it works. It even works in this case, at least as far as preview goes. What I can't figure out is why it doesn't work when I go to article/number-6-truck (which is an article node I created to test this). I set the path for the page display for the view to article/%1.
So why does it work in preview and not live?
Any ideas?
(At some point I am going to mess with Panels, honest.. I just want to defeat Views before moving on to another Big Boss...)
RESOLVED
As it turns out, I set the argument for the path in correctly. I created a Page display for the view and under "Path" I put "article/%1" thinking that I needed to add the one at the end.
Wrong.
I removed the 1 and tested and the view is showing up.