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

WIP: Group roles UI #196

Open
wants to merge 32 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e7b97e9
Merge branch 'roles-ui' into group-roles-ui
pfrenssen May 13, 2016
ab106fe
Start implementing the roles overview.
pfrenssen May 13, 2016
5c3d853
Add provisionary routes for the roles and permissions edit forms so w…
pfrenssen May 14, 2016
713d8ea
Add a method to determine whether a role is mutable.
pfrenssen May 14, 2016
0b0d53e
Show the links for editing roles and permissions in the table.
pfrenssen May 14, 2016
ca2bc0e
Merge branch 'roles-ui' into group-roles-ui
pfrenssen May 14, 2016
0fb28c6
Merge branch 'default-roles' into group-roles-ui
pfrenssen May 14, 2016
3edfcd4
Change the value of the administrator role name so it doesn't contain…
pfrenssen May 14, 2016
479c341
Merge branch 'default-roles' into group-roles-ui
pfrenssen May 14, 2016
1eb0375
Turn the roles overview from a form into a controller.
pfrenssen May 14, 2016
2c69737
Provide a local action to add a role.
pfrenssen May 14, 2016
39a086b
Return a 404 on the group role overview when the entity is not a group.
pfrenssen May 14, 2016
03597f4
Clean up whitespace.
pfrenssen May 14, 2016
49b5c8d
Merge branch 'default-roles' into group-roles-ui
pfrenssen May 17, 2016
5b95dd4
Merge branch 'default-roles' into group-roles-ui
pfrenssen May 26, 2016
cda9868
Merge branch 'default-roles' into group-roles-ui
pfrenssen Jun 1, 2016
be6b209
Add a skeleton for the roles form.
pfrenssen Jun 1, 2016
b5a8bdb
Change the method from isMutable() to isLocked() so it matches what i…
pfrenssen Jun 1, 2016
ecd3185
Update documentation.
pfrenssen Jun 1, 2016
683a190
Revert "Add a skeleton for the roles form."
pfrenssen Jun 1, 2016
5d193db
isMutable() has been inverted and is now called isLocked().
pfrenssen Jun 2, 2016
91b8bef
Add links to the OG Role entity declaration.
pfrenssen Jun 2, 2016
ca7cfc7
Implement the role add/edit form.
pfrenssen Jun 2, 2016
bf11cba
Use the correct naming scheme for entity forms.
pfrenssen Jun 2, 2016
73910d9
Fix redirect link to role overview after editing a role.
pfrenssen Jun 2, 2016
0c39dd7
Actually check if a property gets changed before complaining.
pfrenssen Jun 2, 2016
ac995b2
Merge branch 'default-roles' into group-roles-ui
pfrenssen Jun 8, 2016
8615938
Merge 8.x-1.x
amitaibu Aug 23, 2016
0ef056c
Role add controller and small form modification.
ArchieVasyatkinMonsoon Feb 1, 2017
2fdd29f
Merge branch '8.x-1.x' into group-roles-ui
ArchieDoe Feb 1, 2017
dc2c6a4
Adding reoute for creation group-specific role links, restoring overv…
ArchieVasyatkinMonsoon Feb 1, 2017
946bf7d
Merge branch '8.x-1.x' into group-roles-ui
damienmckenna Oct 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions og_ui/og_ui.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
og_ui.role_add:
route_name: og_ui.role_add_form
title: 'Add role'
appears_on:
- og_ui.roles_overview
16 changes: 16 additions & 0 deletions og_ui/og_ui.links.menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ og_ui.settings:
parent: og_ui.admin_index
description: 'Administer OG settings.'
route_name: og_ui.settings

og_ui.roles_overview:
title: 'OG roles'
parent: og_ui.admin_index
description: 'Administer OG roles.'
route_name: og_ui.roles_permissions_overview
route_parameters:
type: 'roles'

og_ui.permissions_overview:
title: 'OG permissions'
parent: og_ui.admin_index
description: 'Administer OG permissions.'
route_name: og_ui.roles_permissions_overview
route_parameters:
type: 'permissions'
46 changes: 42 additions & 4 deletions og_ui/og_ui.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,56 @@ og_ui.roles_permissions_overview:
_permission: 'administer group'
type: '^(roles|permissions)$'

