OwnerActivityContext.php in Open Social 8.4
Same filename and directory in other branches
- 8.9 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8.2 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8.3 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8.5 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8.6 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8.7 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 8.8 modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 10.3.x modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 10.0.x modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 10.1.x modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
- 10.2.x modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.php
File
modules/custom/activity_basics/src/Plugin/ActivityContext/OwnerActivityContext.phpView source
<?php
namespace Drupal\activity_basics\Plugin\ActivityContext;
use Drupal\activity_creator\Plugin\ActivityContextBase;
use Drupal\activity_creator\ActivityFactory;
/**
* Provides a 'OwnerActivityContext' activity context.
*
* @ActivityContext(
* id = "owner_activity_context",
* label = @Translation("Owner activity context"),
* )
*/
class OwnerActivityContext extends ActivityContextBase {
/**
* {@inheritdoc}
*/
public function getRecipients(array $data, $last_uid, $limit) {
$recipients = [];
// We only know the context if there is a related object.
if (isset($data['related_object']) && !empty($data['related_object'])) {
$related_entity = ActivityFactory::getActivityRelatedEntity($data);
$allowed_entity_types = [
'node',
'post',
'comment',
];
if (in_array($related_entity['target_type'], $allowed_entity_types)) {
$recipients += $this
->getRecipientOwnerFromEntity($related_entity, $data);
}
}
return $recipients;
}
/**
* Returns owner recipient from entity.
*/
public function getRecipientOwnerFromEntity(array $related_entity, array $data) {
$recipients = [];
$entity_storage = \Drupal::entityTypeManager()
->getStorage($related_entity['target_type']);
$entity = $entity_storage
->load($related_entity['target_id']);
// Don't return recipients if user comments on own content.
$original_related_object = $data['related_object'][0];
if (isset($original_related_object['target_type']) && $original_related_object['target_type'] == 'comment') {
$storage = \Drupal::entityTypeManager()
->getStorage($original_related_object['target_type']);
$original_related_entity = $storage
->load($original_related_object['target_id']);
if (!empty($original_related_entity) && $original_related_entity
->getOwnerId() == $entity
->getOwnerId()) {
return $recipients;
}
}
$recipients[] = [
'target_type' => 'user',
'target_id' => $entity
->getOwnerId(),
];
return $recipients;
}
}
Classes
Name | Description |
---|---|
OwnerActivityContext | Provides a 'OwnerActivityContext' activity context. |