og-8.x-1.x-dev/src/OgMembershipViewsData.php

src/OgMembershipViewsData.php
<?php

declare(strict_types=1);

namespace Drupal\og;

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\views\EntityViewsData;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the views data for the OG Membership entity type.
 */
class OgMembershipViewsData extends EntityViewsData {

  public function __construct(
    EntityTypeInterface $entity_type,
    SqlEntityStorageInterface $storage_controller,
    EntityTypeManagerInterface $entity_type_manager,
    ModuleHandlerInterface $module_handler,
    TranslationInterface $translation_manager,
    EntityFieldManagerInterface $entity_field_manager,
    protected GroupTypeManagerInterface $groupTypeManager,
  ) {
    parent::__construct($entity_type, $storage_controller, $entity_type_manager, $module_handler, $translation_manager, $entity_field_manager);
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('entity_type.manager')->getStorage($entity_type->id()),
      $container->get('entity_type.manager'),
      $container->get('module_handler'),
      $container->get('string_translation'),
      $container->get('entity_field.manager'),
      $container->get('og.group_type_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getViewsData() {
    $data = parent::getViewsData();
    $data['og_membership']['og_membership_bulk_form'] = [
      'title' => $this->t('Bulk update'),
      'help' => $this->t('Add a form element that lets you run operations on multiple members.'),
      'field' => [
        'id' => 'og_membership_bulk_form',
      ],
    ];

    foreach (array_keys($this->groupTypeManager->getGroupMap()) as $entity_type_id) {
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
      if (!$entity_type instanceof ContentEntityType) {
        continue;
      }

      $base_table = $this->getViewsTableForEntityType($entity_type);
      $data['og_membership']["group_entity_{$entity_type_id}"]['relationship'] = [
        'title' => $this->t('OG Group: @label referenced from OG Membership', ['@label' => $entity_type->getLabel()]),
        'label' => $this->t('OG Group: @label', ['@label' => $entity_type->getLabel()]),
        'help' => $this->t('Relate the OG membership to its OG Group: @label.', ['@label' => $entity_type->getLabel()]),
        'id' => 'standard',
        'base' => $base_table,
        'base field' => $entity_type->getKey('id'),
        'relationship field' => 'entity_id',
        'entity type' => $entity_type_id,
        'extra' => [
          [
            'left_field' => 'entity_type',
            'value' => $entity_type_id,
          ],
        ],
      ];
    }

    return $data;
  }

}

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

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