1

I am using PHP code in a block with the PHP filter module. I am attempting to render an image within this block and the image rendered should be dynamically determined and specific to the content being shown in the content block. I was thinking I could use something like the node id or title of the page but so far I have been unable to use $title or $node->id.

So far my proof of concept with this is limited to being able to determine the site name or site slogan which is not specific to the node (and therefore doesn't help). This is what the code in my block looks like now. If I can figure out a way to replace $site_name with something useful, I will put that at the top (instead of the the $gbhe variable) and use it to determine which image to show.

  <?php 
  $gbhe = true;
  if ($gbhe) {
    $src = "http://localhost:8888/drupal-7/sites/this_site/files/SIDEBAR_IMAGE-P2_07.gif";
  } elseif (!$gbhe) {
    $src = "some_other_image_url_TBD";
  }
?>
<p><img alt="gbhe" src="<?php print $src; ?>" /></p>
<?php 
  $site_name = variable_get('site_name');
  echo $site_name;
  // $my_vars = get_defined_vars();
  // print_r($my_vars);
?>
  • 2
    To get to the node you can use menu_get_object – J. Reynolds Oct 4 '15 at 5:22
  • As side note, the if() statement is overly complicated. $src = ($gbhe ? 'http://' : 'some_other_image_url_TBD'): – kiamlaluno Oct 4 '15 at 5:49
  • 1
    As for not being able to use $node->id, the PHP filter module doesn't pass any preset variable to the PHP code it runs, so $node is not available, and it would not even be available as global variable to a module. Plus, the node ID is $node->nid, assuming you initialised a $node variable. – kiamlaluno Oct 4 '15 at 5:54
  • Thanks especially to @J. Reynolds. I got this to work using menu_get_object() and the node id ($node->nid) being careful to test first that there is a node using isset(). – jbchurchill Oct 28 '15 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.