diff --git a/og.module b/og.module index 765816f39..88e9384e8 100755 --- a/og.module +++ b/og.module @@ -305,6 +305,7 @@ function og_entity_type_alter(array &$entity_types) { /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ foreach ($entity_types as $entity_type_id => $entity_type) { $entity_type->setLinkTemplate('og-admin-routes', "/group/$entity_type_id/{{$entity_type_id}}/admin"); + $entity_type->setLinkTemplate('og-content', "/group/$entity_type_id/{{$entity_type_id}}/admin/content"); } } diff --git a/src/Controller/OgAdminContentController.php b/src/Controller/OgAdminContentController.php new file mode 100644 index 000000000..849024803 --- /dev/null +++ b/src/Controller/OgAdminContentController.php @@ -0,0 +1,144 @@ +groupTypeManager = $group_type_manager; + $this->groupAudienceHelper = $group_audience_helper; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('og.group_type_manager'), + $container->get('og.group_audience_helper') + ); + } + + /** + * Display content form. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route match service. + * + * @return array + * The content form. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + public function content(RouteMatchInterface $route_match) { + $parameter_name = $route_match->getRouteObject()->getOption('_og_entity_type_id'); + /** @var \Drupal\Core\Entity\EntityInterface $group */ + $group = $route_match->getParameter($parameter_name); + $bundle_ids = $this->groupTypeManager->getAllGroupContentBundleIds($group->getEntityTypeId()); + $results = []; + foreach ($bundle_ids as $entity_type_id => $bundles) { + $results[$entity_type_id] = []; + foreach ($bundles as $bundle) { + $fields = $this->groupAudienceHelper->getAllGroupAudienceFields($entity_type_id, $bundle); + foreach ($fields as $field) { + $storage = $this->entityTypeManager() + ->getStorage($entity_type_id); + $result = $storage->getQuery() + ->condition($field->getName(), $group->id()) + ->execute(); + $results[$entity_type_id] = array_unique(array_merge($results[$entity_type_id], $result)); + } + } + } + + $build = []; + foreach ($results as $entity_type_id => $entity_ids) { + $build[$entity_type_id] = $this->renderListing($entity_type_id, $entity_ids); + } + + return $build; + } + + /** + * Build render array. + * + * @param string $entity_type_id + * The entity type id. + * @param array $entity_ids + * The entity ids. + * + * @return null|array + * Render render array or null if no list builder. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * + * @see \Drupal\Core\Entity\EntityListBuilderInterface::render() + */ + public function renderListing($entity_type_id, array $entity_ids) { + if ($this->entityTypeManager()->hasHandler($entity_type_id, 'list_builder')) { + /** @var \Drupal\Core\Entity\EntityListBuilder $list_builder */ + $list_builder = $this->entityTypeManager() + ->getHandler($entity_type_id, 'list_builder'); + $entity_type = $this->entityTypeManager()->getStorage($entity_type_id)->getEntityType(); + $build['label'] = [ + '#type' => 'html_tag', + '#tag' => 'h3', + '#value' => $entity_type->getLabel(), + ]; + $build['table'] = [ + '#type' => 'table', + '#header' => $list_builder->buildHeader(), + '#title' => $entity_type->getLabel(), + '#rows' => [], + '#empty' => $this->t('There are no @label yet.', ['@label' => $entity_type->getPluralLabel()]), + '#cache' => [ + 'contexts' => $entity_type->getListCacheContexts(), + 'tags' => $entity_type->getListCacheTags(), + ], + ]; + $entities = $this->entityTypeManager()->getStorage($entity_type_id)->loadMultiple($entity_ids); + foreach ($entities as $entity) { + if ($row = $list_builder->buildRow($entity)) { + $build['table']['#rows'][$entity->id()] = $row; + } + } + return $build; + } + } + +} diff --git a/src/EventSubscriber/OgEventSubscriber.php b/src/EventSubscriber/OgEventSubscriber.php index 8d363fab8..81cb4e970 100644 --- a/src/EventSubscriber/OgEventSubscriber.php +++ b/src/EventSubscriber/OgEventSubscriber.php @@ -153,6 +153,12 @@ public function provideDefaultOgPermissions(PermissionEventInterface $event) { 'default roles' => [OgRoleInterface::ADMINISTRATOR], 'restrict access' => TRUE, ]), + new GroupPermission([ + 'name' => 'access content overview', + 'title' => $this->t('Access the Content overview page'), + 'description' => $this->t('User may see and administer content related to the group.'), + 'default roles' => [OgRoleInterface::ADMINISTRATOR], + ]), new GroupPermission([ 'name' => 'administer permissions', 'title' => $this->t('Administer permissions'), @@ -377,6 +383,15 @@ public function provideOgAdminRoutes(OgAdminRoutesEventInterface $event) { '_module_dependencies' => 'views', ], ]; + $routes_info['content'] = [ + 'controller' => '\Drupal\og\Controller\OgAdminContentController::content', + 'title' => 'Group content', + 'description' => 'Listing of group content', + 'path' => 'content', + 'requirements' => [ + '_og_user_access_group' => 'access content overview', + ], + ]; $event->setRoutesInfo($routes_info); }