4

I have a page callback like this:

function example_page() {
  $build = array();

  $build['#attributes'] = array('class' => array('example-class'));

  $build[] = array(
    '#markup' => '<p>My text here</p>',
  );

  return $build;
 }

As you can see, I have attempted to add classes to the div that wraps the output of the render array in the same fashion as the form API works. I have also tried setting $build['classes'] or $build['classes_array'], but all of these tree syntaxes are ignored, and the content is simply wrapped in <div class="content">, which is not very helpful.

2

#attributes is the correct property, but the problem is that $build itself is not actually an render element (unless make it one), it's just a collection of render elements.

To make it an actual render element, you have to set the #type property to for example 'container'. That is nothing more than a div with attributes that contains other elements. See When should the "container" form element be used? and theme_container().

You can however not affect the mentioned content div, that is outside of your control. Adding a container will add a new div inside that one.

1
  • Setting $build['#type'] = 'container'; seems to make no difference. Was that what you meant? – mikl Jun 28 '11 at 18:23
1

Assuming your module is called 'example', you can do this:

function example_preprocess_node(&$variables, $hook) {
  $variables['content_attributes_array']['class'][] = 'my_class';
}
1
  • Ah, a small correction - actually in your case (custom page) it would be example_preprocess_block(), not node... – Maciej Zgadzaj Jul 9 '11 at 23:54
0

In addition to the answer which Berdir gave you could also look at using #theme_wrappers to wrap the element using a theme function. I think it may have similar results.

<div class="content">

is a pretty standard element in Drupal to define the page content. You may find you have unwanted side affects if you remove it.

1
  • I wasn't aiming to remove it, just add another class to it, to avoid having yet another div-wrapper inside it :) – mikl Jun 28 '11 at 16:10

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.