2015-08-18 00:00:26 +00:00
< ? php
/**
* @ file
* Provide views data for comment . module .
*/
2017-04-13 14:53:35 +00:00
use Drupal\Core\Entity\ContentEntityInterface ;
2015-08-18 00:00:26 +00:00
/**
* Implements hook_views_data_alter () .
*/
function comment_views_data_alter ( & $data ) {
// New comments are only supported for node table because it requires the
// history table.
2017-04-13 14:53:35 +00:00
$data [ 'node' ][ 'new_comments' ] = [
2015-08-18 00:00:26 +00:00
'title' => t ( 'New comments' ),
'help' => t ( 'The number of new comments on the node.' ),
2017-04-13 14:53:35 +00:00
'field' => [
2015-08-18 00:00:26 +00:00
'id' => 'node_new_comments' ,
'no group by' => TRUE ,
2017-04-13 14:53:35 +00:00
],
];
2015-08-18 00:00:26 +00:00
// Provide a integration for each entity type except comment.
foreach ( \Drupal :: entityManager () -> getDefinitions () as $entity_type_id => $entity_type ) {
2017-04-13 14:53:35 +00:00
if ( $entity_type_id == 'comment' || ! $entity_type -> entityClassImplements ( ContentEntityInterface :: class ) || ! $entity_type -> getBaseTable ()) {
2015-08-18 00:00:26 +00:00
continue ;
}
$fields = \Drupal :: service ( 'comment.manager' ) -> getFields ( $entity_type_id );
$base_table = $entity_type -> getDataTable () ? : $entity_type -> getBaseTable ();
2017-04-13 14:53:35 +00:00
$args = [ '@entity_type' => $entity_type_id ];
2015-08-18 00:00:26 +00:00
if ( $fields ) {
2017-04-13 14:53:35 +00:00
$data [ $base_table ][ 'comments_link' ] = [
'field' => [
2015-08-18 00:00:26 +00:00
'title' => t ( 'Add comment link' ),
'help' => t ( 'Display the standard add comment link used on regular @entity_type, which will only display if the viewing user has access to add a comment.' , $args ),
'id' => 'comment_entity_link' ,
2017-04-13 14:53:35 +00:00
],
];
2015-08-18 00:00:26 +00:00
// Multilingual properties are stored in data table.
if ( ! ( $table = $entity_type -> getDataTable ())) {
$table = $entity_type -> getBaseTable ();
}
2017-04-13 14:53:35 +00:00
$data [ $table ][ 'uid_touch' ] = [
2015-08-18 00:00:26 +00:00
'title' => t ( 'User posted or commented' ),
'help' => t ( 'Display nodes only if a user posted the @entity_type or commented on the @entity_type.' , $args ),
2017-04-13 14:53:35 +00:00
'argument' => [
2015-08-18 00:00:26 +00:00
'field' => 'uid' ,
'name table' => 'users_field_data' ,
'name field' => 'name' ,
'id' => 'argument_comment_user_uid' ,
'no group by' => TRUE ,
'entity_type' => $entity_type_id ,
'entity_id' => $entity_type -> getKey ( 'id' ),
2017-04-13 14:53:35 +00:00
],
'filter' => [
2015-08-18 00:00:26 +00:00
'field' => 'uid' ,
'name table' => 'users_field_data' ,
'name field' => 'name' ,
'id' => 'comment_user_uid' ,
'entity_type' => $entity_type_id ,
'entity_id' => $entity_type -> getKey ( 'id' ),
2017-04-13 14:53:35 +00:00
],
];
2015-08-18 00:00:26 +00:00
foreach ( $fields as $field_name => $field ) {
2017-04-13 14:53:35 +00:00
$data [ $base_table ][ $field_name . '_cid' ] = [
'title' => t ( 'Comments of the @entity_type using field: @field_name' , $args + [ '@field_name' => $field_name ]),
2015-08-18 00:00:26 +00:00
'help' => t ( 'Relate all comments on the @entity_type. This will create 1 duplicate record for every comment. Usually if you need this it is better to create a comment view.' , $args ),
2017-04-13 14:53:35 +00:00
'relationship' => [
2015-08-18 00:00:26 +00:00
'group' => t ( 'Comment' ),
'label' => t ( 'Comments' ),
'base' => 'comment_field_data' ,
'base field' => 'entity_id' ,
'relationship field' => $entity_type -> getKey ( 'id' ),
'id' => 'standard' ,
2017-04-13 14:53:35 +00:00
'extra' => [
[
2015-08-18 00:00:26 +00:00
'field' => 'entity_type' ,
'value' => $entity_type_id ,
2017-04-13 14:53:35 +00:00
],
[
2015-08-18 00:00:26 +00:00
'field' => 'field_name' ,
'value' => $field_name ,
2017-04-13 14:53:35 +00:00
],
],
],
];
2015-08-18 00:00:26 +00:00
}
}
}
}