Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a content listing route #423

Open
wants to merge 12 commits into
base: 8.x-1.x
Choose a base branch
from
1 change: 1 addition & 0 deletions og.module
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
144 changes: 144 additions & 0 deletions src/Controller/OgAdminContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

declare(strict_types = 1);

namespace Drupal\og\Controller;
MPParsley marked this conversation as resolved.
Show resolved Hide resolved

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\OgGroupAudienceHelperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* OgAdminContentController class.
*/
class OgAdminContentController extends ControllerBase {

/**
* The group type manager.
*
* @var \Drupal\og\GroupTypeManagerInterface
*/
protected $groupTypeManager;

/**
* The group audience helper.
*
* @var \Drupal\og\OgGroupAudienceHelperInterface
*/
protected $groupAudienceHelper;

/**
* OgAdminContentController constructor.
*
* @param \Drupal\og\GroupTypeManagerInterface $group_type_manager
* The group type manager.
* @param \Drupal\og\OgGroupAudienceHelperInterface $group_audience_helper
* The group audience helper.
*/
public function __construct(GroupTypeManagerInterface $group_type_manager, OgGroupAudienceHelperInterface $group_audience_helper) {
$this->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;
}
}

}
15 changes: 15 additions & 0 deletions src/EventSubscriber/OgEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ public function provideDefaultOgPermissions(PermissionEventInterface $event) {
'default roles' => [OgRoleInterface::ADMINISTRATOR],
'restrict access' => TRUE,
]),
new GroupPermission([
'name' => 'access content overview',
'title' => t('Access the Content overview page'),
'description' => t('User may see and administer content related to the group.'),
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
'default roles' => [OgRoleInterface::ADMINISTRATOR],
]),
new GroupPermission([
'name' => 'administer permissions',
'title' => $this->t('Administer permissions'),
Expand Down Expand Up @@ -363,6 +369,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);
}
Expand Down