group-8.x-1.x-dev/src/Hook/ViewsHooks.php

src/Hook/ViewsHooks.php
<?php

declare(strict_types=1);

namespace Drupal\group\Hook;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface;
use Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface;

/**
 * Views hook implementations for Group.
 */
final class ViewsHooks {

  use StringTranslationTrait;

  public function __construct(
    protected EntityTypeManagerInterface $entityTypeManager,
    protected GroupRelationTypeManagerInterface $groupRelationTypeManager,
    TranslationInterface $translation,
  ) {
    $this->stringTranslation = $translation;
  }

  /**
   * Implements hook_views_data_alter().
   */
  #[Hook('views_data_alter')]
  public function viewsDataAlter(array &$data): void {
    $entity_types = $this->entityTypeManager->getDefinitions();

    // Get the data table for GroupRelationship entities.
    $data_table = $entity_types['group_content']->getDataTable();

    // Add views data for all defined plugins so modules can provide default
    // views even though their plugins may not have been installed yet.
    foreach ($this->groupRelationTypeManager->getDefinitions() as $group_relation_type) {
      assert($group_relation_type instanceof GroupRelationTypeInterface);
      $entity_type_id = $group_relation_type->getEntityTypeId();
      if (!isset($entity_types[$entity_type_id])) {
        continue;
      }
      $entity_type = $entity_types[$entity_type_id];
      $entity_data_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();

      // We only add one 'group_content' entry per entity type.
      if (isset($data[$entity_data_table]['group_content'])) {
        continue;
      }

      $t_args = [
        '@entity_type' => $entity_type->getLabel(),
      ];

      // This relationship will allow an entity to easily map to the group
      // relationship entity that ties it to a group, optionally filtering by
      // plugin.
      $data[$entity_data_table]['group_content'] = [
        'title' => $this->t('Group relationship for @entity_type', $t_args),
        'help' => $this->t('Relates to the group relationship entities that represent the @entity_type.', $t_args),
        'relationship' => [
          'group' => $this->t('Group relationship'),
          'base' => $data_table,
          'base field' => 'entity_id',
          'relationship field' => $entity_type->getKey('id'),
          'id' => 'group_content_to_entity_reverse',
          'label' => $this->t('@entity_type group relationship', $t_args),
        ],
      ];
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc