Taxonomy.php 1.62 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 Drupal\Component\Utility\SafeMarkup;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15

16 17 18 19
/**
 * Argument handler for basic taxonomy tid.
 *
 * @ingroup views_argument_handlers
20
 *
21
 * @ViewsArgument("taxonomy")
22
 */
23
class Taxonomy extends NumericArgument implements ContainerFactoryPluginInterface {
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 49

  /**
   * @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')
    );
  }
50 51 52 53 54 55 56

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

66
}