GroupContentMenuListBuilder.php in Group Content Menu 8
File
src/GroupContentMenuListBuilder.php
View source
<?php
namespace Drupal\group_content_menu;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\group\Entity\GroupContentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GroupContentMenuListBuilder extends EntityListBuilder {
protected $dateFormatter;
protected $redirectDestination;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->redirectDestination = $redirect_destination;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('date.formatter'), $container
->get('redirect.destination'));
}
public function load() {
$group = \Drupal::routeMatch()
->getParameter('group');
if ($group) {
return array_map(static function (GroupContentInterface $group_content) {
return $group_content
->getEntity();
}, group_content_menu_get_menus_per_group($group));
}
return [];
}
public function buildHeader() {
$header['id'] = $this
->t('ID');
$header['bundle'] = $this
->t('Type');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['id'] = $entity
->toLink($entity
->label(), 'edit-form')
->toString();
$row['bundle'] = \Drupal::entityTypeManager()
->getStorage('group_content_menu_type')
->load($entity
->bundle())
->label();
return $row + parent::buildRow($entity);
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$group_contents = \Drupal::entityTypeManager()
->getStorage('group_content')
->loadByEntity($entity);
if ($group_content = reset($group_contents)) {
$entity_type_id = $entity
->getEntityTypeId();
$account = \Drupal::currentUser()
->getAccount();
if ($entity
->hasLinkTemplate('edit-form') && $group_content
->getGroup()
->hasPermission("update own group_content_menu:{$entity_type_id} entity", $account)) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'weight' => 10,
'url' => $this
->ensureDestination($entity
->toUrl('edit-form')),
];
}
if ($entity
->hasLinkTemplate('delete-form') && $group_content
->getGroup()
->hasPermission("delete own group_content_menu:{$entity_type_id} entity", $account)) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'weight' => 100,
'url' => $this
->ensureDestination($entity
->toUrl('delete-form')),
];
}
}
return $operations;
}
}