Skip to content

Commit

Permalink
Change group role overview page to DraggableListBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Apr 12, 2017
1 parent c3118ce commit 2193a8f
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 93 deletions.
5 changes: 2 additions & 3 deletions og_ui/og_ui.routing.yml
Expand Up @@ -26,10 +26,9 @@ og_ui.roles_permissions_overview:
type: '^(roles|permissions)$'

og_ui.roles_overview:
path: 'admin/config/group/roles/{entity_type}/{bundle}'
path: 'admin/config/group/roles/{entity_type_id}/{bundle}'
defaults:
_controller: '\Drupal\og_ui\Controller\OgUiController::rolesOverviewPage'
_title_callback: '\Drupal\og_ui\Controller\OgUiController::rolesOverviewPageTitleCallback'
_entity_list: 'og_role'
requirements:
_permission: 'administer group'

Expand Down
86 changes: 1 addition & 85 deletions og_ui/src/Controller/OgUiController.php
Expand Up @@ -90,7 +90,7 @@ public function rolesPermissionsOverviewPage($type) {
],
[
'data' => Link::createFromRoute($action, 'og_ui.' . $type . '_overview', [
'entity_type' => $entity_type,
'entity_type_id' => $entity_type,
'bundle' => $bundle,
]),
],
Expand Down Expand Up @@ -121,90 +121,6 @@ public function rolesPermissionsOverviewTitleCallback($type) {
return $this->t('OG @type overview', ['@type' => $type]);
}

/**
* Returns the OG role overview page for a given entity type and bundle.
*
* @param string $entity_type
* The entity type for which to show the OG roles.
* @param string $bundle
* The bundle for which to show the OG roles.
*
* @return array
* The overview as a render array.
*/
public function rolesOverviewPage($entity_type = '', $bundle = '') {
// Return a 404 error when this is not a group.
if (!Og::isGroup($entity_type, $bundle)) {
throw new NotFoundHttpException();
}

$rows = [];

$properties = [
'group_type' => $entity_type,
'group_bundle' => $bundle,
];
/** @var \Drupal\og\Entity\OgRole $role */
foreach ($this->entityTypeManager->getStorage('og_role')->loadByProperties($properties) as $role) {
// Add the role name cell.
$columns = [['data' => $role->getLabel()]];
$operations = [];
$edit_permissions = [
'title' => $this->t('Edit permissions'),
'weight' => 10,
'url' => Url::fromRoute('og_ui.permissions_edit_form', [
'entity_type' => $entity_type,
'bundle' => $bundle,
'og_role' => $role->id(),
]),
];

// Add the edit role link if the role is editable.
if (!$role->isLocked()) {
if ($role->access('update') && $role->hasLinkTemplate('edit-form')) {
$operations['edit'] = [
'title' => $this->t('Edit role'),
'weight' => 10,
'url' => $role->urlInfo('edit-form'),
];
}

$operations['permissions'] = $edit_permissions;
if ($role->access('delete') && $role->hasLinkTemplate('delete-form')) {
$operations['delete'] = [
'title' => $this->t('Delete role'),
'weight' => 100,
'url' => $role->urlInfo('delete-form'),
];
}
}
else {
// Add the edit permissions link.
// @TODO investigate using a clean id string (id() is "node-bundle-rid").
$operations['permissions'] = $edit_permissions;
}

$columns[] = [
'data' => [
'#type' => 'operations',
'#links' => $operations,
],
];

$rows[] = $columns;
}

return [
'#theme' => 'table',
'#header' => [
$this->t('Role name'),
['data' => $this->t('Operations')],
],
'#rows' => $rows,
'#empty' => $this->t('No roles available.'),
];
}

