I am trying to render a block depending on the parameter(entity) present in the requested page. Suppose if I am on a url like this: http://localhost/drupal8/user/1
then a block with label user is created. Same thing I am trying to achieve for other urls like http://localhost/drupal8/node/1
, http://localhost/drupal8/comment/1
. I am using this piece of code:
if ($this->request->attributes->has($entity_type)) {
return code for rendering block
}
(here $entity_type will be replaced by user,comment,node) to check whether requested page has a particular key. This piece of code works for checking keys like node and user when I am on page http://localhost/drupal8/node/1
and http://localhost/drupal8/user/1
respectively but when I am on page like http://localhost/drupal8/comment/1
and check for key: 'comment' then it returns FALSE
because it is not defined. Is this an expected behaviour, if yes then what should I do to solve my problem. Thanks.
comment
exists as a key. It doesn't exist as key hence I am not able to get an instance of comment entity (by using$this->request->attributes->all()
) which I need to render block content. I hope u have understood my problem.$this->request->attributes->get()
).