grant-1.x-dev/src/Plugin/views/argument_default/GrantCurrentUserEmails.php

src/Plugin/views/argument_default/GrantCurrentUserEmails.php
<?php

namespace Drupal\grant\Plugin\views\argument_default;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * GrantCurrentUserMail argument default plugin.
 *
 * @ViewsArgumentDefault(
 *   id = "grant_current_user_emails",
 *   title = @Translation("User email address(es) from logged in user (grant module)")
 * )
 */
class GrantCurrentUserEmails extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The constructor.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The active user account.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    AccountInterface $account,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_user'),
    );
  }

  /**
   * Return the default argument.
   *
   * Currently we need to alter the query for multiple mails via
   * hook_views_query_alter().
   *
   * @see https://www.drupal.org/project/drupal/issues/3301388
   * @see grant.views_execution.inc
   */
  public function getArgument() {
    return $this->account->getEmail();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return ['user'];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc