You are here

class MentionActivityContext in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  2. 8 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  3. 8.2 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  4. 8.3 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  5. 8.4 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  6. 8.5 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  7. 8.6 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  8. 8.8 modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  9. 10.3.x modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  10. 10.0.x modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  11. 10.1.x modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext
  12. 10.2.x modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php \Drupal\social_mentions\Plugin\ActivityContext\MentionActivityContext

Provides a 'MentionActivityContext' activity context.

Plugin annotation


@ActivityContext(
  id = "mention_activity_context",
  label = @Translation("Mention activity context"),
)

Hierarchy

Expanded class hierarchy of MentionActivityContext

File

modules/social_features/social_mentions/src/Plugin/ActivityContext/MentionActivityContext.php, line 16

Namespace

Drupal\social_mentions\Plugin\ActivityContext
View source
class MentionActivityContext extends ActivityContextBase {

  /**
   * {@inheritdoc}
   */
  public function getRecipients(array $data, $last_uid, $limit) {
    $recipients = [];
    $mentions = [];

    // We only know the context if there is a related object.
    if (isset($data['related_object']) && !empty($data['related_object'])) {
      $related_object = $data['related_object'][0];
      $mentions_storage = $this->entityTypeManager
        ->getStorage('mentions');
      if ($related_object['target_type'] === 'mentions') {
        $mentions[] = $mentions_storage
          ->load($related_object['target_id']);
      }
      else {
        $entity_storage = $this->entityTypeManager
          ->getStorage($related_object['target_type']);
        $entity = $entity_storage
          ->load($related_object['target_id']);
        $mentions = $this
          ->getMentionsFromRelatedEntity($entity);
      }
      if (!empty($mentions)) {

        /** @var \Drupal\mentions\MentionsInterface $mention */
        foreach ($mentions as $mention) {
          if (isset($mention->uid)) {
            $uid = $mention
              ->getMentionedUserId();

            // Don't send notifications to myself.
            if ($uid === $data['actor']) {
              continue;
            }
            $entity_storage = $this->entityTypeManager
              ->getStorage($mention
              ->getMentionedEntityTypeId());
            $mentioned_entity = $entity_storage
              ->load($mention
              ->getMentionedEntityId());

            /** @var \Drupal\user\UserInterface $account */
            $account = $mention->uid->entity;
            if ($mentioned_entity
              ->access('view', $account)) {
              $recipients[] = [
                'target_type' => 'user',
                'target_id' => $uid,
              ];
            }
          }
        }
      }
    }
    return $recipients;
  }

  /**
   * Check for valid entity.
   */
  public function isValidEntity(EntityInterface $entity) {
    if ($entity
      ->getEntityTypeId() === 'mentions') {
      return TRUE;
    }

    // Special cases for comments and posts.
    $allowed_content_types = [
      'comment',
    ];
    if (in_array($entity
      ->getEntityTypeId(), $allowed_content_types)) {
      $mentions = $this
        ->getMentionsFromRelatedEntity($entity);
      if (!empty($mentions)) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Get the mentions from the related entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   The mentions.
   */
  public function getMentionsFromRelatedEntity(EntityInterface $entity) {
    if ($entity
      ->getEntityTypeId() === 'comment') {
      if ($entity
        ->hasParentComment()) {
        $entity = $entity
          ->getParentComment();
      }
    }

    // Mention entity can't be loaded at time of new post or comment creation.
    return $this->entityTypeManager
      ->getStorage('mentions')
      ->loadByProperties([
      'entity_type' => $entity
        ->getEntityTypeId(),
      'entity_id' => $entity
        ->id(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActivityContextBase::$entityQuery private property The entity query.
ActivityContextBase::$entityTypeManager protected property The entity type manager.
ActivityContextBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 3
ActivityContextBase::getRecipientsFromPost public function Returns recipients from post.
ActivityContextBase::__construct public function ActivityContextBase constructor. Overrides PluginBase::__construct 3
MentionActivityContext::getMentionsFromRelatedEntity public function Get the mentions from the related entity.
MentionActivityContext::getRecipients public function Returns a batched list of recipients for this context. Overrides ActivityContextBase::getRecipients
MentionActivityContext::isValidEntity public function Check for valid entity. Overrides ActivityContextBase::isValidEntity
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.