Taxonomy.php 1.56 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\taxonomy\Plugin\views\argument\Taxonomy.
6 7
 */

8
namespace Drupal\taxonomy\Plugin\views\argument;
9

10 11
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12
use Drupal\views\Plugin\views\argument\NumericArgument;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14

15 16 17 18
/**
 * Argument handler for basic taxonomy tid.
 *
 * @ingroup views_argument_handlers
19
 *
20
 * @ViewsArgument("taxonomy")
21
 */
22
class Taxonomy extends NumericArgument implements ContainerFactoryPluginInterface {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  /**
   * @var EntityStorageInterface
   */
  protected $termStorage;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $term_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->termStorage = $term_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.manager')->getStorage('taxonomy_term')
    );
  }
49 50 51 52 53 54 55

  /**
   * Override the behavior of title(). Get the title of the node.
   */
  function title() {
    // There might be no valid argument.
    if ($this->argument) {
56
      $term = $this->termStorage->load($this->argument);
57
      if (!empty($term)) {
58
        return $term->getName();
59 60 61
      }
    }
    // TODO review text
62
    return $this->t('No name');
63
  }
64

65
}