2009-05-17 11:16:51 +00:00
< ? php
/**
* @ file
2014-07-08 09:47:24 +00:00
* Provides views data for taxonomy . module .
2009-05-17 11:16:51 +00:00
*/
2014-07-18 18:56:27 +00:00
use Drupal\field\FieldStorageConfigInterface ;
2013-09-01 06:20:08 +00:00
2009-05-17 11:16:51 +00:00
/**
* Implements hook_views_data_alter () .
*/
function taxonomy_views_data_alter ( & $data ) {
$data [ 'node' ][ 'term_node_tid' ] = array (
'title' => t ( 'Taxonomy terms on node' ),
2013-12-03 15:54:20 +00:00
'help' => t ( 'Relate nodes to taxonomy terms, specifying which vocabulary or vocabularies to use. This relationship will cause duplicated records if there are multiple terms.' ),
2009-05-17 11:16:51 +00:00
'relationship' => array (
2012-08-11 11:46:07 +00:00
'id' => 'node_term_data' ,
2009-05-17 11:16:51 +00:00
'label' => t ( 'term' ),
'base' => 'taxonomy_term_data' ,
),
'field' => array (
'title' => t ( 'All taxonomy terms' ),
'help' => t ( 'Display all taxonomy terms associated with a node from specified vocabularies.' ),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid' ,
2009-05-17 11:16:51 +00:00
'no group by' => TRUE ,
2013-01-21 20:59:01 +00:00
'click sortable' => FALSE ,
2009-05-17 11:16:51 +00:00
),
);
$data [ 'node' ][ 'term_node_tid_depth' ] = array (
'help' => t ( 'Display content if it has the selected taxonomy terms, or children of the selected terms. Due to additional complexity, this has fewer options than the versions without depth.' ),
'real field' => 'nid' ,
'argument' => array (
'title' => t ( 'Has taxonomy term ID (with depth)' ),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid_depth' ,
2009-05-17 11:16:51 +00:00
'accept depth modifier' => TRUE ,
),
'filter' => array (
'title' => t ( 'Has taxonomy terms (with depth)' ),
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid_depth' ,
2009-05-17 11:16:51 +00:00
),
);
$data [ 'node' ][ 'term_node_tid_depth_modifier' ] = array (
'title' => t ( 'Has taxonomy term ID depth modifier' ),
'help' => t ( 'Allows the "depth" for Taxonomy: Term ID (with depth) to be modified via an additional contextual filter value.' ),
'argument' => array (
2012-08-11 11:46:07 +00:00
'id' => 'taxonomy_index_tid_depth_modifier' ,
2009-05-17 11:16:51 +00:00
),
);
}
/**
* Implements hook_field_views_data () .
*
* Views integration for taxonomy_term_reference fields . Adds a term relationship to the default
* field data .
*
* @ see field_views_field_default_views_data ()
*/
2014-07-18 18:56:27 +00:00
function taxonomy_field_views_data ( FieldStorageConfigInterface $field_storage ) {
$data = field_views_field_default_views_data ( $field_storage );
2009-05-17 11:16:51 +00:00
foreach ( $data as $table_name => $table_data ) {
foreach ( $table_data as $field_name => $field_data ) {
if ( isset ( $field_data [ 'filter' ]) && $field_name != 'delta' ) {
2012-08-11 11:46:07 +00:00
$data [ $table_name ][ $field_name ][ 'filter' ][ 'id' ] = 'taxonomy_index_tid' ;
2014-07-18 18:56:27 +00:00
$allowed_values = $field_storage -> getSetting ( 'allowed_values' );
2013-10-04 07:55:32 +00:00
$data [ $table_name ][ $field_name ][ 'filter' ][ 'vocabulary' ] = $allowed_values [ 0 ][ 'vocabulary' ];
2009-05-17 11:16:51 +00:00
}
}
// Add the relationship only on the tid field.
2014-07-18 18:56:27 +00:00
$field_name = $field_storage -> getName ();
2013-10-04 07:55:32 +00:00
$data [ $table_name ][ $field_name . '_target_id' ][ 'relationship' ] = array (
2012-08-11 11:46:07 +00:00
'id' => 'standard' ,
2009-05-17 11:16:51 +00:00
'base' => 'taxonomy_term_data' ,
'base field' => 'tid' ,
2013-10-04 07:55:32 +00:00
'label' => t ( 'term from !field_name' , array ( '!field_name' => $field_name )),
2009-05-17 11:16:51 +00:00
);
}
return $data ;
}
/**
* Implements hook_field_views_data_views_data_alter () .
*
* Views integration to provide reverse relationships on term references .
*/
2014-07-18 18:56:27 +00:00
function taxonomy_field_views_data_views_data_alter ( array & $data , FieldStorageConfigInterface $field_storage ) {
$field_name = $field_storage -> getName ();
2014-08-29 22:38:20 +00:00
$entity_type_id = $field_storage -> getTargetEntityTypeId ();
$entity_manager = \Drupal :: entityManager ();
$entity_type = $entity_manager -> getDefinition ( $entity_type_id );
2014-02-10 09:24:05 +00:00
$pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id ;
2014-08-29 22:38:20 +00:00
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_manager -> getStorage ( $entity_type_id ) -> getTableMapping ();
2013-09-01 06:20:08 +00:00
2014-02-10 09:24:05 +00:00
list ( $label ) = field_views_field_label ( $entity_type_id , $field_name );
2013-09-01 06:20:08 +00:00
$data [ 'taxonomy_term_data' ][ $pseudo_field_name ][ 'relationship' ] = array (
2014-02-10 09:24:05 +00:00
'title' => t ( '@entity using @field' , array ( '@entity' => $entity_type -> getLabel (), '@field' => $label )),
'help' => t ( 'Relate each @entity with a @field set to the term.' , array ( '@entity' => $entity_type -> getLabel (), '@field' => $label )),
2013-09-01 06:20:08 +00:00
'id' => 'entity_reverse' ,
2013-10-04 07:55:32 +00:00
'field_name' => $field_name ,
2014-02-10 09:24:05 +00:00
'entity_type' => $entity_type_id ,
2014-08-29 22:38:20 +00:00
'field table' => $table_mapping -> getDedicatedDataTableName ( $field_storage ),
2013-10-04 07:55:32 +00:00
'field field' => $field_name . '_target_id' ,
2014-02-10 09:24:05 +00:00
'base' => $entity_type -> getBaseTable (),
'base field' => $entity_type -> getKey ( 'id' ),
2013-10-04 07:55:32 +00:00
'label' => t ( '!field_name' , array ( '!field_name' => $field_name )),
2013-09-01 06:20:08 +00:00
'join_extra' => array (
0 => array (
'field' => 'deleted' ,
'value' => 0 ,
'numeric' => TRUE ,
2009-05-17 11:16:51 +00:00
),
2013-09-01 06:20:08 +00:00
),
);
2009-05-17 11:16:51 +00:00
}
2013-12-04 12:18:02 +00:00
/**
* Implements hook_views_plugins_argument_validator_alter () .
*
* Extend the generic entity argument validator .
*
* @ see \Drupal\views\Plugin\views\argument_validator\Entity
*/
function taxonomy_views_plugins_argument_validator_alter ( array & $plugins ) {
$plugins [ 'entity:taxonomy_term' ][ 'title' ] = t ( 'Taxonomy term ID' );
$plugins [ 'entity:taxonomy_term' ][ 'class' ] = 'Drupal\taxonomy\Plugin\views\argument_validator\Term' ;
$plugins [ 'entity:taxonomy_term' ][ 'provider' ] = 'taxonomy' ;
}