I'm currently trying to set up a simple LMS and have already defined a content type course
that contains a term reference field accesstags
, which I would like to use for defining view permissions. I.e. the field accesstags
will contain multiple terms that will be linked to roles that should have access to the node in question.
To make a long story short: I have created a custom module that contains the function hook_entity_view_mode_alter
and I would like to check, whether the term reference field accesstags
contains the term "subscription":
function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
...
if (in_array('subscription', ?????)) {
... allow access ...
}
...
}
However, I'm not sure what to put at the spot that I marked with "?????". I know that I can construct the array of referenced terms by using $entity->get('field_accesstags')->referencedEntities()
... but this array then contains the whole structure, whereas I only need the array of labels.
Is there an easy way for getting this array? Maybe even a built-in function?
Any help would be much appreciated.
$term->getName()
when iterating the array, for example witharray_filter()
.array_map()
in combination with a small helper function that calledgetName()
on the elements of the array built withreferencedEntities()
. Much appreciated. (Would you like to post your comment as an answer so that I can accept it?)