2

I am trying to get my head around the drupal 7 render() function but there is something I can't get straight.

The data source that I want to render looks like this:

Array
(
    [id] => 3315539862007
    [name] => Some Name
    [picture] => some-facebook-image.jpg
    [link] => http://www.facebook.com/some-page

I pass it to a $block['content'] like this (I also tried changing '#children' to '#element', but with no success):

$block['content'] = array(
  '#theme' => 'opengraph_company',
  '#children' => $data
);

but inside my custom theming function the data that is passed looks really weird

function theme_opengraph_company($variables) {
  $element = $variables['element'];

  return 'hello';
}

if I debug the $variables inside the theming function I get something like this

Array
(
    [element] => Array
        (
            [#theme] => opengraph_company
            [#children] => Array
                (
                    [id] => 3315539862007
                    [name] => Some Name

which to me looks really weird. The MYMODULE_theme function looks like this

function opengraph_theme() {
  return array(
      'opengraph_company' => array(
      'render element' => 'element',
    ),
  );
}

I would expect access to the data based to the theming function to easier, something like

function theme_opengraph_company($variables) {
  $element = $variables['element'];

  return $element['name'];
}

Am I passing the parameters wrong to the theming function?

4

When you use render element, the element which you apply the theme to is passed into the theme function in the $vars array...the index in the array will be whatever you've set render element to in your theme function, which in this case is element; hence the element you've asked the theme API to pass in is available in $vars['element'], which is a carbon copy of the original render element.

To get the effect you're looking for you would normally use variables instead of render element:

function opengraph_theme() {
  return array(
    'opengraph_company' => array(
      'variables' => array(
        'id' => 0,
        'name' => '',
        'picture' => '',
        'link' => ''
      ),
    ),
  );
}

Then you'd build your render element like this:

$block['content'] = array(
  '#theme' => 'opengraph_company',
  '#id' => 123,
  '#name' => 'Name',
  '#picture' => 'Picture',
  '#link' => 'Link'
);

Or, if you want to use your current $data array, you can pass it directly to the theme function:

$block['content'] = array(
  '#markup' => theme('opengraph_company', $data)
);

Bear in mind with that second method any modules whose hooks might be run after yours will no longer get the chance to alter the theme output, as you're converting it down to a string.

Using either of those methods though, when your theme function is run you will have access to the variables like this:

function theme_opengraph_company(&$vars) {
  $id = $vars['id'];
  $name = $vars['name'];
  // etc...
}

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.