Skip to content

Instantly share code, notes, and snippets.

@Niklan
Last active April 4, 2018 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niklan/05bed3a0b96bda2d150ec72492d4eb16 to your computer and use it in GitHub Desktop.
Save Niklan/05bed3a0b96bda2d150ec72492d4eb16 to your computer and use it in GitHub Desktop.
Drupal 8 detect if there entity on page
$parameters = \Drupal::routeMatch()->getParameters();
foreach ($parameters as $parameter) {
if ($parameter instanceof EntityInterface) {
// Entity page.
}
}
@Chi-teck
Copy link

Chi-teck commented Apr 4, 2018

$current_url = Url::fromRoute('<current>')->toString();
$parameters = \Drupal::routeMatch()->getParameters();
foreach ($parameters as $parameter) {
  if ($parameter instanceof EntityInterface && $parameter->toUrl()->toString() == $current_url) {
    $is_canonical_entity_page = TRUE;
    break;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment