"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php" (26 Nov 2020, 5793 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TaxonomyIndexTid.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\taxonomy\Plugin\views\field;
    4 
    5 use Drupal\Core\Form\FormStateInterface;
    6 use Drupal\views\ViewExecutable;
    7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
    8 use Drupal\views\Plugin\views\field\PrerenderList;
    9 use Symfony\Component\DependencyInjection\ContainerInterface;
   10 use Drupal\taxonomy\VocabularyStorageInterface;
   11 
   12 /**
   13  * Field handler to display all taxonomy terms of a node.
   14  *
   15  * @ingroup views_field_handlers
   16  *
   17  * @ViewsField("taxonomy_index_tid")
   18  */
   19 class TaxonomyIndexTid extends PrerenderList {
   20 
   21   /**
   22    * The vocabulary storage.
   23    *
   24    * @var \Drupal\taxonomy\VocabularyStorageInterface
   25    */
   26   protected $vocabularyStorage;
   27 
   28   /**
   29    * Constructs a TaxonomyIndexTid object.
   30    *
   31    * @param array $configuration
   32    *   A configuration array containing information about the plugin instance.
   33    * @param string $plugin_id
   34    *   The plugin_id for the plugin instance.
   35    * @param mixed $plugin_definition
   36    *   The plugin implementation definition.
   37    * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   38    *   The vocabulary storage.
   39    */
   40   public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage) {
   41     parent::__construct($configuration, $plugin_id, $plugin_definition);
   42     $this->vocabularyStorage = $vocabulary_storage;
   43   }
   44 
   45   /**
   46    * {@inheritdoc}
   47    */
   48   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
   49     return new static(
   50       $configuration,
   51       $plugin_id,
   52       $plugin_definition,
   53       $container->get('entity_type.manager')->getStorage('taxonomy_vocabulary')
   54     );
   55   }
   56 
   57   /**
   58    * {@inheritdoc}
   59    */
   60   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
   61     parent::init($view, $display, $options);
   62 
   63     // @todo: Wouldn't it be possible to use $this->base_table and no if here?
   64     if ($view->storage->get('base_table') == 'node_field_revision') {
   65       $this->additional_fields['nid'] = ['table' => 'node_field_revision', 'field' => 'nid'];
   66     }
   67     else {
   68       $this->additional_fields['nid'] = ['table' => 'node_field_data', 'field' => 'nid'];
   69     }
   70   }
   71 
   72   protected function defineOptions() {
   73     $options = parent::defineOptions();
   74 
   75     $options['link_to_taxonomy'] = ['default' => TRUE];
   76     $options['limit'] = ['default' => FALSE];
   77     $options['vids'] = ['default' => []];
   78 
   79     return $options;
   80   }
   81 
   82   /**
   83    * Provide "link to term" option.
   84    */
   85   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
   86     $form['link_to_taxonomy'] = [
   87       '#title' => $this->t('Link this field to its term page'),
   88       '#type' => 'checkbox',
   89       '#default_value' => !empty($this->options['link_to_taxonomy']),
   90     ];
   91 
   92     $form['limit'] = [
   93       '#type' => 'checkbox',
   94       '#title' => $this->t('Limit terms by vocabulary'),
   95       '#default_value' => $this->options['limit'],
   96     ];
   97 
   98     $options = [];
   99     $vocabularies = $this->vocabularyStorage->loadMultiple();
  100     foreach ($vocabularies as $voc) {
  101       $options[$voc->id()] = $voc->label();
  102     }
  103 
  104     $form['vids'] = [
  105       '#type' => 'checkboxes',
  106       '#title' => $this->t('Vocabularies'),
  107       '#options' => $options,
  108       '#default_value' => $this->options['vids'],
  109       '#states' => [
  110         'visible' => [
  111           ':input[name="options[limit]"]' => ['checked' => TRUE],
  112         ],
  113       ],
  114 
  115     ];
  116 
  117     parent::buildOptionsForm($form, $form_state);
  118   }
  119 
  120   /**
  121    * Add this term to the query
  122    */
  123   public function query() {
  124     $this->addAdditionalFields();
  125   }
  126 
  127   public function preRender(&$values) {
  128     $vocabularies = $this->vocabularyStorage->loadMultiple();
  129     $this->field_alias = $this->aliases['nid'];
  130     $nids = [];
  131     foreach ($values as $result) {
  132       if (!empty($result->{$this->aliases['nid']})) {
  133         $nids[] = $result->{$this->aliases['nid']};
  134       }
  135     }
  136 
  137     if ($nids) {
  138       $vocabs = array_filter($this->options['vids']);
  139       if (empty($this->options['limit'])) {
  140         $vocabs = [];
  141       }
  142       $result = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getNodeTerms($nids, $vocabs);
  143 
  144       foreach ($result as $node_nid => $data) {
  145         foreach ($data as $tid => $term) {
  146           $this->items[$node_nid][$tid]['name'] = \Drupal::service('entity.repository')->getTranslationFromContext($term)->label();
  147           $this->items[$node_nid][$tid]['tid'] = $tid;
  148           $this->items[$node_nid][$tid]['vocabulary_vid'] = $term->bundle();
  149           $this->items[$node_nid][$tid]['vocabulary'] = $vocabularies[$term->bundle()]->label();
  150 
  151           if (!empty($this->options['link_to_taxonomy'])) {
  152             $this->items[$node_nid][$tid]['make_link'] = TRUE;
  153             $this->items[$node_nid][$tid]['path'] = 'taxonomy/term/' . $tid;
  154           }
  155         }
  156       }
  157     }
  158   }
  159 
  160   public function render_item($count, $item) {
  161     return $item['name'];
  162   }
  163 
  164   protected function documentSelfTokens(&$tokens) {
  165     $tokens['{{ ' . $this->options['id'] . '__tid' . ' }}'] = $this->t('The taxonomy term ID for the term.');
  166     $tokens['{{ ' . $this->options['id'] . '__name' . ' }}'] = $this->t('The taxonomy term name for the term.');
  167     $tokens['{{ ' . $this->options['id'] . '__vocabulary_vid' . ' }}'] = $this->t('The machine name for the vocabulary the term belongs to.');
  168     $tokens['{{ ' . $this->options['id'] . '__vocabulary' . ' }}'] = $this->t('The name for the vocabulary the term belongs to.');
  169   }
  170 
  171   protected function addSelfTokens(&$tokens, $item) {
  172     foreach (['tid', 'name', 'vocabulary_vid', 'vocabulary'] as $token) {
  173       $tokens['{{ ' . $this->options['id'] . '__' . $token . ' }}'] = isset($item[$token]) ? $item[$token] : '';
  174     }
  175   }
  176 
  177 }