We help IT Professionals succeed at work.

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

kellybelly
kellybelly asked
on
Medium Priority
436 Views
Last Modified: 2013-11-10
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.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT

Commented:
Some weird characters have come out, they're all single quotes

Author

Commented:
is that code for the arguments in the view, or the block visibility?
CERTIFIED EXPERT

Commented:
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!  

Author

Commented:
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?
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
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.)
CERTIFIED EXPERT
Commented:
Yeah it's easy to make things complicated.  Now I've got a better idea of what you're trying to do I would probably do it this way:

Set up your brand taxonomy.  You can then use the path_auto module to make your nodes have a path of something like /brand-name/node-title.  i.e. /[taxonomy-name]/[node-title].  Then you can create a view that generates a block of quick facts and use the trick to get the first part of the address i.e. the taxonomy to use as an argument.

I haven't quite got the time to go into specifics but the path_auto module will help you out anyway. http://drupal.org/project/pathauto

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.