Skip to content

Commit

Permalink
Issue #3280811 by vnech: Make possible for group managers manage book…
Browse files Browse the repository at this point in the history
…s structure
  • Loading branch information
nechai committed Jun 29, 2022
1 parent 8cf6976 commit cedf6e0
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 160 deletions.
2 changes: 1 addition & 1 deletion modules/social_features/social_book/social_book.install
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function social_book_update_10301() {
/**
* Hide book image field labels in view displays.
*/
function social_book_update_11400(): string {
function social_book_update_11401(): string {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: 'Social Flexible Group - Book pages'
description: 'Provides an ability to have Book Page node type in flexible groups.'
type: module
core_version_requirement: ^9
package: Social
package: Social (Experimental)
lifecycle: experimental
hidden: true
dependencies:
- social:social_group_flexible_group
- social:social_book
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,85 @@
* Install and update functions for the "social_flexible_group_book" module.
*/

use Drupal\social_flexible_group_book\SocialFlexibleGroupBookInstallHelper;

/**
* Implements hook_install().
*/
function social_flexible_group_book_install(): void {
/** @var \Drupal\social_flexible_group_book\SocialFlexibleGroupBookInstallHelper $install_helper */
$install_helper = \Drupal::classResolver(SocialFlexibleGroupBookInstallHelper::class);
$install_helper->setGroupPermissions();
}
// Make this module hooks fired after "book" module.
$extension_config = \Drupal::configFactory()->getEditable('core.extension');
$weight = $extension_config->get('module.book');
// Set the weight of this module.
module_set_weight('social_flexible_group_book', $weight + 1);

/**
* Implements hook_uninstall().
*/
function social_group_flexible_group_uninstall(bool $is_syncing): void {
/** @var \Drupal\social_flexible_group_book\SocialFlexibleGroupBookInstallHelper $install_helper */
$install_helper = \Drupal::classResolver(SocialFlexibleGroupBookInstallHelper::class);
$install_helper->revokeGroupPermissions();
// Set global permissions.
$permissions['verified'] = [
'add content to books',
];

$roles = (array) \Drupal::entityTypeManager()->getStorage('user_role')
->getQuery()
->accessCheck(FALSE)
->condition('id', 'administrator', '<>')
->execute();

/** @var \Drupal\user\Entity\Role $role */
foreach ($roles as $role) {
if (!empty($permissions[$role->id()])) {
user_role_grant_permissions($role->id(), $permissions[$role->id()]);
}
}

// Set group permissions.
// Group anonymous.
$group_permissions['flexible_group-anonymous'] = [
'view group_node:book content',
'view group_node:book entity',
];

// Group outsider.
$group_permissions['flexible_group-outsider'] = $group_permissions['flexible_group-anonymous'];

// Group member.
$group_permissions['flexible_group-member'] = $group_permissions['flexible_group-outsider'];

// Group manager.
$group_permissions['flexible_group-group_manager'] = [
...$group_permissions['flexible_group-member'],
'create group_node:book content',
'create group_node:book entity',
'delete own group_node:book entity',
'delete own group_node:book content',
'update own group_node:book content',
'update own group_node:book entity',
];
// Group outside role: Content manager.
$group_permissions['flexible_group-83776d798'] = $group_permissions['flexible_group-group_manager'];

// Group admin.
$group_permissions['flexible_group-group_admin'] = [
...$group_permissions['flexible_group-group_manager'],
'delete any group_node:book content',
'delete any group_node:book entity',
'update any group_node:book content',
'update any group_node:book entity',
'view unpublished group_node:book entity',
];

// Group outside role: Site manager.
$group_permissions['flexible_group-ba5854c29'] = $group_permissions['flexible_group-group_admin'];

// Group outside role: Administrator.
$group_permissions['flexible_group-a416e6833'] = $group_permissions['flexible_group-group_admin'];

/** @var \Drupal\group\Entity\GroupTypeInterface $group_type */
$group_type = \Drupal::entityTypeManager()
->getStorage('group_type')
->load('flexible_group');

foreach ($group_type->getRoles() as $role) {
if (!empty($group_permissions[$role->id()])) {
$role->grantPermissions($group_permissions[$role->id()])
->save();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use Drupal\group\Entity\GroupInterface;
*/
function social_flexible_group_book_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context): void {
if (
$context['entity_type'] === 'node'
&& $context['bundle'] === 'book'
&& $form_display->getMode() === 'default'
$context['entity_type'] === 'node' &&
$context['bundle'] === 'book' &&
$form_display->getMode() === 'default'
) {
// Add "groups" field to the form.
/* @see \Drupal\social_group\SocialGroupSelectorWidgetConfigOverride::loadOverrides */
// @see \Drupal\social_group\SocialGroupSelectorWidgetConfigOverride::loadOverrides
$form_display->setComponent('groups', [
'type' => 'social_group_selector_widget',
'weight' => 0,
Expand All @@ -39,9 +39,9 @@ function social_flexible_group_book_entity_form_display_alter(EntityFormDisplayI
}

if (
$context['entity_type'] === 'group'
&& $context['bundle'] === 'flexible_group'
&& $form_display->getMode() === 'default'
$context['entity_type'] === 'group' &&
$context['bundle'] === 'flexible_group' &&
$form_display->getMode() === 'default'
) {
$form_display->setComponent('enable_books', [
'weight' => 6,
Expand Down Expand Up @@ -89,6 +89,19 @@ function social_flexible_group_book_form_node_form_alter(array &$form, FormState
$form['#fieldgroups']['group_visibility']->children[] = 'groups';
$form['#group_children']['groups'] = 'group_visibility';
}

// We need add this option here because by default "book" module require
// a specific global permission - "create new books".
// Group managers with only "verified" global role should be able to create
// new books as well even without this permission.
$nid = !$node->isNew() ? $node->id() : 'new';
$options = $form['book']['bid']['#options'];
if (!isset($options[$nid])) {
if ($nid == 'new' || ($nid != $node->book['original_bid'])) {
// The node can become a new book, if it is not one already.
$form['book']['bid']['#options'] = [$nid => t('- Create a new book -')] + $options;
}
}
}

/**
Expand Down Expand Up @@ -127,10 +140,9 @@ function social_flexible_group_book_block_access(Block $block, string $operation
}

if (
in_array($block->getPluginId(), ['group_hero_block', 'group_statistic_block'])
|| $block->id() === 'socialblue_pagetitleblock_content'
in_array($block->getPluginId(), ['group_hero_block', 'group_statistic_block']) ||
$block->id() === 'socialblue_pagetitleblock_content'
) {
// @todo This should be added to the block configuration from here.
$request_path = $block->getVisibility()['request_path'];
$request_path['pages'] .= "\r\n/group/*/books";
$block->setVisibilityConfig('request_path', $request_path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function isValidEntityCondition($entity): bool {
if (!empty($node) && $node instanceof NodeInterface) {
// The main book page always has the depth 1.
if (
$node->bundle() === 'book'
&& isset($node->book['depth'])
&& (int) $node->book['depth'] === 1
$node->bundle() === 'book' &&
isset($node->book['depth']) &&
(int) $node->book['depth'] === 1
) {
return TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public function blockAccess(AccountInterface $account): AccessResultInterface {
$group = _social_group_get_current_group();

if ($group instanceof GroupInterface) {
if (
$group->hasPermission('create group_node:book entity', $account)
&& $account->hasPermission('create book content')
) {
if ($group->hasPermission('create group_node:book entity', $account)) {
return AccessResult::allowed();
}
}
Expand Down

This file was deleted.

0 comments on commit cedf6e0

Please sign in to comment.