To add image in block by selecting “PHP code” as the Input format in Drupal 7

To add image in block by selecting “PHP code” as the input format the following steps can be followed.

Step 1. Enable PHP filter module.

Administration > Modules > PHP filter

Note: Fresh install of Drupal 7 has PHP Filter module disabled by default. Ensure your role has permission to enable this module.

Step 2 . If we need to add image from theme, we can use the Drupal function drupal_get_path to get the correct path.

Place the below code in Block body.

<?php
$thempath= drupal_get_path('theme', 'themename');
$image1 = array( 'path' => $thempath . '/images/image1.png',  'alt' => 'image alt text',  );
print "<a target='_blank' href='http://www.test.com' >". theme('image', $image1) . "</a>";
?>

Note: ‘themename’ is the name of your theme.

Step 3. Select ‘PHP Code’ option from the Text Format dropdown list, and save.

This entry was posted in Drupal Developer Notes. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *