digital_signage_framework-2.3.x-dev/src/Plugin/Action/Base.php

src/Plugin/Action/Base.php
<?php

namespace Drupal\digital_signage_framework\Plugin\Action;

use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Push temporary configuration to devices.
 */
abstract class Base extends ActionBase implements ContainerFactoryPluginInterface {

  /**
   * The tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected PrivateTempStoreFactory $tempStoreFactory;

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

  /**
   * {@inheritdoc}
   */
  final public function __construct(array $configuration, $plugin_id, $plugin_definition, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user) {
    $this->currentUser = $current_user;
    $this->tempStoreFactory = $temp_store_factory;

    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

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

  /**
   * Provides the required permission.
   *
   * @return string
   *   The required permission.
   */
  protected function permission(): string {
    return 'push digital signage config';
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE): bool|AccessResultInterface {
    return AccessResultAllowed::allowedIf($account !== NULL && $account->hasPermission($this->permission()));
  }

  /**
   * {@inheritdoc}
   */
  public function executeMultiple(array $entities): void {
    $this->tempStoreFactory->get($this->pluginId)->set((string) $this->currentUser->id(), $entities);
  }

  /**
   * {@inheritdoc}
   */
  public function execute(?ContentEntityInterface $object = NULL): void {
    $this->executeMultiple([$object]);
  }

}

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

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