This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change group role overview page to DraggableListBuilder
- Loading branch information
Showing
6 changed files
with
237 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
} |