l10n_server-2.x-dev/l10n_groups/src/Plugin/Block/GroupInformationBlock.php
l10n_groups/src/Plugin/Block/GroupInformationBlock.php
<?php declare(strict_types=1); namespace Drupal\l10n_groups\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Database\Connection; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\group\Entity\GroupInterface; use Drupal\group\Plugin\Group\Relation\GroupRelationInterface; use Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a group information block. * * @Block( * id = "l10n_groups_group_information", * admin_label = @Translation("Group Information"), * category = @Translation("L10n"), * context_definitions = { * "group" = @ContextDefinition("entity:group", required = FALSE) * } * ) */ class GroupInformationBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The group relation type manager. * * @var \Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface */ protected GroupRelationTypeManagerInterface $pluginManager; /** * The database connection. * * @var \Drupal\Core\Database\Connection */ protected Connection $database; /** * The current user account. * * @var \Drupal\Core\Session\AccountInterface */ protected AccountInterface $currentUser; /** * Creates a GroupOperationsBlock instance. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface $plugin_manager * The group relation type manager. * @param \Drupal\Core\Database\Connection $database * The database connection. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, GroupRelationTypeManagerInterface $plugin_manager, Connection $database, AccountInterface $current_user, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->pluginManager = $plugin_manager; $this->database = $database; $this->currentUser = $current_user; } /** * {@inheritdoc} */ public static function create( ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('group_relation_type.manager'), $container->get('database'), $container->get('current_user'), ); } /** * {@inheritdoc} */ public function build() { $build = [ '#type' => 'container', ]; $cacheable_metadata = new CacheableMetadata(); $cacheable_metadata->setCacheContexts(['user.group_permissions']); /** @var \Drupal\group\Entity\Group $group */ if (($group = $this->getContextValue('group')) && $group->getGroupType()->id() == 'translation') { assert($group instanceof GroupInterface); $title = $this->t('Language Team'); /** @var \Drupal\Core\Language\LanguageInterface $language */ if ($language = $group->field_translation_language->entity) { $title = $this->t('@language team', [ '@language' => $language->getName(), ]); } $build['title'] = [ '#type' => 'html_tag', '#tag' => 'h2', '#value' => $title, ]; // Get the links for every operation allowed on the group. This is lifted // from \Drupal\group\Plugin\Block\GroupOperationsBlock. We couldn't use // it directly as it's using a #type operations for display. $links = []; // Retrieve the operations and cacheable metadata from the plugins. foreach ($group->getGroupType()->getInstalledPlugins() as $plugin) { assert($plugin instanceof GroupRelationInterface); $operation_provider = $this->pluginManager ->getOperationProvider($plugin->getRelationTypeId()); $operations = $operation_provider->getGroupOperations($group); $cacheable_metadata = $cacheable_metadata ->merge(CacheableMetadata::createFromRenderArray($operations)); unset($operations['#cache']); $links += $operations; } // Massage the render array coming from the group relationship plugins. foreach ($links as $operation => $link) { $renderable_links[$operation] = [ '#type' => 'link', '#title' => $link['title'], '#url' => $link['url'], ]; } if ($group->hasPermission('administer group members', $this->currentUser)) { // Get the members count directly from the database for performance. $members_query = $this->database ->query('SELECT count(DISTINCT entity_id) as count FROM {group_relationship_field_data} WHERE gid = :id AND type = :type', [ ':id' => $group->id(), ':type' => $group->getGroupType()->id() . '-group_membership', ]); $result = $members_query->fetchAll(); $members_count = $result[0]->count; $renderable_links['see-members'] = [ '#type' => 'link', '#title' => $this->formatPlural($members_count, '1 member', '@count members', ['@count' => $members_count]), '#url' => Url::fromRoute('view.group_members.page_1', ['group' => $group->id()]), ]; } if ($group->hasPermission('view group_membership relationship', $this->currentUser)) { $renderable_links['see-owner'] = [ '#type' => 'inline_template', '#template' => "{% trans %}Manager{% endtrans %}: {{name}}", '#context' => [ 'name' => $group->getOwner()->toLink()->toRenderable(), ], ]; } $build['content'] = [ '#theme' => 'item_list', '#list_type' => 'ul', '#items' => $renderable_links, ]; } // Set the cacheable metadata on the build. $cacheable_metadata->applyTo($build); return $build; } /** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { return AccessResult::allowedIf( $account->hasPermission('access localization community') ); } }