GroupAddBlock.php in Open Social 8.7
Same filename and directory in other branches
- 8.9 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8.2 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8.3 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8.4 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8.5 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8.6 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 8.8 modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 10.3.x modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 10.0.x modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 10.1.x modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
- 10.2.x modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.php
Namespace
Drupal\social_group\Plugin\BlockFile
modules/social_features/social_group/src/Plugin/Block/GroupAddBlock.phpView source
<?php
namespace Drupal\social_group\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\group\Entity\GroupType;
/**
* Provides a 'GroupAddBlock' block.
*
* @Block(
* id = "group_add_block",
* admin_label = @Translation("Group add block"),
* )
*/
class GroupAddBlock extends BlockBase {
/**
* {@inheritdoc}
*
* Custom access logic to display the block.
*/
public function blockAccess(AccountInterface $account) {
$current_user = \Drupal::currentUser();
$route_user_id = \Drupal::routeMatch()
->getParameter('user');
// Show this block only on current user Groups page.
$can_create_groups = FALSE;
foreach (GroupType::loadMultiple() as $group_type) {
$permissions = 'create ' . $group_type
->id() . ' group';
if ($account
->hasPermission($permissions)) {
$can_create_groups = TRUE;
break;
}
}
if ($current_user
->id() == $route_user_id && $can_create_groups) {
return AccessResult::allowed();
}
// By default, the block is not visible.
return AccessResult::forbidden();
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
// TODO: Change url and add caching when closed groups will be added.
$url = Url::fromUserInput('/group/add');
$link_options = [
'attributes' => [
'class' => [
'btn',
'btn-primary',
'btn-raised',
'waves-effect',
'brand-bg-primary',
],
],
];
$url
->setOptions($link_options);
$build['content'] = Link::fromTextAndUrl(t('Add a group'), $url)
->toRenderable();
return $build;
}
}
Classes
Name | Description |
---|---|
GroupAddBlock | Provides a 'GroupAddBlock' block. |