Hi,

I have a block that I want to show to the end user on all nodes:

So I enter:
node/* as the path to allow the block to be visible there

However I *do not* want the block to be displayed when I create a node. The path will be something like node/add/story. This matches node/* unfortunately.

Can we use regular expressions here? That might be a way to escape this problem.

Thanks,

Sidharth

Comments

dman’s picture

You'd think that would be simple, but the expression engine being used there is maybe not up to it.
It appears that drupal_match_path() filters out ALL attempts at cleverness - which is hardly for efficiency as it uses patterns anyway. :-(
I'd hope that
node/\d+
would just do it - but no luck, \ is escaped. I can't see the reason why we can't use better patterns there.

You'll have to try php I'm sorry.

// Only show on true node VIEW pages. Not node/add or node/n/edit etc
return (arg(0)=='node' && is_numeric(arg(1)) && empty(arg(2)));

[EDIT] Such a good question, I've added it to the snippets. http://drupal.org/node/321754
See that chapter for more examples.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

sidharth_k’s picture

Thanks for your php code! And thanks for including my question in code snippets. It solves my problem.

(Feature Request)
It would be a good idea here to use Apache style allow, deny syntax (Instead of php or regular expressions).

In Apache allow, deny (as you may be aware) you specify the order of allow and deny. Let us say for the hypothetical drupal 7.x, we could specify order as:

First allow then deny.

So,
Allow: node/*
Deny: node/add/*, node/edit/*

This would solve my problem in a simpler fashion than the php (which is no doubt quite simple but difficult for non-coders).

dman’s picture

A fair suggestion, but the Apache syntax is just as special in its own way. It's yet another thing to learn for someone who doesn't know either. I'd prefer learning re-usable code logic than something unique.

Also would be quite a lot of parsing to happen for every block on every page every time!
My preference is for regular expressions, which are at least handled internally to PHP.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

sidharth_k’s picture

I'm getting wierd errors like:

Fatal error: Can't use function return value in write context in /home/sidk/public_html/reforms/public/includes/common.inc(1547) : eval()'d code on line 3

When I pasted your code... It worked for one block. And its refusing to work for others.

Your code is syntactically correct.

sidharth_k’s picture

Ugh!!

Wonder what is happening. Why does such a simple code snippet not work...?

dman’s picture

May be PHP versions and something about empty() only working on variables
http://nz.php.net/empty
try

(! arg(2))

instead. :-/

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

sidharth_k’s picture

No return keyword?

sidharth_k’s picture

Naa...doesn't work :-( (with and without return keyword)

dman’s picture

<?
return (arg(0)=='node' && is_numeric(arg(1)) && (! arg(2)));
?>
fixes it up for me.
Odd, I never knew empty() had that problem. Ugly error.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

cog.rusty’s picture

I noticed the same problem while I was trying to use isset(). Finally I settled for is_null() which worked fine. (Although I hate how PHP butchers the concept of null from logic.)

Also, I usually prefer ctype_digit(), except in taxonomy terms where I use is_numeric() to allow for arithmetic operators.

sidharth_k’s picture

I've tried doing this many ways... disabling the module...adding the php and then enabling etc.

You always get an error. And it breaks the site. Perhaps you should test this out on your machine... don't want some newbie to break the drupal site by looking at the code snippets :-(

markspall’s picture

Well i have broken my site.....

had something like isset (arg (0)) in my PHP code to determine whether to display a block or not.

Now is breaks with Fatal error: Can't use function return value in write context in ......./includes/common.inc(1547) : eval()'d code on line 2

Where do I find the offending PHP I put in so I can remove it?

It was in the block configuration. the bit where you can display or not depending on how some PHP code evaluates.

Help!

Thanks in advance,

Mark.

markspall’s picture

Found the offending code snippet in the blocks table of the database in pages... deleted it and made visibilty 0 and it's back again now.

Now to work out why that code doesn't work!

WorldFallz’s picture

you've just learned the hard way to always test block code in a page first, lol. Been there done that-- and still do sometimes when I'm "positive" my block code is correct ;-)

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

dman’s picture

I mis-posted ... ignore this...