The following method not returning any value (true or false).
\Drupal::service('path.matcher')->isFrontPage();
How to check if the page is front or not?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThis is indeed a way to check if it is the front page and should return a value if you check the method code.
I am using it in a few preprocess hooks:
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
url.path.is_front
cache context should be added.
#cache
and #attached
on the variables array, see ThemeManager::render()
- if that answers your Q? ie $variables['#cache']['contexts'][] = 'url.path.is_front';
{% if is_front %}
.
here is an example page.html.twig \Drupal::service('path.matcher')->isFrontPage();
@Andy
in the comments to@Kevin
's answer below. Where exactly do you call that snippet from?