1

I am implementing hook_menu and I want to render the output of one page callback in a certain region, other than content.

Is there a way of doing this?

2 Answers 2

1

Instead of defining a page via hook_menu it is better to define a block that you can place everywhere across the site.

Is there any requirement that restricts you to using a page?

If not the following code creates a block:

/**
* Implements hook_block_info().
*/
function YOUR_MODULE_block_info() {
  $blocks = array();
  $blocks['YOUR_BLOCK_ABC'] = array(
    'info' => t('YOUR BLOCK NAME'),
  );
  return $blocks;
}

/**
* Implements hook_block_view().
*/
function YOUR_MODULE_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'YOUR_BLOCK_ABC':
      $block['subject'] = '';
      $block['content'] = _YOUR_MODULE_BLOCK_ABC_CONTENT();
      break;
  }
  return $block;
}

function _YOUR_MODULE_BLOCK_ABC_CONTENT() {
  // This is where you code you use for the page goes.
  return t('Hello world');
}

Code from http://kahthong.com/2013/06/create-your-own-custom-drupal-block-programmatically

You can restrict the block visibility in the block settings to only be shown on single pages. Maybe this might solve the problem. To access arguments from the current page you can use the arg function.

Cheers j

3
  • Hey! Well.. I am not restricted to using hook_menu, but I wanted to create something directly on a certain page and I thought that was most straight way of doing it. So there is no way of forcing the output of the page callback to a certain region, eh?
    – Marian
    Commented Feb 18, 2015 at 9:49
  • 1
    Unfortunately I do not know about something like that unless you digg into your theme and check for the path before rendering. Blocks are actually pretty straight forward, too I think ;)
    – schlicki
    Commented Feb 18, 2015 at 9:51
  • without using a hook_menu there's no URL defined, so the block renders on a 404 page, only based on it's visibility settings. has anyone found a workaround for this other than @Marian
    – E.A.T
    Commented Jun 28, 2019 at 0:33
0

I have managed to move the output from the content region to the content_top custom region like this:

function MYMODULE_page_alter(&$page)
{
    list($path) = explode('/', current_path());

    if ('shopping-list' == $path) {
        $page['content']['#region'] = 'content_top';
        $page['content_top'] = $page['content'];
        unset($page['content']);
    }
}

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.