[Drupal] How to wrap two render array elements together?

| | 1 min read

In Drupal 7, when we do customizations we would want the custom module to create content in the form of a render array. We know that a page callback should return a render array which allows the custom module to handle the content as long as possible in the page generation process. Sometimes, wrapping two render array elements together could be an easy way out while dealing with render array manipulations. Here is how we can achieve that.

For example, we had to wrap a mark-up render array and a field-set render array together in one of our custom module's pre-process function. We have used a #container form element to wrap them together with a <div> tag.

// wrap two render array elements together

// Create a container element
$wrapper = array(
  '#type' => 'container',
  '#attributes' => array(
    'class' => array('comment-lite'),
  ),
);
// Add form elements to the container
$wrapper['comment'] = $variables['form']['comments']['#options']['subject']['data'];
$wrapper['comment_edit'] = $variables['form']['comment_edit'];

// Add the container element to the form
$variables['form']['comments']['#options'][$key]['subject']['data'] = $wrapper;

Additionally we can set CSS class which is the one passed in the #attributes property. Check out other Drupal Form API related articles. For clearing your doubts and your valuable suggestions, just type your words here.

 

 

Zyxware is a leading Drupal development company with a team of high expertise in Drupal custom module development services since 2006.