TeamListBuilder.php in Apigee Edge 8
File
modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php
View source
<?php
namespace Drupal\apigee_edge_teams\Entity\ListBuilder;
use Drupal\apigee_edge\Element\StatusPropertyElement;
use Drupal\apigee_edge\Entity\ListBuilder\EdgeEntityListBuilder;
use Drupal\apigee_edge_teams\Entity\TeamInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
class TeamListBuilder extends EdgeEntityListBuilder {
public function buildHeader() {
$headers = [];
$headers['name'] = [
'data' => $this
->t('@entity_type name', [
'@entity_type' => ucfirst($this->entityType
->getSingularLabel()),
]),
'specifier' => 'displayName',
'field' => 'displayName',
'sort' => 'desc',
];
$headers['status'] = [
'data' => $this
->t('Status'),
'specifier' => 'status',
'field' => 'status',
];
return $headers + parent::buildHeader();
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$team_app_list_url = Url::fromRoute('entity.team_app.collection_by_team', [
'team' => $entity
->id(),
]);
if ($team_app_list_url
->access()) {
$team_app_entity_def = $this->entityTypeManager
->getDefinition('team_app');
$operations['apps'] = [
'title' => $team_app_entity_def
->getCollectionLabel(),
'url' => $team_app_list_url,
'weight' => -10,
];
}
if ($entity
->hasLinkTemplate('members')) {
$members_url = $entity
->toUrl('members');
if ($members_url
->access()) {
$operations['members'] = [
'title' => $this
->t('Members'),
'url' => $members_url,
];
}
}
return $operations;
}
public function load() {
return array_filter(parent::load(), function (TeamInterface $entity) {
return $entity
->access('view');
});
}
public function buildRow(EntityInterface $entity) {
$row['name']['data'] = $entity
->toLink()
->toRenderable();
$row['status']['data'] = [
'#type' => 'status_property',
'#value' => $entity
->getStatus(),
'#indicator_status' => $entity
->getStatus() === TeamInterface::STATUS_ACTIVE ? StatusPropertyElement::INDICATOR_STATUS_OK : StatusPropertyElement::INDICATOR_STATUS_ERROR,
];
return $row + parent::buildRow($entity);
}
public function render() {
$build = parent::render();
$account = $this->entityTypeManager
->getStorage('user')
->load(\Drupal::currentUser()
->id());
$build = empty($build['table']) ? $build : $build['table'];
$build['#cache']['keys'][] = 'team_list_per_user';
$build['#cache']['contexts'][] = 'user';
$build['#cache']['contexts'][] = 'user.permissions';
$build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], $account
->getCacheTags());
$build['#cache']['max-age'] = $this->configFactory
->get('apigee_edge_teams.team_settings')
->get('cache_expiration');
return $build;
}
}