1

I've created a custom entity and a template for it.

path to template /modules/custom/my_module/templates.

My theme_hook()

/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  return [
    'my_module' => [
      'path' => $path . '/templates',
      'template' => 'my-module',
      'variables' => [
        '#my_entity' => '',
      ],
    ],
  ];
}

My preprocess

/**
 * Prepares variables for My Module templates.
 *
 * Default template: my-module.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the user information and any
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_my_template(array &$variables) {
  // Fetch my_entity Entity Object.
  $my_entity = $variables['elements']['#my_entity'];

  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}

But all I get is the user object nothing else.

Did I miss something?

1
  • 1
    The variables don't start with #.
    – Kevin
    Oct 27 '17 at 18:00
2

I needed to remove the #

/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  return [
    'my_module' => [
      'path' => $path . '/templates',
      'template' => 'my-module',
      'variables' => [
        'my_entity' => '',
      ],
    ],
  ];
}

now i have my entity and can work on :)

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.