SocialGroupContentListBuilder.php in Open Social 8.3
File
modules/social_features/social_group/src/Controller/SocialGroupContentListBuilder.php
View source
<?php
namespace Drupal\social_group\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialGroupContentListBuilder extends EntityListBuilder {
protected $group;
protected $entityTypeManager;
protected $redirectDestination;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RedirectDestinationInterface $redirect_destination, RouteMatchInterface $route_match, EntityTypeInterface $entity_type) {
parent::__construct($entity_type, $entity_type_manager
->getStorage($entity_type
->id()));
$this->entityTypeManager = $entity_type_manager;
$this->redirectDestination = $redirect_destination;
$this->group = $route_match
->getParameters()
->get('group');
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_type.manager'), $container
->get('redirect.destination'), $container
->get('current_route_match'), $entity_type);
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery();
$query
->sort($this->entityType
->getKey('id'));
$query
->condition('gid', $this->group
->id());
$query
->condition('type', 'group_membership', 'CONTAINS');
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
public function buildHeader() {
$header = [
'member' => $this
->t('Member'),
'organization' => $this
->t('Organization'),
'group_role' => $this
->t('Role'),
];
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
if ($entity
->getContentPlugin()
->getPluginId() == 'group_membership') {
$roles = [];
foreach ($entity->group_roles
->referencedEntities() as $group_role) {
$roles[] = $group_role
->label();
}
if (empty($roles)) {
$roles[] = $this
->t('Member');
}
$roles = implode(', ', $roles);
$profile = _social_group_get_member_profile($entity);
if (!empty($profile)) {
$row['member']['data'] = \Drupal::entityTypeManager()
->getViewBuilder('profile')
->view($profile, 'table');
$row['organization']['data'] = $profile
->get('field_profile_organization')
->view([
'label' => 'hidden',
]);
$row['group_role'] = $roles;
}
}
else {
$row['member'] = $entity
->id();
$row['organization']['data'] = $entity
->toLink()
->toRenderable();
}
if (isset($row)) {
return $row + parent::buildRow($entity);
}
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('There are no members yet.');
return $build;
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if (isset($operations['edit'])) {
$operations['edit']['title'] = $this
->t('Edit');
}
if (isset($operations['delete'])) {
$operations['delete']['title'] = $this
->t('Remove');
}
$destination = $this->redirectDestination
->getAsArray();
foreach ($operations as $key => $operation) {
$operations[$key]['query'] = $destination;
}
if ($entity
->getEntity()
->access('view') && $entity
->getEntity()
->hasLinkTemplate('canonical')) {
$operations['view'] = [
'title' => $this
->t('View'),
'weight' => 101,
'url' => $entity
->getEntity()
->toUrl('canonical'),
];
}
return $operations;
}
}