View source
<?php
namespace Drupal\apigee_edge\Entity\ListBuilder;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Exception\DeveloperDoesNotExistException;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
class DeveloperAppListBuilderForDeveloper extends AppListBuilder implements ContainerInjectionInterface {
protected $currentUser;
protected $routeMatch;
private $appNameCssIdCache = [];
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, RequestStack $request_stack, TimeInterface $time, AccountInterface $current_user, RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory = NULL) {
if (!$config_factory) {
$config_factory = \Drupal::service('config.factory');
}
parent::__construct($entity_type, $entity_type_manager, $render, $request_stack, $time, $config_factory);
$this->currentUser = $current_user;
$this->routeMatch = $route_match;
}
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('current_user'), $container
->get('current_route_match'), $container
->get('config.factory'));
}
public static function create(ContainerInterface $container) {
return static::createInstance($container, $container
->get('entity_type.manager')
->getDefinition('developer_app'));
}
protected function buildEntityIdQuery() : QueryInterface {
$query = parent::buildEntityIdQuery();
$user = $this->routeMatch
->getParameter('user');
$developer_id = $user
->get('apigee_edge_developer_id')->value;
if ($developer_id === NULL) {
throw new DeveloperDoesNotExistException($user
->getEmail());
}
$query
->condition('developerId', $developer_id);
return $query;
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
foreach ($operations as $operation => $parameters) {
if ($entity
->hasLinkTemplate("{$operation}-for-developer")) {
$operations[$operation]['url'] = $entity
->toUrl("{$operation}-for-developer");
}
if ($entity
->hasLinkTemplate("{$operation}-form-for-developer")) {
$operations[$operation]['url'] = $this
->ensureDestination($entity
->toUrl("{$operation}-form-for-developer"));
}
}
return $operations;
}
protected function generateCssIdForApp(AppInterface $app) : string {
if (!array_key_exists($app
->getName(), $this->appNameCssIdCache)) {
$this->appNameCssIdCache[$app
->getName()] = Html::getUniqueId($app
->getName());
}
return $this->appNameCssIdCache[$app
->getName()];
}
protected function renderAppName(AppInterface $app) : array {
if ($app
->access('view')) {
return $app
->toLink(NULL, 'canonical-by-developer')
->toRenderable();
}
return parent::renderAppName($app);
}
public function appsPage() : RedirectResponse {
$options['absolute'] = TRUE;
$url = Url::fromRoute('entity.developer_app.collection_by_developer', [
'user' => $this->currentUser
->id(),
], $options);
return new RedirectResponse($url
->toString(), 302);
}
public function pageTitle() : TranslatableMarkup {
$account = $this->routeMatch
->getParameter('user');
if ($account && $account
->id() != $this->currentUser
->id()) {
$args['@user'] = Markup::create($account
->label());
$title = $this
->t('Apps of @user', $args);
}
else {
$title = apigee_edge_app_listing_page_title();
}
return $title;
}
protected function getCacheMaxAge() {
return $this->configFactory
->get('apigee_edge.developer_app_settings')
->get('cache_expiration');
}
}