/**
* Title callback for the roles overview page.
*
Expand Down
2 changes: 1 addition & 1 deletion og_ui/src/Form/OgRoleForm.php
Expand Up @@ -40,7 +40,7 @@ public function form(array $form, FormStateInterface $form_state) {
// The actual role id is <ENTITY TYPE>-<BUNDLE>-<ID>
// Given the machine_name constraints, we go back to ID here
// as the full id is assembled in OgRole::save().
list($entity_type, , $role_id) = explode('-', $og_role->id(), 3);
list(, , $role_id) = explode('-', $og_role->id(), 3);
}

$form['name'] = [
Expand Down
12 changes: 8 additions & 4 deletions src/Entity/OgRole.php
Expand Up @@ -28,7 +28,11 @@
* "default" = "Drupal\og_ui\Form\OgRoleForm",
* "delete" = "Drupal\og_ui\Form\OgRoleDeleteForm",
* "edit" = "Drupal\og_ui\Form\OgRoleForm",
* }
* },
* "route_provider" = {
* "html" = "Drupal\og\Entity\OgRoleRouteProvider",
* },
* "list_builder" = "Drupal\og\Entity\OgRoleListBuilder",
* },
* admin_permission = "administer group",
* static_cache = TRUE,
Expand All @@ -38,10 +42,10 @@
* "weight" = "weight"
* },
* links = {
* "delete-form" = "/admin/config/group/role/{og_role}/delete",
* "add-form" = "/admin/config/group/role/{entity_type}/{bundle}/add",
* "edit-form" = "/admin/config/group/role/{og_role}/edit",
* "edit-permissions-form" = "/admin/config/group/permission/{og_role}/edit",
* "collection" = "/admin/config/group/roles",
* "delete-form" = "/admin/config/group/role/{og_role}/delete",
* "collection" = "/admin/config/group/roles/{entity_type_id}/{bundle}",
* },
* config_export = {
* "id",
Expand Down
165 changes: 165 additions & 0 deletions src/Entity/OgRoleListBuilder.php
@@ -0,0 +1,165 @@
<?php

namespace Drupal\og\Entity;

use Drupal\Core\Url;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\og\Og;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Defines a class to build a listing of og_role entities.
*
* @see \Drupal\og\Entity\OgRole
*/
class OgRoleListBuilder extends DraggableListBuilder {

/**
* The group entity type.
*
* @var string
*/
protected $group_type;

/**
* The group entity bundle id.
*
* @var string
*/
protected $group_bundle;

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

/**
* Constructs a new OgRoleListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route matcher.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RouteMatchInterface $route_match) {
parent::__construct($entity_type, $storage);

// When on the default og_role list route,
//we should have a group entity type and bundle.
if ($route_match->getRouteName() == 'entity.og_role.collection') {
$parameters = $route_match->getParameters();

// Check if the route had a group type parameter.
if ($parameters->has('entity_type_id')) {
$this->group_type = $parameters->get('entity_type_id');
}
if ($parameters->has('bundle')) {
$this->group_bundle = $parameters->get('bundle');
}
}
}

/**
* {@inheritdoc}
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery()
->condition('group_type', $this->group_type, '=')
->condition('group_bundle', $this->group_bundle, '=')
->sort($this->entityType->getKey('weight'));

// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}

return array_values($query->execute());
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'og_roles_admin';
}

/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = t('Name');
return $header + parent::buildHeader();
}

/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
return $row + parent::buildRow($entity);
}

/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $role) {
$operations = parent::getDefaultOperations($role);

// @TODO if ($entity->hasLinkTemplate('edit-permissions-form')).
$operations['permissions'] = [
'title' => t('Edit permissions'),
'weight' => 20,
'url' => Url::fromRoute('og_ui.permissions_edit_form', [
'entity_type' => $this->group_type,
'bundle' => $this->group_bundle,
'og_role' => $role->id(),
]),
];

if ($role->isLocked()) {
if (isset($operations['edit'])) {
unset($operations['edit']);
}

if (isset($operations['delete'])) {
unset($operations['delete']);
}
}

return $operations;
}

/**
* {@inheritdoc}
*/
public function render() {
// Return a 404 error when this is not a group.
if (!Og::isGroup($this->group_type, $this->group_bundle)) {
throw new NotFoundHttpException();
}

$build = parent::render();
$build['entities']['#empty'] = $this->t('There are no OG roles available yet. <a href="@link">Add an OG role</a>.', [
'@link' => Url::fromRoute('entity.og_role.add_form', [
'entity_type' => $this->group_type,
'bundle' => $this->group_bundle,
])->toString()
]);

return $build;
}

}
60 changes: 60 additions & 0 deletions src/Entity/OgRoleRouteProvider.php
@@ -0,0 +1,60 @@
<?php

namespace Drupal\og\Entity;


use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;

/**
* Provides routes for og_roles.
*/
class OgRoleRouteProvider extends AdminHtmlRouteProvider {

/**
* {@inheritdoc}
*/
protected function getAddFormRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getAddFormRoute($entity_type)) {
$route->setOption('parameters', ['entity_type' => ['type' => 'entity:{entity_type}']])
->setOption('parameters', ['bundle' => ['type' => '{entity_type}:{bundle}']]);
return $route;
}
}

/**
* {@inheritdoc}
*/
protected function getEditFormRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getEditFormRoute($entity_type)) {
$route->setDefault('_title_callback', '\Drupal\og_ui\Form\OgRoleForm::editRoleTitleCallback')
->setOption('parameters', ['group_type' => ['type' => 'entity:group_type']])
->setRequirement('_entity_access', 'og_role.update');
return $route;
}
}

/**
* {@inheritdoc}
*/
protected function getDeleteFormRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getDeleteFormRoute($entity_type)) {
$route->setDefault('_title_callback', '\Drupal\og_ui\Form\OgRoleForm::editRoleTitleCallback');
$route->setOption('parameters', ['group_type' => ['type' => 'entity:group_type']]);
return $route;
}
}

/**
* {@inheritdoc}
*/
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getCollectionRoute($entity_type)) {
$route->setDefault('_title_callback', '\Drupal\og_ui\Controller\OgUiController::rolesOverviewPageTitleCallback')
->setOption('parameters', ['entity_type' => ['type' => 'entity:{entity_type}']])
->setRequirement('_permission', 'administer group');
return $route;
}
}

}

0 comments on commit 2193a8f

Please sign in to comment.