0

I'm trying to make a custom module based on menu module to render a sitemap.

I want to use a custom theme function for rendering it but for unknown reasons my variables are not passed to the theme function. Here is my code (simplified so we get what matter) :

function custom_module_theme() {
  return array(
    'custom_module_link' => array(
      'variables' => array(
       'attributes' => NULL,
       'title' => NULL,
       'href' => NULL,
       'below' => NULL,
       'original_link' => NULL,
      ),
    ),
  );
}

function theme_custom_module_link(array $variables) {
 $element = $variables;
 $output = ...;
 ...
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . "</li>\n";
}

function _custom_module_tree_output($tree) {
    ...
    ...
    // Allow menu-specific theme overrides.
    $element['#theme'] = 'custom_module_link';
    $element['#attributes']['class'] = $class;
    $element['#title'] = $data['link']['link_title'];
    $element['#href'] = $data['link']['link_path'];
    $element['#original_link'] = $data['link'];
  }
 return $element;
}

function custom_module_page() {
  $tree = ...
  $output = _custom_module_tree_output($tree);

  return drupal_render($output);
}

This code return a error page with the following : TypeError: Argument 1 passed to drupal_attributes() must be of the type array, null given ...

I don't understand why drupal_render doesn't iterate over the $element['#theme'] call to fetch variables as described in the documentation. What I'm missing ?

From the doc : Basically, the '#theme' = 'function_name' calls theme_function_name(), and other array values of the form '#var_name' = $value in the same array are passed as arguments to the theme function

If I dpm $variables in my theme_custom_module_link function, variables are set as NULL as in my hook_theme ...

1
  • 1
    You missed template or render element in the hook_theme definition - one of them is required
    – Clive
    Sep 28, 2018 at 13:06

1 Answer 1

0

What I wanted to do is to get the html through a function and not a template. So actually there is no need to set the variables, just the 'render element' did the trick.

function custom_module_theme() {
  return array(
    'custom_module_link' => array(
      'render element' => 'element',
    ),
  );
}

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.