Search code examples
phpdrupaldrupal-7block

Writing php code to creating menu in block show and alert


I have a menu in a block and this is my code in that block that is in sidebar second:

<?php
  $url=$base_url.'/drupal';
  echo
  '<ul>
  <li><a href="'.$url.'/article/1">One</a></li>
  <li> <a href="'.$url.'/article/2">Two</a></li>
  </ul>';
?>

it is working correctly, but return and red alert:

Notice: Undefined variable: base_url in eval() (line 2 of C:\xampp\htdocs\drupal\modules\php\php.module(80) : eval()'d code).


Solution

  • You will need to bring the variable $base_url and $base_path into the global scope. At the top of your function or template file add:

    global $base_url;
    

    and your code would be like this:

    <?php
      global $base_url;
      $url=$base_url.'/drupal';
      echo
      '<ul>
      <li><a href="'.$url.'/article/1">One</a></li>
      <li> <a href="'.$url.'/article/2">Two</a></li>
      </ul>';
    ?>