public function Subscribers::getBasicContext in Message Subscribe 8
Get context from a given entity type.
This is a naive implementation, which extracts context from an entity. For example, given a node we extract the node author and related taxonomy terms.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity object.
bool $skip_detailed_context: (optional) Skip detailed context detection and just use entity ID/type. Defaults to FALSE.
array $context: (optional) The starting context array to modify.
Return value
array Array keyed with the entity type and array of entity IDs as the value.
Overrides SubscribersInterface::getBasicContext
2 calls to Subscribers::getBasicContext()
- Subscribers::getSubscribers in src/
Subscribers.php - Retrieve a list of subscribers for a given entity.
- Subscribers::sendMessage in src/
Subscribers.php - Process a message and send to subscribed users.
File
- src/
Subscribers.php, line 403
Class
- Subscribers
- A message subscribers service.
Namespace
Drupal\message_subscribeCode
public function getBasicContext(EntityInterface $entity, $skip_detailed_context = FALSE, array $context = []) {
if (empty($context)) {
$id = $entity
->id();
$context[$entity
->getEntityTypeId()][$id] = $id;
}
if ($skip_detailed_context) {
return $context;
}
$context += [
'node' => [],
'user' => [],
'taxonomy_term' => [],
];
// Default context for comments.
if ($entity instanceof CommentInterface) {
$context['node'][$entity
->getCommentedEntityId()] = $entity
->getCommentedEntityId();
$context['user'][$entity
->getOwnerId()] = $entity
->getOwnerId();
}
if (empty($context['node'])) {
return $context;
}
/** @var \Drupal\node\NodeInterface[] $nodes */
$nodes = $this->entityTypeManager
->getStorage('node')
->loadMultiple($context['node']);
if ($this->moduleHandler
->moduleExists('og')) {
// Iterate over existing nodes to extract the related groups.
foreach ($nodes as $node) {
foreach ($this->membershipManager
->getGroupIds($node) as $group_type => $gids) {
foreach ($gids as $gid) {
$context[$group_type][$gid] = $gid;
}
}
}
// Re-load nodes as the OG context may have added additional ones.
/** @var \Drupal\node\NodeInterface[] $nodes */
$nodes = $this->entityTypeManager
->getStorage('node')
->loadMultiple($context['node']);
}
foreach ($nodes as $node) {
$context['user'][$node
->getOwnerId()] = $node
->getOwnerId();
if ($this->moduleHandler
->moduleExists('taxonomy')) {
// Iterate over all taxonomy term reference fields, or entity-reference
// fields that reference terms.
foreach ($node
->getFieldDefinitions() as $field) {
if ($field
->getType() != 'entity_reference' || $field
->getSetting('target_type') != 'taxonomy_term') {
// Not an entity reference field or not referencing a taxonomy term.
continue;
}
// Add referenced terms.
foreach ($node
->get($field
->getName()) as $tid) {
$context['taxonomy_term'][$tid->target_id] = $tid->target_id;
}
}
}
}
return $context;
}