og_ui.roles_form:
og_ui.roles_overview:
path: 'admin/config/group/roles/{entity_type}/{bundle}'
defaults:
_form: '\Drupal\og_ui\Form\OgRolesForm'
_title: '@todo - create title callback'
_controller: '\Drupal\og_ui\Controller\OgUiController::rolesOverviewPage'
_title_callback: '\Drupal\og_ui\Controller\OgUiController::rolesOverviewPageTitleCallback'
requirements:
_permission: 'administer group'

og_ui.role_add_form:
path: 'admin/config/group/role/{entity_type}/{bundle}/add'
defaults:
_controller: '\Drupal\og_ui\Controller\OgRoleController::addGroupTypeRole'
_title_callback: '\Drupal\og_ui\Form\OgRoleForm::addRoleTitleCallback'
requirements:
# Todo: This should become a dedicated permission:
# _entity_access: og_role.add
_permission: 'administer group'

og_ui.group_role_add_form:
path: 'admin/config/group/role/{entity_type}/{entity}/add'
defaults:
_controller: '\Drupal\og_ui\Controller\OgRoleController::addGroupRole'
_title_callback: '\Drupal\og_ui\Form\OgRoleForm::addRoleTitleCallback'
requirements:
# Todo: This should become a dedicated permission:
# _entity_access: og_role.add
_permission: 'administer group'

og_ui.permissions_form:
entity.og_role.edit_form:
path: 'admin/config/group/role/{og_role}/edit'
defaults:
_entity_form: og_role.default
_title_callback: '\Drupal\og_ui\Form\OgRoleForm::editRoleTitleCallback'
requirements:
# Todo: This should become a dedicated permission:
# _entity_access: og_role.update
_permission: 'administer group'

og_ui.permissions_overview:
path: 'admin/config/group/permissions/{entity_type}/{bundle}'
defaults:
_form: '\Drupal\og_ui\Form\OgPermissionsForm'
_title: '@todo - create title callback'
requirements:
_permission: 'administer group'

og_ui.permissions_edit_form:
path: 'admin/config/group/permission/{og_role}/edit'
defaults:
_form: '\Drupal\og_ui\Form\OgPermissionsEditForm'
_title_callback: '\Drupal\og_ui\Form\OgPermissionsEditForm::titleCallback'
requirements:
_permission: 'administer group'
45 changes: 45 additions & 0 deletions og_ui/src/Controller/OgRoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drupal\og_ui\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\Entity;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;

