0

I am creating a module for wich i need to create a block with php code. I`m making it as follow:

function my_modulo_block_info() {
   $blocks = array();
   $blocks['start'] = array(
      'title' => 'Start',
      'info' => t('The start block'),
      'status' => TRUE,
      'region' => 'content',
   );
   return $blocks;
}

function my_modulo_block_view($delta='') {
   $block = array();
   switch($delta) {
      case 'start' :
         $block['content'] = '<?php echo "Hello world"; ?>';
         break;
   }
   return $block;
}

But i see only the block name on the page, it dont print Hello world. Any body know why?

8
  • Do you actually want <?php echo "Hello world"; ?> or do you want 'Hello World' to display? If you want the php, I am wondering if it is in your html but you don't see it in the browser because of the <> maybe try using &lt; and &gt; or if you do just want 'Hello World' then change the line to $block['content'] = 'Hello world';
    – rjl
    May 12, 2017 at 1:31
  • I really need the php code, "Hello world" is only an example for undeerstand. Wath are &lt and &gt ?
    – Dyn
    May 12, 2017 at 1:35
  • They are character codes link. &lt; = < and &gt; = > You might also use the php function highlight_string() link
    – rjl
    May 12, 2017 at 1:39
  • That is not the problem, it seems that the block does not recognize php code. I do not know how to define the block that recognizes php code, just like full html and plain text format on configure block pages.
    – Dyn
    May 12, 2017 at 1:50
  • Sorry, I don't understand what you mean by "recognizes php code." What to you actually want to see in the browser? The $block['content'] variable is for the HTML content you want to display.
    – rjl
    May 12, 2017 at 2:03

1 Answer 1

2

call a php function to built block-content, in hook_view code should be:

//construct your output as a function    
$block['content'] = myfunctionname(); // this function is called 3 lines below
....
return $block;
//then call your output content function
function myfunctionname() {
$output = 'whatever you want(arrays, functions, html, more php, ....)'
 };
 return $output;

that will return $output as your content.

1
  • It's preferable to return a render array as the content, but this is the right idea
    – Clive
    May 12, 2017 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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