class CommentViewsHooks

Hook implementations for comment.

Hierarchy

Expanded class hierarchy of CommentViewsHooks

File

core/modules/comment/src/Hook/CommentViewsHooks.php, line 12

Namespace

Drupal\comment\Hook
View source
class CommentViewsHooks {
  use StringTranslationTrait;
  
  /**
   * Implements hook_views_data_alter().
   */
  public function viewsDataAlter(&$data) : void {
    // New comments are only supported for node table because it requires the
    // history table.
    $data['node']['new_comments'] = [
      'title' => $this->t('New comments'),
      'help' => $this->t('The number of new comments on the node.'),
      'field' => [
        'id' => 'node_new_comments',
        'no group by' => TRUE,
      ],
    ];
    // Provides an integration for each entity type except comment.
    foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
        continue;
      }
      $fields = \Drupal::service('comment.manager')->getFields($entity_type_id);
      $base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
      $args = [
        '@entity_type' => $entity_type_id,
      ];
      if ($fields) {
        $data[$base_table]['comments_link'] = [
          'field' => [
            'title' => $this->t('Add comment link'),
            'help' => $this->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',
          ],
        ];
        // Multilingual properties are stored in data table.
        if (!($table = $entity_type->getDataTable())) {
          $table = $entity_type->getBaseTable();
        }
        $data[$table]['uid_touch'] = [
          'title' => $this->t('User posted or commented'),
          'help' => $this->t('Display nodes only if a user posted the @entity_type or commented on the @entity_type.', $args),
          'argument' => [
            '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'),
          ],
          'filter' => [
            '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'),
          ],
        ];
        foreach ($fields as $field_name => $field) {
          $data[$base_table][$field_name . '_cid'] = [
            'title' => $this->t('Comments of the @entity_type using field: @field_name', $args + [
              '@field_name' => $field_name,
            ]),
            'help' => $this->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),
            'relationship' => [
              'group' => $this->t('Comment'),
              'label' => $this->t('Comments'),
              'base' => 'comment_field_data',
              'base field' => 'entity_id',
              'relationship field' => $entity_type->getKey('id'),
              'id' => 'standard',
              'extra' => [
                [
                  'field' => 'entity_type',
                  'value' => $entity_type_id,
                ],
                [
                  'field' => 'field_name',
                  'value' => $field_name,
                ],
              ],
            ],
          ];
        }
      }
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
CommentViewsHooks::viewsDataAlter public function Implements hook_views_data_alter().
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.