You are here

class DefaultProfileLoad in Open Social 10.1.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_profile/src/Plugin/GraphQL/DataProducer/DefaultProfileLoad.php \Drupal\social_profile\Plugin\GraphQL\DataProducer\DefaultProfileLoad

Loads the default profile for a user.

On many Open Social platforms there is only one profile.

Plugin annotation


@DataProducer(
  id = "default_profile_load",
  name = @Translation("Load default profile"),
  description = @Translation("Loads the default profile for a user."),
  produces = @ContextDefinition("entity:profile",
    label = @Translation("Profile")
  ),
  consumes = {
    "user" = @ContextDefinition("entity:user",
      label = @Translation("User")
    )
  }
)

Hierarchy

Expanded class hierarchy of DefaultProfileLoad

File

modules/social_features/social_profile/src/Plugin/GraphQL/DataProducer/DefaultProfileLoad.php, line 33

Namespace

Drupal\social_profile\Plugin\GraphQL\DataProducer
View source
class DefaultProfileLoad extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity buffer service.
   *
   * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer
   */
  protected $entityBuffer;

  /**
   * {@inheritdoc}
   *
   * @codeCoverageIgnore
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('graphql.buffer.entity'));
  }

  /**
   * EntityLoad constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param array $pluginDefinition
   *   The plugin definition array.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   * @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer
   *   The entity buffer service.
   *
   * @codeCoverageIgnore
   */
  public function __construct(array $configuration, $pluginId, array $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, EntityBuffer $entityBuffer) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->entityTypeManager = $entityTypeManager;
    $this->entityBuffer = $entityBuffer;
  }

  /**
   * Resolve to a value.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to fetch the profile for.
   * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
   *   Cacheability metadata.
   *
   * @return \GraphQL\Deferred|null
   *   A promise that resolves to the loaded profile or null if the user does
   *   not have a published profile.
   */
  public function resolve(AccountInterface $account, RefinableCacheableDependencyInterface $metadata) {
    $storage = $this->entityTypeManager
      ->getStorage('profile');
    $type = $storage
      ->getEntityType();
    $query = $storage
      ->getQuery()
      ->currentRevision()
      ->accessCheck()
      ->condition('uid', $account
      ->id())
      ->condition('type', 'profile')
      ->condition('status', TRUE)
      ->sort('is_default', 'DESC')
      ->sort('profile_id', 'DESC')
      ->range(0, 1);
    $metadata
      ->addCacheTags($type
      ->getListCacheTags());
    $metadata
      ->addCacheContexts($type
      ->getListCacheContexts());
    $result = $query
      ->execute();
    if (empty($result)) {
      return NULL;
    }
    $resolver = $this->entityBuffer
      ->add('profile', reset($result));
    return new Deferred(function () use ($resolver, $metadata) {
      $entity = $resolver();
      $metadata
        ->addCacheableDependency($entity);
      return $entity;
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultProfileLoad::$entityBuffer protected property The entity buffer service.
DefaultProfileLoad::$entityTypeManager protected property The entity type manager service.
DefaultProfileLoad::create public static function @codeCoverageIgnore Overrides ContainerFactoryPluginInterface::create
DefaultProfileLoad::resolve public function Resolve to a value.
DefaultProfileLoad::__construct public function EntityLoad constructor.