View source
<?php
namespace Drupal\apigee_edge\Entity\ListBuilder;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\AppWarningsCheckerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class AppListBuilder extends EdgeEntityListBuilder {
protected $renderer;
protected $requestStack;
protected $time;
protected $configFactory;
protected $appWarningsChecker;
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, RequestStack $request_stack, TimeInterface $time, ConfigFactoryInterface $config_factory = NULL, AppWarningsCheckerInterface $app_warnings_checker = NULL) {
if (!$config_factory) {
$config_factory = \Drupal::service('config.factory');
}
if (!$app_warnings_checker) {
@trigger_error('Calling ' . __METHOD__ . ' without the $app_warnings_checker is deprecated in apigee_edge:8-x-1.18 and is required before apigee_edge:8.x-2.0. See https://github.com/apigee/apigee-edge-drupal/pull/507', E_USER_DEPRECATED);
$app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker');
}
parent::__construct($entity_type, $entity_type_manager, $config_factory);
$this->renderer = $renderer;
$this->entityTypeManager = $entity_type_manager;
$this->requestStack = $request_stack;
$this->time = $time;
$this->appWarningsChecker = $app_warnings_checker;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('request_stack'), $container
->get('datetime.time'), $container
->get('config.factory'));
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity
->access('analytics') && $entity
->hasLinkTemplate('analytics')) {
$operations['analytics'] = [
'title' => $this
->t('Analytics'),
'weight' => 150,
'url' => $entity
->toUrl('analytics'),
];
}
return $operations;
}
public function buildHeader() {
$headers = [];
$headers['name'] = [
'data' => $this
->t('@entity_type name', [
'@entity_type' => ucfirst($this->entityType
->getSingularLabel()),
]),
'specifier' => 'displayName',
'field' => 'displayName',
'sort' => 'asc',
];
$headers['status'] = [
'data' => $this
->t('Status'),
'specifier' => 'status',
'field' => 'status',
];
return $headers + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
return [];
}
protected function buildInfoRow(AppInterface $app, array &$rows) {
$css_id = $this
->getCssIdForInfoRow($app);
$row = [
'data' => [],
'id' => $css_id,
'class' => [
'row--app',
'row--info',
],
];
$row['data']['name']['data'] = $this
->renderAppName($app);
$row['data']['status']['data'] = $this
->renderAppStatus($app);
$row['data'] += parent::buildRow($app);
if (array_key_exists($css_id, $rows)) {
$rows[$css_id] = NestedArray::mergeDeep($rows[$css_id], $row);
}
else {
$rows[$css_id] = $row;
}
}
public function render() {
$build = parent::render();
$build['#cache']['contexts'][] = 'user';
$build['#cache']['contexts'][] = 'user.permissions';
if ($this
->usingDisplayType(static::VIEW_MODE_DISPLAY_TYPE)) {
return $build;
}
$build['table']['#attributes']['class'][] = 'table--app-list';
$build['table']['#rows'] = [];
$build['table']['#empty'] = $this
->t('Looks like you do not have any apps. Get started by adding one.');
foreach ($this
->load() as $entity) {
$rows = [];
$this
->buildInfoRow($entity, $rows);
$this
->buildWarningRow($entity, $rows);
$build['table']['#rows'] += $rows;
}
$build['#attached']['library'][] = 'apigee_edge/apigee_edge.app_listing';
return $build;
}
protected function buildWarningRow(AppInterface $app, array &$rows) {
$info_row_css_id = $this
->getCssIdForInfoRow($app);
$warning_row_css_id = $this
->getCssIdForWarningRow($app);
$row = [
'data' => [],
'id' => $warning_row_css_id,
'class' => [
'row--app',
'row--warning',
],
];
$warnings = $this
->checkAppCredentialWarnings($app);
if ($app
->getStatus() === AppInterface::STATUS_APPROVED && ($warnings['revokedCred'] || $warnings['revokedOrPendingCredProduct'] || $warnings['expiredCred'])) {
$build['status'] = $rows[$info_row_css_id]['data']['status']['data'];
$build['warning'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => '!',
'#attributes' => [
'class' => 'circle',
],
];
$link_options = [
'attributes' => [
'class' => [
'toggle--warning',
'closed',
],
'data-text-open' => [
$this
->t('Show details'),
],
'data-text-closed' => [
$this
->t('Hide details'),
],
],
'fragment' => $warning_row_css_id,
];
$url = Url::fromUserInput($this->requestStack
->getCurrentRequest()
->getRequestUri(), $link_options);
$link = Link::fromTextAndUrl($this
->t('<span class="ui-icon-triangle-1-e ui-icon"></span><span class="text">Show details</span>'), $url);
$build['warning-toggle'] = $link
->toRenderable();
$rows[$info_row_css_id]['data']['status']['data'] = $this->renderer
->renderPlain($build);
$row['data']['info'] = [
'colspan' => count($this
->buildHeader()),
];
$items = [];
if ($warnings['revokedCred']) {
$items[] = $warnings['revokedCred'];
}
elseif ($warnings['revokedOrPendingCredProduct']) {
$items[] = $warnings['revokedOrPendingCredProduct'];
}
if ($warnings['expiredCred']) {
$items[] = $warnings['expiredCred'];
}
$row['data']['info']['data'] = [
'#theme' => 'item_list',
'#items' => $items,
];
}
$rows[$warning_row_css_id] = $row;
}
protected function renderAppName(AppInterface $app) : array {
if ($app
->access('view')) {
return $app
->toLink()
->toRenderable();
}
return [
'#markup' => $app
->label(),
];
}
protected function renderAppStatus(AppInterface $app) : array {
$field = $this->entityTypeManager
->getViewBuilder($this->entityTypeId)
->viewField($app
->get('status'), 'default');
$field['#label_display'] = 'hidden';
return $field;
}
protected function checkAppCredentialWarnings(AppInterface $app) : array {
return $this->appWarningsChecker
->getWarnings($app);
}
protected function generateCssIdForApp(AppInterface $app) : string {
return Html::getId($app
->getAppId());
}
protected final function getCssIdForInfoRow(AppInterface $app) : string {
return $this
->generateCssIdForApp($app) . '-info';
}
protected final function getCssIdForWarningRow(AppInterface $app) : string {
return $this
->generateCssIdForApp($app) . '-warning';
}
}