Commit bb543187 authored by catch's avatar catch
Browse files

Issue #2640994 by dww, tduong, Alex Bukach, Berdir, ameymudras, catch,...

Issue #2640994 by dww, tduong, Alex Bukach, Berdir, ameymudras, catch, mohit_aghera, raman.b, jofitz, Ben Buske, ankithashetty, smustgrave, nikitagupta, nicxvan, FeyP, pameeela, technoveltyco, dawehner, kim.pepper, Lendude: Fix label token replacement for views entity reference arguments

(cherry picked from commit 46fd5d2d)
parent 590fb527
Loading
Loading
Loading
Loading
Loading
+2 −60
Original line number Diff line number Diff line
@@ -2,11 +2,8 @@

namespace Drupal\file\Plugin\views\argument;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Attribute\ViewsArgument;
use Drupal\views\Plugin\views\argument\NumericArgument;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\views\Plugin\views\argument\EntityArgument;

/**
 * Argument handler to accept multiple file ids.
@@ -16,59 +13,4 @@
#[ViewsArgument(
  id: 'file_fid',
)]
class Fid extends NumericArgument implements ContainerFactoryPluginInterface {

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

  /**
   * Constructs a Drupal\file\Plugin\views\argument\Fid object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  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')
    );
  }

  /**
   * Override the behavior of titleQuery(). Get the filenames.
   */
  public function titleQuery() {
    $storage = $this->entityTypeManager->getStorage('file');
    $fids = $storage->getQuery()
      ->accessCheck(FALSE)
      ->condition('fid', $this->value, 'IN')
      ->execute();
    $files = $storage->loadMultiple($fids);
    $titles = [];
    foreach ($files as $file) {
      $titles[] = $file->getFilename();
    }
    return $titles;
  }

}
class Fid extends EntityArgument {}
+2 −55
Original line number Diff line number Diff line
@@ -2,10 +2,8 @@

namespace Drupal\node\Plugin\views\argument;

use Drupal\node\NodeStorageInterface;
use Drupal\views\Attribute\ViewsArgument;
use Drupal\views\Plugin\views\argument\NumericArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\views\Plugin\views\argument\EntityArgument;

/**
 * Argument handler to accept a node id.
@@ -13,55 +11,4 @@
#[ViewsArgument(
  id: 'node_nid',
)]
class Nid extends NumericArgument {

  /**
   * The node storage.
   *
   * @var \Drupal\node\NodeStorageInterface
   */
  protected $nodeStorage;

  /**
   * Constructs the Nid object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\node\NodeStorageInterface $node_storage
   *   The node storage handler.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, NodeStorageInterface $node_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->nodeStorage = $node_storage;
  }

  /**
   * {@inheritdoc}
   */
  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')->getStorage('node')
    );
  }

  /**
   * Override the behavior of title(). Get the title of the node.
   */
  public function titleQuery() {
    $titles = [];

    $nodes = $this->nodeStorage->loadMultiple($this->value);
    foreach ($nodes as $node) {
      $titles[] = $node->label();
    }
    return $titles;
  }

}
class Nid extends EntityArgument {}
+2 −58
Original line number Diff line number Diff line
@@ -2,12 +2,8 @@

namespace Drupal\taxonomy\Plugin\views\argument;

use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Attribute\ViewsArgument;
use Drupal\views\Plugin\views\argument\NumericArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\views\Plugin\views\argument\EntityArgument;

/**
 * Argument handler for basic taxonomy tid.
@@ -17,56 +13,4 @@
#[ViewsArgument(
  id: 'taxonomy',
)]
class Taxonomy extends NumericArgument implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   *
   * @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no
   *   replacement.
   *
   * @see https://www.drupal.org/node/3427843
   */
  protected $termStorage;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected EntityStorageInterface|EntityRepositoryInterface $entityRepository) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if ($entityRepository instanceof EntityStorageInterface) {
      // @phpstan-ignore-next-line
      $this->termStorage = $this->entityRepository;
      @trigger_error('Calling ' . __CLASS__ . '::__construct() with the $termStorage argument as \Drupal\Core\Entity\EntityStorageInterface is deprecated in drupal:10.3.0 and it will require Drupal\Core\Entity\EntityRepositoryInterface in drupal:11.0.0. See https://www.drupal.org/node/3427843', E_USER_DEPRECATED);
      $this->entityRepository = \Drupal::service('entity.repository');
    }
  }

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

  /**
   * Override the behavior of title(). Get the title of the node.
   */
  public function title() {
    // There might be no valid argument.
    if ($this->argument) {
      $term = $this->entityRepository->getCanonical('taxonomy_term', $this->argument);
      if (!empty($term)) {
        return $term->label();
      }
    }
    // TODO review text
    return $this->t('No name');
  }

}
class Taxonomy extends EntityArgument {}
+2 −54
Original line number Diff line number Diff line
@@ -3,9 +3,7 @@
namespace Drupal\taxonomy\Plugin\views\argument;

use Drupal\views\Attribute\ViewsArgument;
use Drupal\views\Plugin\views\argument\NumericArgument;
use Drupal\taxonomy\VocabularyStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\views\Plugin\views\argument\EntityArgument;

/**
 * Argument handler to accept a vocabulary id.
@@ -15,54 +13,4 @@
#[ViewsArgument(
  id: 'vocabulary_vid',
)]
class VocabularyVid extends NumericArgument {

  /**
   * The vocabulary storage.
   *
   * @var \Drupal\taxonomy\VocabularyStorageInterface
   */
  protected $vocabularyStorage;

  /**
   * Constructs the VocabularyVid object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   *   The vocabulary storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->vocabularyStorage = $vocabulary_storage;
  }

  /**
   * {@inheritdoc}
   */
  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')->getStorage('taxonomy_vocabulary')
    );
  }

  /**
   * Override the behavior of title(). Get the name of the vocabulary.
   */
  public function title() {
    $vocabulary = $this->vocabularyStorage->load($this->argument);
    if ($vocabulary) {
      return $vocabulary->label();
    }

    return $this->t('No vocabulary');
  }

}
class VocabularyVid extends EntityArgument {}
+13 −10
Original line number Diff line number Diff line
@@ -5,12 +5,14 @@
namespace Drupal\taxonomy_test\Plugin\views\argument;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\taxonomy\Plugin\views\argument\Taxonomy;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Test argument handler for testing deprecation in IndexTidDepth plugin.
 * Test argument handler for testing deprecation in Taxonomy argument plugin.
 *
 * Intentionally setup our properties and constructor as Drupal 10.2.x and
 * earlier used in the Taxonomy argument handler.
 *
 * @ingroup views_argument_handlers
 *
@@ -18,23 +20,24 @@
 */
class TaxonomyViewsArgumentTest extends Taxonomy {

  /**
   * Constructs new IndexTidDepthTestPlugin object.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $term_storage, protected EntityTypeBundleInfoInterface $entityTypeBundleInfo) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $term_storage);
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected EntityStorageInterface $termStorage,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $termStorage);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
  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')->getStorage('taxonomy_term'),
      $container->get('entity_type.bundle.info'),
      $container->get('entity_type.manager')->getStorage('taxonomy_term')
    );
  }

Loading