I see that in /block/configure/ there is a setting for "Page specific visibility settings" that says: Show if the following PHP code returns TRUE (PHP-mode, experts only).

Are there any examples of this? Any practical applications that someone can share?

I have a need where this might come in handy. I have a block that I want to appear on SOME /taxonomy/term pages but not others - based on if the term is part of "taxonomy X" or not.

That is, I want to be able to say "Show this block only if the page is displaying items from a specific taxonomy".

I do not want to have to use the "Show on only the listed pages" and then list a bunch of taxonomy/term/XX lines. If I add a new taxonomy item, then I have to remember to update my block config, and I want to avoid that.

Comments

LukeLast’s picture

Here is an example of some code I used in a block so the block would only be visible to me.

<?php
global $user;
return $user->uid == 1;
?>
ericdfields’s picture

What is the simplest way I can do an if is admin in this area?
--
eric

jeffrey.dalton’s picture

I just posted this http://drupal.org/node/68033 and then found this post. I am looking for this exact same functionality in drupal 4.7. Can anyone give an example of the code needed to check the taxonomy term and get the true statement back?

Jeffrey Dalton
Creative Director & Founding Partner
BKJ Digital
https://bkjdigital.com

rivena’s picture

This is not exactly what you want, but there are some block visibility php snippets in the handbook:
http://drupal.org/node/60317

Anisa.

-----------------------------------------------------------
Kindness builds stronger bonds than necessity.

www.animecards.org - 16,000 card scans and counting!
-----------------------------------------------------------

jeffrey.dalton’s picture

This is what I ended up implimenting. I am no PHP wiz so if someone can improve on this it could develop into a very handy snippet to be added to the snippet directory.

My understanding of what is happening is I grab arg(1) which is the node ID number and run it through the taxonomy_node_get_terms() function which returns and array of information on the nodes terms. Then I grab one of the the array items called 'name' and check its value against the term I want to stick this menu to... OR I check the url structure to see if I am on a 'axonomy/term/x' page and I check the x value to see if it is my taxonomy term value which in this case is 1. Everything else returns false. This will probably become a switch statement so I can list multiple terms to check against.

Hope this saves someone some time. Any improvements are most welcome.

<?php
$myNodesID = arg(1);
$taxArray=taxonomy_node_get_terms($myNodesID);

if (($taxArray[1]->name == "News") || (arg(0) == "taxonomy" && arg(1) == "term" && arg(2) == 1))
{
return TRUE;
}
else {
return FALSE;
}
?>

Jeffrey Dalton
Creative Director & Founding Partner
BKJ Digital
https://bkjdigital.com

sadeh’s picture

I have created a new custom content type and also a new custom block that I want to show on every pages that has that particular content type.
So I need to define a php code in “Show block on specific pages” section of block detail page, and when the result was TRUE I should see the block on those pages.

I need to check the content type and see if it is equal to what I want, I did something like this:

<?php

    return $node->type == ‘my_custom_content_type’;

 ?>

But it doesn’t work. Looks like the $node is not available there. Is there any other function to call to get the type of node that this block is on?
Any help would be appreciated.

lias’s picture

I would like to know this as well. There is a block snippet that loads the block if it's a certain content type and node ID:

// Only show block from types array and nodes array
$match = FALSE;
// Which node types
$types = array('page' => 1);
// Which nodes (by nid)
$nodes = array(1, 2);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  if (isset($types[$type]) && in_array($nid, $nodes)) {
        $match = TRUE;
  }
}
return $match;

I just can't figure out how to remove node ID so it displays block on content type only regardless of node ID.

WeRockYourWeb.com’s picture

Hey guys,

I used <?php if ($node->type == 'page') ?> to only display theme elements on pages (ie. so they don't show up in admin/ user/ 404/ other content types, etc.) and it works fine (Drupal 5.3). I noticed that your single quotes were a different char encoding? Try using the ascii encoding above (non-slanted single quote).

Alex
----------
Contract Web Development, Inc.

nsyll’s picture

you need just to call $node as a global

  global $node; 
  if ($node->type == 'page'){
    return TRUE;  
  } 

the Greek Drupal community mydrupal.gr

manish.p’s picture

============ Show if the following PHP code returns TRUE (PHP-mode, experts only). ================

Hi !!
I have simply change on file tinymce.module function theme_tinymce_theme like this way :::

Step 1 -

function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
$splittext_name = explode("##",$textarea_name);
//echo $textexplode[1];
if($splittext_name[1]=="tinymce-editor")
{
switch ($textarea_name) {
// Disable tinymce for these textareas
case 'log': // book and page log
case 'img_ .................
......................................

return $init;
}
}

Step 2 :- Upload file on server.
Step 3 :- Give control name as DESCRIPTION##tinymce_editor.

Step 4:- And select "Show if the following PHP code returns TRUE (PHP-mode, experts only)." option.(Optional)
with out code
.

And its working cool .. ...

Warm Regards
Manish Painuly.

asinnema’s picture

I have drupal 6 and I want to only show a the authored by block on blog pages. I have tried the suggestions from the above comments but no luck - has something changed in drupal 6? And does anyone have any suggestions otherwise? :)

FrontBurner’s picture

<?php
  if(arg(0) == 'node')
  {
      $nid = arg(1);
      $node = node_load(array('nid' => $nid));
      if ($node->type == 'content_type') 
      {
         return TRUE;
      }
  } 
  return FALSE;
?>
Julien Verkest’s picture

Thanks, your code work also on Drupal 5.

I add language condition with i18n module.

<?php
  if(arg(0) == 'node')
  {
      $nid = arg(1);
      $node = node_load(array('nid' => $nid));
      if ($node->type == 'sejours' && i18n_get_lang()=='fr')
      {
         return TRUE;
      }
  }
  return FALSE;
?>
doublejosh’s picture

This seems URL dependent, which is exactly what I was trying to avoid by actually checking the node->$type rather than just rely on the URL to be right via something like pathauto. Also, seems pretty overhead expensive to do an extra node load just to check a block.

aubjr_drupal’s picture

This worked like a charm in Drupal 6 as well.

thoughtcat’s picture

Hi,

I've tried adapting that suggested code to only show a block on nodes on my Drupal 5 site where the value of a field called "town/city" is "London". I've used Content Templates to identify the field containing the required value and have put the code below into the "Show if the following PHP code returns TRUE" box in the block config, but the block is not displaying:

<?php
  
      if ($node->field_town_city_0[0]['view'] == 'London') 
      {
         return TRUE;
      }
 
  return FALSE;
?>

I've tried different versions of "London" e.g. "london", "LONDON" etc but no dice.

Any ideas?

naisanzaa’s picture

What would the php code for hiding a block when not a public user? Thanks

-Eric

edario’s picture

Hi - I'd like to know that as well =)