class TaxonomyIndexTid in Drupal 10
Same name in this branch
- 10 core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid
- 10 core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
Same name and namespace in other branches
- 8 core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
- 9 core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
Field handler to display all taxonomy terms of a node.
Plugin annotation
@ViewsField("taxonomy_index_tid");
Hierarchy
- class \Drupal\views\Plugin\views\field\PrerenderList extends \Drupal\views\Plugin\views\field\FieldPluginBase implements MultiItemsFieldHandlerInterface
- class \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
Expanded class hierarchy of TaxonomyIndexTid
File
- core/
modules/ taxonomy/ src/ Plugin/ views/ field/ TaxonomyIndexTid.php, line 19
Namespace
Drupal\taxonomy\Plugin\views\fieldView source
class TaxonomyIndexTid extends PrerenderList {
/**
* The vocabulary storage.
*
* @var \Drupal\taxonomy\VocabularyStorageInterface
*/
protected $vocabularyStorage;
/**
* Constructs a TaxonomyIndexTid 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'));
}
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
// @todo: Wouldn't it be possible to use $this->base_table and no if here?
if ($view->storage
->get('base_table') == 'node_field_revision') {
$this->additional_fields['nid'] = [
'table' => 'node_field_revision',
'field' => 'nid',
];
}
else {
$this->additional_fields['nid'] = [
'table' => 'node_field_data',
'field' => 'nid',
];
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_taxonomy'] = [
'default' => TRUE,
];
$options['limit'] = [
'default' => FALSE,
];
$options['vids'] = [
'default' => [],
];
return $options;
}
/**
* Provide "link to term" option.
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_to_taxonomy'] = [
'#title' => $this
->t('Link this field to its term page'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['link_to_taxonomy']),
];
$form['limit'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Limit terms by vocabulary'),
'#default_value' => $this->options['limit'],
];
$options = [];
$vocabularies = $this->vocabularyStorage
->loadMultiple();
foreach ($vocabularies as $voc) {
$options[$voc
->id()] = $voc
->label();
}
$form['vids'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Vocabularies'),
'#options' => $options,
'#default_value' => $this->options['vids'],
'#states' => [
'visible' => [
':input[name="options[limit]"]' => [
'checked' => TRUE,
],
],
],
];
parent::buildOptionsForm($form, $form_state);
}
/**
* Add this term to the query.
*/
public function query() {
$this
->addAdditionalFields();
}
public function preRender(&$values) {
$vocabularies = $this->vocabularyStorage
->loadMultiple();
$this->field_alias = $this->aliases['nid'];
$nids = [];
foreach ($values as $result) {
if (!empty($result->{$this->aliases['nid']})) {
$nids[] = $result->{$this->aliases['nid']};
}
}
if ($nids) {
$vocabs = array_filter($this->options['vids']);
if (empty($this->options['limit'])) {
$vocabs = [];
}
$result = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->getNodeTerms($nids, $vocabs);
foreach ($result as $node_nid => $data) {
foreach ($data as $tid => $term) {
$this->items[$node_nid][$tid]['name'] = \Drupal::service('entity.repository')
->getTranslationFromContext($term)
->label();
$this->items[$node_nid][$tid]['tid'] = $tid;
$this->items[$node_nid][$tid]['vocabulary_vid'] = $term
->bundle();
$this->items[$node_nid][$tid]['vocabulary'] = $vocabularies[$term
->bundle()]
->label();
if (!empty($this->options['link_to_taxonomy'])) {
$this->items[$node_nid][$tid]['make_link'] = TRUE;
$this->items[$node_nid][$tid]['path'] = 'taxonomy/term/' . $tid;
}
}
}
}
}
public function render_item($count, $item) {
return $item['name'];
}
protected function documentSelfTokens(&$tokens) {
$tokens['{{ ' . $this->options['id'] . '__tid' . ' }}'] = $this
->t('The taxonomy term ID for the term.');
$tokens['{{ ' . $this->options['id'] . '__name' . ' }}'] = $this
->t('The taxonomy term name for the term.');
$tokens['{{ ' . $this->options['id'] . '__vocabulary_vid' . ' }}'] = $this
->t('The machine name for the vocabulary the term belongs to.');
$tokens['{{ ' . $this->options['id'] . '__vocabulary' . ' }}'] = $this
->t('The name for the vocabulary the term belongs to.');
}
protected function addSelfTokens(&$tokens, $item) {
foreach ([
'tid',
'name',
'vocabulary_vid',
'vocabulary',
] as $token) {
$tokens['{{ ' . $this->options['id'] . '__' . $token . ' }}'] = $item[$token] ?? '';
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DerivativeInspectionInterface:: |
public | function | Gets the base_plugin_id of the plugin instance. | |
DerivativeInspectionInterface:: |
public | function | Gets the derivative_id of the plugin instance. | |
FieldHandlerInterface:: |
public | function | Renders a field using advanced settings. | |
FieldHandlerInterface:: |
public | function | Adds an ORDER BY clause to the query for click sort columns. | |
FieldHandlerInterface:: |
public | function | Determines if this field is click sortable. | |
FieldHandlerInterface:: |
public | function | Returns the class of the field. | |
FieldHandlerInterface:: |
public | function | Returns the class of the field's label. | |
FieldHandlerInterface:: |
public | function | Returns an HTML element for the label based upon the field's element type. | |
FieldHandlerInterface:: |
public | function | Returns an HTML element based upon the field's element type. | |
FieldHandlerInterface:: |
public | function | Returns the class of the field's wrapper. | |
FieldHandlerInterface:: |
public | function | Returns an HTML element for the wrapper based upon the field's element type. | |
FieldHandlerInterface:: |
public | function | Provides a list of elements valid for field HTML. | |
FieldHandlerInterface:: |
public | function | Gets the entity matching the current row and relationship. | |
FieldHandlerInterface:: |
public | function | Gets the 'render' tokens to use for advanced rendering. | |
FieldHandlerInterface:: |
public | function | Gets the value that's supposed to be rendered. | |
FieldHandlerInterface:: |
public | function | Checks if a field value is empty. | |
FieldHandlerInterface:: |
public | function | Gets this field's label. | |
FieldHandlerInterface:: |
public | function | Runs after every field has been rendered. | |
FieldHandlerInterface:: |
public | function | Renders the field. | |
FieldHandlerInterface:: |
public | function | Performs an advanced text render for the item. | |
FieldHandlerInterface:: |
public | function | Renders row values using $this->themeFunctions() as #theme. | |
FieldHandlerInterface:: |
public | function | Replaces a value with tokens from the last field. | |
FieldHandlerInterface:: |
public | function | Determines if this field will be available as an option to group the result by in the style settings. | |
PluginInspectionInterface:: |
public | function | Gets the definition of the plugin implementation. | 1 |
PluginInspectionInterface:: |
public | function | Gets the plugin_id of the plugin instance. | 1 |
PrerenderList:: |
public | property | Stores all items which are used to render the items. | |
PrerenderList:: |
public | function |
Items should be stored in the result array, if possible, as an array
with 'value' as the actual displayable value of the item, plus
any items that might be found in the 'alter' options array for
creating links, such as… Overrides MultiItemsFieldHandlerInterface:: |
|
PrerenderList:: |
public | function |
Render all items in this field together. Overrides MultiItemsFieldHandlerInterface:: |
|
TaxonomyIndexTid:: |
protected | property | The vocabulary storage. | |
TaxonomyIndexTid:: |
protected | function | ||
TaxonomyIndexTid:: |
public | function |
Provide "link to term" option. Overrides PrerenderList:: |
|
TaxonomyIndexTid:: |
public static | function |
Overrides ViewsPluginInterface:: |
|
TaxonomyIndexTid:: |
protected | function |
Overrides PrerenderList:: |
|
TaxonomyIndexTid:: |
protected | function | ||
TaxonomyIndexTid:: |
public | function |
Initialize the plugin. Overrides ViewsPluginInterface:: |
|
TaxonomyIndexTid:: |
public | function |
Runs before any fields are rendered. Overrides FieldHandlerInterface:: |
|
TaxonomyIndexTid:: |
public | function |
Add this term to the query. Overrides ViewsPluginInterface:: |
|
TaxonomyIndexTid:: |
public | function |
Renders a single item of a row. Overrides MultiItemsFieldHandlerInterface:: |
|
TaxonomyIndexTid:: |
public | function | Constructs a TaxonomyIndexTid object. | |
ViewsHandlerInterface:: |
public | function | Check whether given user has access to this handler. | |
ViewsHandlerInterface:: |
public | function | Return a string representing this handler's name in the UI. | |
ViewsHandlerInterface:: |
public | function | Provide text for the administrative summary. | |
ViewsHandlerInterface:: |
public static | function | Breaks x,y,z and x+y+z into an array. | |
ViewsHandlerInterface:: |
public | function | Determines if the handler is considered 'broken', meaning it's a placeholder used when a handler can't be found. | |
ViewsHandlerInterface:: |
public | function | Ensure the main table for this handler is in the query. This is used a lot. | |
ViewsHandlerInterface:: |
public | function | Determines the entity type used by this handler. | |
ViewsHandlerInterface:: |
public | function | Shortcut to get a handler's raw field value. | |
ViewsHandlerInterface:: |
public | function | Get the join object that should be used for this handler. | |
ViewsHandlerInterface:: |
public static | function | Fetches a handler to join one table to a primary table from the data cache. | |
ViewsHandlerInterface:: |
public | function | Run after the view is executed, before the result is cached. | |
ViewsHandlerInterface:: |
public | function | Run before the view is built. | |
ViewsHandlerInterface:: |
public | function | Sanitize the value for output. | |
ViewsHandlerInterface:: |
public | function | Called just prior to query(), this lets a handler set up any relationship it needs. | |
ViewsHandlerInterface:: |
public | function | Shortcut to display the exposed options form. | |
ViewsPluginInterface:: |
public | function | Clears a plugin. | |
ViewsPluginInterface:: |
public | function | Filter out stored options depending on the defined options. | |
ViewsPluginInterface:: |
public | function | Returns an array of available token replacements. | |
ViewsPluginInterface:: |
public | function | Returns the plugin provider. | |
ViewsPluginInterface:: |
public | function | Adds elements for available core tokens to a form. | |
ViewsPluginInterface:: |
public | function | Returns a string with any core tokens replaced. | |
ViewsPluginInterface:: |
public | function | Return the human readable name of the display. | |
ViewsPluginInterface:: |
public static | function | Moves form elements into fieldsets for presentation purposes. | |
ViewsPluginInterface:: |
public static | function | Flattens the structure of form elements. | |
ViewsPluginInterface:: |
public | function | Handle any special handling on the validate form. | |
ViewsPluginInterface:: |
public | function | Returns the summary of the settings in the display. | |
ViewsPluginInterface:: |
public | function | Provide a full list of possible theme templates used by this style. | |
ViewsPluginInterface:: |
public | function | Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away. | |
ViewsPluginInterface:: |
public | function | Returns the usesOptions property. | |
ViewsPluginInterface:: |
public | function | Validate that the plugin is correct and can be saved. | |
ViewsPluginInterface:: |
public | function | Validate the options form. |