bulk_edit_terms-8.x-1.1/src/Plugin/Action/EditTermsNode.php

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

declare(strict_types=1);

namespace Drupal\bulk_edit_terms\Plugin\Action;

use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\node\NodeInterface;
use Drupal\node\NodeStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Update term reference fields on nodes.
 */
#[Action(
  id: 'node_edit_terms_action',
  label: new TranslatableMarkup('Update term references for the selected content.'),
  confirm_form_route_name: 'node.select_taxonomy_terms',
  type: 'node',
)]
class EditTermsNode extends ActionBase implements ContainerFactoryPluginInterface {

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected PrivateTempStoreFactory $privateTempStoreFactory,
    protected AccountInterface $currentUser,
    protected EntityTypeManagerInterface $entityTypeManager,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

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

  /**
   * {@inheritdoc}
   */
  public function executeMultiple(array $entities): void {
    $nodeIds = array_map(fn(NodeInterface $entity) => $entity->id(), $entities);
    $this->privateTempStoreFactory->get('node_edit_terms')->set($this->currentUser->id(), $nodeIds);
  }

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

  /**
   * {@inheritdoc}
   */
  public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE): bool|AccessResultInterface {
    // We operate on the latest revision of the node, so load that before
    // checking update access.
    assert($object instanceof NodeInterface);
    $node_storage = $this->entityTypeManager->getStorage('node');
    assert($node_storage instanceof NodeStorageInterface);
    $latest_vid = $node_storage->getLatestRevisionId($object->id());
    $node = $node_storage->loadRevision($latest_vid);

    $result = $node->access('update', $account, TRUE);

    return $return_as_object ? $result : $result->isAllowed();
  }

}

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

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