View source
<?php
namespace Drupal\social_follow_taxonomy\Plugin\ActivityContext;
use Drupal\activity_creator\ActivityFactory;
use Drupal\activity_creator\Plugin\ActivityContextBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\Sql\QueryFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FollowTaxonomyActivityContext extends ActivityContextBase {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryFactory $entity_query, EntityTypeManagerInterface $entity_type_manager, ActivityFactory $activity_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_query, $entity_type_manager, $activity_factory);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.query.sql'), $container
->get('entity_type.manager'), $container
->get('activity_creator.activity_factory'), $container
->get('module_handler'));
}
public function getRecipients(array $data, $last_uid, $limit) {
if (!empty($data['actor']) && $data['actor'] === 0) {
return [];
}
$recipients = [];
if (isset($data['related_object']) && !empty($data['related_object'])) {
$related_entity = $this->activityFactory
->getActivityRelatedEntity($data);
if ($related_entity['target_type'] == 'node' || $related_entity['target_type'] == 'post') {
$recipients += $this
->getRecipientsWhoFollowTaxonomy($related_entity, $data);
}
}
return $recipients;
}
public function taxonomyTermsList($entity) {
$term_ids = social_follow_taxonomy_terms_list($entity);
return $term_ids;
}
public function getRecipientsWhoFollowTaxonomy(array $related_entity, array $data) {
$recipients = [];
$entity = $this->entityTypeManager
->getStorage($related_entity['target_type'])
->load($related_entity['target_id']);
if (!empty($entity)) {
$tids = $this
->taxonomyTermsList($entity);
}
if (empty($tids)) {
return [];
}
$storage = $this->entityTypeManager
->getStorage('flagging');
$flaggings = $storage
->loadByProperties([
'flag_id' => 'follow_term',
'entity_type' => 'taxonomy_term',
'entity_id' => $tids,
]);
foreach ($flaggings as $flagging) {
$recipient = $flagging
->getOwner();
if (!$recipient instanceof UserInterface) {
continue;
}
if ($recipient
->isBlocked() || !$recipient
->getLastLoginTime()) {
continue;
}
if ($recipient
->id() !== $entity
->getOwnerId()) {
if (!in_array($recipient
->id(), array_column($recipients, 'target_id'))) {
$recipients[] = [
'target_type' => 'user',
'target_id' => $recipient
->id(),
];
}
}
}
return $recipients;
}
public function isValidEntity(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface) {
return FALSE;
}
switch ($entity
->getEntityTypeId()) {
case 'node':
foreach ($this
->getListOfTagsFields() as $field_name) {
if ($entity
->hasField($field_name)) {
return TRUE;
}
}
return FALSE;
}
return FALSE;
}
public function getListOfTagsFields() {
$fields_to_check = [
'social_tagging',
];
$this->moduleHandler
->alter('social_follow_taxonomy_fields', $fields_to_check);
return $fields_to_check;
}
}