class Tid in Views (for Drupal 7) 8.3
Taxonomy tid default argument.
Plugin annotation
@Plugin(
id = "taxonomy_tid",
module = "taxonomy",
title = @Translation("Taxonomy term ID from URL")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase
- class \Views\taxonomy\Plugin\views\argument_default\Tid
- class \Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase
- class \Drupal\views\Plugin\views\PluginBase
Expanded class hierarchy of Tid
File
- lib/
Views/ taxonomy/ Plugin/ views/ argument_default/ Tid.php, line 24 - Definition of Views\taxonomy\Plugin\views\argument_default\Tid.
Namespace
Views\taxonomy\Plugin\views\argument_defaultView source
class Tid extends ArgumentDefaultPluginBase {
public function init(ViewExecutable $view, &$argument, $options) {
parent::init($view, $argument, $options);
// Convert legacy vids option to machine name vocabularies.
if (!empty($this->options['vids'])) {
$vocabularies = taxonomy_vocabulary_get_names();
foreach ($this->options['vids'] as $vid) {
if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
$this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
}
}
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['term_page'] = array(
'default' => TRUE,
'bool' => TRUE,
);
$options['node'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['anyall'] = array(
'default' => ',',
);
$options['limit'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['vocabularies'] = array(
'default' => array(),
);
return $options;
}
public function buildOptionsForm(&$form, &$form_state) {
$form['term_page'] = array(
'#type' => 'checkbox',
'#title' => t('Load default filter from term page'),
'#default_value' => $this->options['term_page'],
);
$form['node'] = array(
'#type' => 'checkbox',
'#title' => t('Load default filter from node page, that\'s good for related taxonomy blocks'),
'#default_value' => $this->options['node'],
);
$form['limit'] = array(
'#type' => 'checkbox',
'#title' => t('Limit terms by vocabulary'),
'#default_value' => $this->options['limit'],
'#states' => array(
'visible' => array(
':input[name="options[argument_default][taxonomy_tid][node]"]' => array(
'checked' => TRUE,
),
),
),
);
$options = array();
$vocabularies = taxonomy_vocabulary_get_names();
foreach ($vocabularies as $voc) {
$options[$voc->machine_name] = check_plain($voc->name);
}
$form['vocabularies'] = array(
'#type' => 'checkboxes',
'#title' => t('Vocabularies'),
'#options' => $options,
'#default_value' => $this->options['vocabularies'],
'#states' => array(
'visible' => array(
':input[name="options[argument_default][taxonomy_tid][limit]"]' => array(
'checked' => TRUE,
),
':input[name="options[argument_default][taxonomy_tid][node]"]' => array(
'checked' => TRUE,
),
),
),
);
$form['anyall'] = array(
'#type' => 'radios',
'#title' => t('Multiple-value handling'),
'#default_value' => $this->options['anyall'],
'#options' => array(
',' => t('Filter to items that share all terms'),
'+' => t('Filter to items that share any term'),
),
'#states' => array(
'visible' => array(
':input[name="options[argument_default][taxonomy_tid][node]"]' => array(
'checked' => TRUE,
),
),
),
);
}
public function submitOptionsForm(&$form, &$form_state, &$options = array()) {
// Filter unselected items so we don't unnecessarily store giant arrays.
$options['vocabularies'] = array_filter($options['vocabularies']);
}
function get_argument() {
// Load default argument from taxonomy page.
if (!empty($this->options['term_page'])) {
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
return arg(2);
}
}
// Load default argument from node.
if (!empty($this->options['node'])) {
foreach (range(1, 3) as $i) {
$node = menu_get_object('node', $i);
if (!empty($node)) {
break;
}
}
// Just check, if a node could be detected.
if ($node) {
$taxonomy = array();
$fields = field_info_instances('node', $node->type);
foreach ($fields as $name => $info) {
$field_info = field_info_field($name);
if ($field_info['type'] == 'taxonomy_term_reference') {
$items = field_get_items('node', $node, $name);
if (is_array($items)) {
foreach ($items as $item) {
$taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
}
}
}
}
if (!empty($this->options['limit'])) {
$tids = array();
// filter by vocabulary
foreach ($taxonomy as $tid => $vocab) {
if (!empty($this->options['vocabularies'][$vocab])) {
$tids[] = $tid;
}
}
return implode($this->options['anyall'], $tids);
}
else {
return implode($this->options['anyall'], array_keys($taxonomy));
}
}
}
// If the current page is a view that takes tid as an argument,
// find the tid argument and return it.
$views_page = views_get_page_view();
if ($views_page && isset($views_page->argument['tid'])) {
return $views_page->argument['tid']->argument;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ArgumentDefaultPluginBase:: |
public | function | Determine if the administrator has the privileges to use this plugin | 1 |
ArgumentDefaultPluginBase:: |
function | If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input. | ||
ArgumentDefaultPluginBase:: |
public | function |
Provide the default form form for validating options Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
public | property | Plugins's definition | |
PluginBase:: |
public | property | The display object this plugin is for. | |
PluginBase:: |
public | property | Options for this plugin will be held here. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | Denotes whether the plugin has an additional options form. | 8 |
PluginBase:: |
public | property | The top object of a view. | 1 |
PluginBase:: |
public | function | Provide a list of additional theme functions for the theme information page | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function | Clears a plugin. | 2 |
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Return the human readable name of the display. | |
PluginBase:: |
public | function | Add anything to the query that we might need to. | 13 |
PluginBase:: |
protected | function | ||
PluginBase:: |
public | function | Returns the summary of the settings in the display. | 6 |
PluginBase:: |
public | function | Provide a full list of possible theme templates used by this style. | 1 |
PluginBase:: |
public | function | Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away. | |
PluginBase:: |
public | function | Returns the usesOptions property. | 8 |
PluginBase:: |
public | function | Validate that the plugin is correct and can be saved. | 4 |
PluginBase:: |
public | function |
Constructs a Plugin object. Overrides PluginBase:: |
2 |
Tid:: |
public | function |
Provide the default form for setting options. Overrides ArgumentDefaultPluginBase:: |
|
Tid:: |
protected | function |
Retrieve the options when this is a new access
control plugin Overrides ArgumentDefaultPluginBase:: |
|
Tid:: |
function |
Return the default argument. Overrides ArgumentDefaultPluginBase:: |
||
Tid:: |
public | function |
Initialize this plugin with the view and the argument
it is linked to. Overrides ArgumentDefaultPluginBase:: |
|
Tid:: |
public | function |
Provide the default form form for submitting options Overrides ArgumentDefaultPluginBase:: |