Link to home
Start Free TrialLog in
Avatar of kellybelly
kellybellyFlag for United States of America

asked on

Drupal 5.X - block visible based on field and URL

I have created a custom content type where I will enter information that needs to appear in a block in the sidebar on specific pages.  It will contain information about specific brands that our company carries, and needs to show up when the title or URL of the page contains that brand's name.

so what I did was make a field in the custom content called "Brand" and I want to make the content show up in a block when the title or URL contain that string.

Can anyone point me in the right direction past that.  To demonstrate how lost I am - right now that block shows all of the content of the entire site.
Avatar of oliverpolden
oliverpolden
Flag of United Kingdom of Great Britain and Northern Ireland image

This should be quite easy.  You just need to use PHP to break the path into an array and then search the array and title for the brand.

You may need to do some fiddling with the elements of the array and the brand name to make sure they match. e.g. if your path says /content/my-brand-name, and your Brand name is My Brand Name, then you would need to convert the brand name to lowercase and convert spaces to hyphens
<?php
$path = drupal_get_path_alias($_GET[q]); //get URL alias
$path = explode(/, $path) // Split path into an array
$path[] = drupal_get_title(); // Add the title to the array
 
return in_array('[YOUR BRAND NAME]', $path);
 
?>

Open in new window

Some weird characters have come out, they're all single quotes
Avatar of kellybelly

ASKER

is that code for the arguments in the view, or the block visibility?
In the block visibility.  You could modify and put that in the arguments in the view.  Depends what you want to do.  If you want it to appear for just one brand then it's easier to put it in the visibility however you may still have the overhead of the view generating the block on every page even if it doesn't display, not sure about that one.  If you want it to appear different for different brands then yes you would need to add it to the argument which is actually an example I took some code from but it's for Drupal 6, not sure about 5:  http://www.ridgesolutions.ie/index.php/2009/01/19/passing-an-argument-to-a-block-view-in-drupal-6/

My comment is wrong by the way!  
the content type has a field in it called 'field_brand'
if I put the code above in and replaced [YOUR BRAND NAME] with $field_brand should that make it show the quick facts box only when the brand name is present in the URL?
I've got an idea but I'm not sure if you can do it in views 1.  What you could do is create a content type that contains your quick facts.  You could create a taxonomy of brand names that links your node with your quick facts.  Then you can create a relationship between your node and your quick facts based on brand.  An alternative would be to store your quick facts in your node buy creating pre defined values in your content type which you could then select.  You could create a template for this content type which would allow you to position your quick facts, or you might find you can do all you want with CSS.
these all sound like good options - I will have to spend a little time with taxonomy.  What you are suggesting with that was something that had occurred to me also.
It seems like this is probably something that people do all the time - having certain type of content that shows up in a block and the actual node changes based on the page selected.  How would it be handled normally?  (I am trying to make sure that I am not making this more complicated than it has to be.)
ASKER CERTIFIED SOLUTION
Avatar of oliverpolden
oliverpolden
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial