You are here

class MasqueradeCallbacks in Masquerade 8.2

Masquerade callbacks.

Hierarchy

Expanded class hierarchy of MasqueradeCallbacks

1 string reference to 'MasqueradeCallbacks'
masquerade.services.yml in ./masquerade.services.yml
masquerade.services.yml
1 service uses MasqueradeCallbacks
masquerade.callbacks in ./masquerade.services.yml
Drupal\masquerade\MasqueradeCallbacks

File

src/MasqueradeCallbacks.php, line 12

Namespace

Drupal\masquerade
View source
class MasqueradeCallbacks implements TrustedCallbackInterface {

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

  /**
   * The masquerade service.
   *
   * @var \Drupal\masquerade\Masquerade
   */
  protected $masquerade;

  /**
   * MasqueradeCallbacks constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\masquerade\Masquerade $masquerade
   *   The masuerade.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, Masquerade $masquerade) {
    $this->entityTypeManager = $entity_type_manager;
    $this->masquerade = $masquerade;
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'renderCacheLink',
    ];
  }

  /**
   * #post_render_cache callback; replaces placeholder with masquerade link.
   *
   * @param int $account_id
   *   The account ID.
   *
   * @return array
   *   A renderable array containing the masquerade link if allowed.
   */
  public function renderCacheLink($account_id) {

    /** @var \Drupal\user\UserInterface $account */
    $account = $this->entityTypeManager
      ->getStorage('user')
      ->load($account_id);
    if (masquerade_target_user_access($account)) {

      // @todo Attaching a CSS class to this would be nice.
      return [
        'masquerade' => [
          '#type' => 'link',
          '#title' => new TranslatableMarkup('Masquerade as @name', [
            '@name' => $account
              ->getDisplayName(),
          ]),
          '#url' => $account
            ->toUrl('masquerade'),
        ],
      ];
    }
    return [
      '#markup' => '',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MasqueradeCallbacks::$entityTypeManager protected property The entity type manager.
MasqueradeCallbacks::$masquerade protected property The masquerade service.
MasqueradeCallbacks::renderCacheLink public function #post_render_cache callback; replaces placeholder with masquerade link.
MasqueradeCallbacks::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
MasqueradeCallbacks::__construct public function MasqueradeCallbacks constructor.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.