You are here

function flag_views_data_alter in Flag 8.4

Same name and namespace in other branches
  1. 6.2 includes/flag.views.inc \flag_views_data_alter()
  2. 6 includes/flag.views.inc \flag_views_data_alter()
  3. 7.3 includes/views/flag.views.inc \flag_views_data_alter()
  4. 7.2 includes/flag.views.inc \flag_views_data_alter()

Implements hook_views_data_alter().

File

./flag.views.inc, line 56
Contains views API hooks for Flag module.

Code

function flag_views_data_alter(array &$data) {
  $flags = \Drupal::service('flag')
    ->getAllFlags();
  $entity_type_manager = \Drupal::entityTypeManager();
  foreach ($flags as $flag) {
    $entity_type_id = $flag
      ->getFlaggableEntityTypeId();
    $entity_type = $entity_type_manager
      ->getDefinition($entity_type_id);
    if ($entity_type
      ->hasHandlerClass('views_data')) {
      $base_table = $entity_type_manager
        ->getHandler($entity_type_id, 'views_data')
        ->getViewsTableForEntityType($entity_type);
      $data[$base_table]['flag_relationship'] = [
        'title' => t('@entity_label flag', [
          '@entity_label' => $entity_type
            ->getLabel(),
        ]),
        'help' => t('Limit results to only those entity flagged by a certain flag; Or display information about the flag set on a entity.'),
        'relationship' => [
          'group' => t('Flag'),
          'label' => t('Flags'),
          'base' => 'flagging',
          'base field' => 'entity_id',
          'relationship field' => $entity_type
            ->getKey('id'),
          'id' => 'flag_relationship',
          'flaggable' => $entity_type_id,
        ],
      ];
    }
  }
}