2

From my reading a render array can define a wrapper div by using the '#type'=>'container'. I am inserting a render array in the page_alter hook function in the page_bottom region. It appears that the container type does not work in this case. The code below does not wrap the two #markupelements with the div wrapper but instead outputs the div then the content at the same level.

$page['page_bottom']["notices"] = array(
  '#type' => 'container',
  '#attributes' => array(
    'id' => array('notices'),
  ),
);

$page['page_bottom']["notices"]['notices-placeholder'] = array(
  '#suffix' => "<div id='notices-placeholder'>",
  '#prefix' => '</div>',
);

$page['page_bottom']["notices"]['notices-tab'] = array(
  '#markup' => '<div class="notices-notification-tab">NOTICES &nbsp;<span class="notices-count">10</span></div>',
);

What I want is the following.

<div id="notices">
    <div id="notices-placeholder"></div>
    <div  class="notices-notification-tab">NOTICES....</div>
</div>

What I get is the following.

<div id="notices"></div>
<div id="notices-placeholder"></div>
<div  class="notices-notification-tab">NOTICES....</div>

--- Added I did try #children earlier in the day and it didn't work as well. Before I left for the day I also tried with

$page['page_bottom']["notices"]['notices-placeholder'] = array(
    '#suffix' => "<div id='notices-placeholder'>",
    '#markup'=>"Empty String", // added to see what it would do
    '#prefix' => '</div>',
);

This resulted in a completely out of sync html with the #markup tag being outside the #prefix/#suffix. I don't have the results on me now to post but if you try it you will see the results for your self.

  • You have a bug in your code :) Your #suffix should be #prefix, and your #prefix should be #suffix. Currently you have '#prefix' => '</div>', so this closes your container. It becomes more clear if you use a different html tag for the prefix/suffix stuff, e.g. '<article>'. Then you would get <div id="notices"></article>. – donquixote Dec 15 '17 at 14:16
4

I was able to obtain what you wanted using the following code.

  $page['page_bottom']["notices"] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => array('notices'),
    ),
  );

  $page['page_bottom']["notices"]['notices-placeholder'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => array('notices-placeholder'),
    ),
  );

  $page['page_bottom']["notices"]['notices-tab'] = array(
    '#markup' => '<div class="notices-notification-tab">NOTICES &nbsp;<span class="notices-count">10</span></div>',
  );

The output I obtain using that code as implementation of hook_page_alter() is the following.

<div id="notices"><div id="notices-placeholder"></div><div class="notices-notification-tab">NOTICES &nbsp;<span class="notices-count">10</span></div></div>

I tried using your code, and setting $page['page_bottom']["notices"]['#tree'] to TRUE, or setting $page['page_bottom']["notices"]['notices-placeholder']['#type'] to 'markup', but I always obtained the same result as you.

The only explanation I have is that, using your code, theme_container() (the function defined as theme wrapper for the "container" element) doesn't get any value for $element['#children']. That would explain why the container HTML output doesn't contain any children element, as the code executed by theme_container() is the following one.

  $element = $variables['element'];

  // Special handling for form elements.
  if (isset($element['#array_parents'])) {
    // Assign an html ID.
    if (!isset($element['#attributes']['id'])) {
      $element['#attributes']['id'] = $element['#id'];
    }
    // Add the 'form-wrapper' class.
    $element['#attributes']['class'][] = 'form-wrapper';
  }

  return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';

References

| improve this answer | |
  • Thanks for the info. I did try with #children as well with no luck. I also tried with the $page['page_bottom']["notices"]['notices-placeholder'] = array( '#suffix' => "<div id='notices-placeholder'>", '#prefix' => '</div>', ); – user10956 Nov 14 '12 at 20:05
  • The first snippet I shown works. – kiamlaluno Nov 14 '12 at 20:08
1

Change suffix and prefix

$page['page_bottom']["notices"]['notices-placeholder'] = array( '#prefix' => "", '#suffix' => '', );

| improve this answer | |
  • Your answer is correct. Perhaps express it in a bit more detail :) – donquixote Dec 15 '17 at 14:18
-1

you have to render the $page : return drupal_render($page)

| improve this answer | |

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.