/**
* Controller for the og_role entity.
*
* @see \Drupal\og\Entity\OgRole.
*/
class OgRoleController extends ControllerBase implements ContainerInjectionInterface {

/**
* Form constructor for OgRole add form.
*
* @param string $entity_type
* Group type.
* @param string $bundle
* Group bundle.
*
* @return array
* An associative array containing:
* - og_role_form: The og_role form as a renderable array.
*/
public function addGroupTypeRole($entity_type, $bundle) {
$build['#title'] = $this->t('Create New Role');

// Show the actual reply box.
$og_role = $this->entityManager()->getStorage('og_role')->create(array(
'group_type' => $entity_type,
'group_bundle' => $bundle,
));
$build['og_role_form'] = $this->entityFormBuilder()->getForm($og_role);

return $build;
}

public function addGroupRole(Entity $entity) {

}

}
85 changes: 83 additions & 2 deletions og_ui/src/Controller/OgUiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\og\Og;
use Drupal\og\GroupTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* The OG UI controller.
Expand Down Expand Up @@ -86,7 +89,7 @@ public function rolesPermissionsOverviewPage($type) {
'data' => $definition->getLabel() . ' - ' . $bundle_info[$bundle]['label'],
],
[
'data' => Link::createFromRoute($action, 'og_ui.' . $type . '_form', [
'data' => Link::createFromRoute($action, 'og_ui.' . $type . '_overview', [
'entity_type' => $entity_type,
'bundle' => $bundle,
]),
Expand All @@ -106,7 +109,7 @@ public function rolesPermissionsOverviewPage($type) {
}

/**
* Title callback for rolesPermissionsOverviewPage.
* Title callback for rolesPermissionsOverviewPage().
*
* @param string $type
* The type of overview, either 'roles' or 'permissions'.
Expand All @@ -118,4 +121,82 @@ 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()]];

// Add the edit role link if the role is editable.
if (!$role->isLocked()) {
$columns[] = [
'data' => Link::createFromRoute($this->t('Edit role'), 'entity.og_role.edit_form', [
'og_role' => $role->id(),
]),
];
}
else {
$columns[] = ['data' => $this->t('Locked')];
}

// Add the edit permissions link.
$columns[] = [
'data' => Link::createFromRoute($this->t('Edit permissions'), 'og_ui.permissions_edit_form', [
'og_role' => $role->id(),
]),
];

$rows[] = $columns;
}

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

/**
* Title callback for the roles overview page.
*
* @param string $entity_type
* The group entity type.
* @param string $bundle
* The group bundle.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
*/
public function rolesOverviewPageTitleCallback($entity_type, $bundle) {
return $this->t('OG @type - @bundle roles', [
'@type' => $this->entityTypeManager->getDefinition($entity_type)->getLabel(),
'@bundle' => $this->entityTypeBundleInfo->getBundleInfo($entity_type)[$bundle]['label'],
]);
}

}
111 changes: 111 additions & 0 deletions og_ui/src/Form/OgRoleForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php


namespace Drupal\og_ui\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\og\Entity\OgRole;

/**
* Form to add or edit an OG role.
*/
class OgRoleForm extends EntityForm {

/**
* The entity being used by this form.
*
* @var \Drupal\og\Entity\OgRole
*/
protected $entity;

/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Role name'),
'#default_value' => $entity->label(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => $this->t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
];
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => $entity->id(),
'#required' => TRUE,
'#disabled' => !$entity->isNew(),
'#size' => 30,
'#maxlength' => 64,
'#machine_name' => array(
'exists' => ['\Drupal\og_uid\Entity\OgRole', 'load'],
),
);
$form['weight'] = array(
'#type' => 'value',
'#value' => $entity->getWeight(),
);

return parent::form($form, $form_state, $entity);
}

/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;

// Prevent leading and trailing spaces in role names.
$entity->set('label', trim($entity->label()));
$entity->set('name', trim($entity->get('name')));
$status = $entity->save();

$edit_link = $this->entity->link($this->t('Edit'));
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('OG role %label has been updated.', ['%label' => $entity->label()]));
$this->logger('user')->notice('OG role %label has been updated.', ['%label' => $entity->label(), 'link' => $edit_link]);
}
else {
drupal_set_message($this->t('OG role %label has been added.', ['%label' => $entity->label()]));
$this->logger('user')->notice('OG role %label has been added.', ['%label' => $entity->label(), 'link' => $edit_link]);
}
$form_state->setRedirect('og_ui.roles_overview', [
'entity_type' => $entity->getGroupType(),
'bundle' => $entity->getGroupBundle(),
]);
}

/**
* Title callback for the edit form for an OG role.
*
* @param \Drupal\og\Entity\OgRole $og_role
* The OG role being edited.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* An object that, when cast to a string, returns the translated title
* callback.
*/
public function editRoleTitleCallback(OgRole $og_role) {
return $this->t('Edit OG role %label', [
'%label' => $og_role->getLabel(),
]);
}

/**
* @param string $bundle
* Entity type bundle.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* An object that, when cast to a string, returns the translated title
* callback.
*/
public function addRoleTitleCallback($bundle) {
return $this->t('Create %bundle OG role', [
'%bundle' => $bundle,
]);
}

}
19 changes: 19 additions & 0 deletions src/Entity/OgRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@
* @ConfigEntityType(
* id = "og_role",
* label = @Translation("OG role"),
* handlers = {
* "form" = {
* "default" = "Drupal\og_ui\Form\OgRoleForm",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm"
* }
* },
* static_cache = TRUE,
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "weight" = "weight"
* },
* links = {
* "delete-form" = "/admin/config/group/role/{og_role}/delete",
* "edit-form" = "/admin/config/group/role/{og_role}/edit",
* "edit-permissions-form" = "/admin/config/group/permission/{og_role}/edit",
* "collection" = "/admin/config/group/roles",
* },
* config_export = {
* "id",
* "label",
Expand Down Expand Up @@ -135,6 +147,13 @@ public function setRoleType($role_type) {
return $this->set('role_type', $role_type);
}

/**
* {@inheritdoc}
*/
public function isLocked() {
return $this->get('role_type') !== OgRoleInterface::ROLE_TYPE_STANDARD;
}

/**
* {@inheritdoc}
*/
Expand Down
Loading