1

How To Add PHP code to the body of a Drupal 7 Block

I just want to make a button on block body. Is that possible?

this link will explain what i want to do [add button]: http://drupal.org/node/1046700

This article is for Drupal 7 developers who have tried to put PHP code in the body of a Custom Block, but can't get it to run.

  • What do you want the button to do? – Alex Gill Dec 11 '12 at 8:53
  • In that link there is a solution also. – niksmac Dec 12 '12 at 5:57
3

I'm not sure why you need this and you want a similar module of PHP filter which adds a new text format.

If you are familiar with custom moduling and hooks, I would give you a straightforward solution. You could implement this using hook_block_view_alter by defining custom patterns (tokens).

<?php
function mymodule_block_view_alter(&$data, $block) {
    $patterns       = array('[add button]'); # more replacement patterns here
    $replacements   = array('<button type="button">Button</button>');
    if(isset($data['content'])){
        $content = $data['content'];
        if(is_string($content)) {
            $data['content'] = str_replace($patterns, $replacements, $content);
        }
        elseif(is_array($content)) {
        }
    }
}

enter image description here

Here is the result.

enter image description here
You may expand the code as you like such as defining more tokens, adding criteria for certain blocks, user roles, etc. You can even create an administration page at admin/config to define token replacements from UI.

0

Enable the PHP filter in the core modules

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.