2

Is there a difference in the use of hook_preprocess_HOOK()?

In Drupal 8 when I use :

  • hook_preprocess_page
  • hook_preprocess_node
  • hook_preprocess_block

All my Implements for page, node or block work good.

But when I use hook_preprocess_menu some staff don't want to work, what work in preprocess function up.

For example:

This work perfect

function theme_name_preprocess_page(&$variables) {
  $variables['logopath'] = '/' . drupal_get_path('theme','theme_name') . '/logo.png';
}

But this don't work.

function theme_name_preprocess_menu(&$variables, $hook) {
$variables['logopath'] = '/' . drupal_get_path('theme','theme_name') . '/logo.png';
  }
  • What do you mean by this doesn't work? What are you expecting it to do and what do you get? – kiamlaluno Mar 1 '16 at 18:33
  • If you think on my example he print logo.png in page where i write {{logopath}}. Its all work nice on page, node or block but dont work on menu. Its not only example i have some other what dont work on menu. – SakaSerbia Mar 1 '16 at 18:36
  • 2
    Looking your question there is nothing wrong, if doesn't work is because something else is wrong. Are you using the right template for menu (menu.html.twig)? – Vagner Mar 1 '16 at 18:38
  • Yes I use menu--main.html.twig because I have other meny. I write this code <a class="navbar-brand" href="#"><img class="" src="{{ logopath }}" /></a> – SakaSerbia Mar 1 '16 at 18:43
  • That's you doing wrong. Posting an answer. – Vagner Mar 1 '16 at 18:44
3

If you use:

function theme_name_preprocess_menu(&$variables, $hook) {
  $variables['logopath'] = '/' . drupal_get_path('theme','theme_name') . '/logo.png';
}

The hook is menu, so Drupal will look for menu.html.twig, if you define any var inside this preprocess you only be able to use in menu.html.twig. Look the Hook at your theme suggestions comments, if you see 'block' you will need use theme_name_preprocess_block instead.

  • Thank you a lot. New I understend. I learn new staff in Drupal. – SakaSerbia Mar 1 '16 at 18:50
0

I think you need to check the file-suggestion by enabling in services.yml debugging, after that you will find the right Hook name. Then you can use....

function theme_name_preprocess_Hook_name(&$variables, $hook) { $variables['logopath'] = '/' . drupal_get_path('theme','theme_name') . '/logo.png'; }

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.