cloud-8.x-2.0-beta1/src/Controller/CloudConfigLocationController.php
src/Controller/CloudConfigLocationController.php
<?php
namespace Drupal\cloud\Controller;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Controller responsible for Cloud Config Location.
*/
class CloudConfigLocationController extends ControllerBase {
/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The cloud service provider plugin manager (CloudConfigPluginManager).
*
* @var \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* CloudConfigLocationController constructor.
*
* @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
* Country Manager.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* Route Provider.
* @param \Symfony\Component\HttpFoundation\Request $request
* Request.
* @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud service provider plugin manager (CloudConfigPluginManager).
*/
public function __construct(CountryManagerInterface $country_manager, RouteProviderInterface $route_provider, Request $request, CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {
$this->countryManager = $country_manager;
$this->routeProvider = $route_provider;
$this->request = $request;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
}
/**
* Dependency Injection.
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('country_manager'),
$container->get('router.route_provider'),
$container->get('request_stack')->getCurrentRequest(),
$container->get('plugin.manager.cloud_config_plugin')
);
}
/**
* Checks user access for cloud config location.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Run access checks for this account.
* @param \Symfony\Component\Routing\Route $route
* The route object.
* @param int $cloud_config
* Cloud Config entity id.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function access(AccountInterface $account, Route $route, $cloud_config = NULL) {
if (!isset($cloud_config)) {
$view = Views::getView('cloud_config');
if ($view->access('admin', $account) || $view->access('list', $account)) {
return AccessResult::allowed();
}
}
else {
$cloud_config_entity = $this->entityTypeManager()->getStorage('cloud_config')->load($cloud_config);
if ($cloud_config_entity) {
if ($cloud_config_entity->access('view')) {
return AccessResult::allowed();
}
}
}
return AccessResult::forbidden();
}
/**
* Get Cloud Config location.
*
* @param int $cloud_config
* Cloud Config entity id.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* A JSON response of a Cloud Config location.
*/
public function getCloudConfigLocation($cloud_config = NULL) {
$response = [];
$country_allowed_values = $this->countryManager::getStandardList();
$cloud_configs = isset($cloud_config)
? $this->entityTypeManager()->getStorage('cloud_config')
->loadByProperties([
'id' => $cloud_config,
])
: $this->entityTypeManager()->getStorage('cloud_config')
->loadMultiple();
foreach ($cloud_configs ?: [] as $cloud_config) {
if (isset($cloud_config)
&& isset($cloud_config->field_location_country) && !empty($cloud_config->field_location_country->value)
&& isset($cloud_config->field_location_city) && !empty($cloud_config->field_location_city->value)
&& isset($cloud_config->field_location_longitude) && !empty($cloud_config->field_location_longitude->value)
&& isset($cloud_config->field_location_latitude) && !empty($cloud_config->field_location_latitude->value)) {
$this->cloudConfigPluginManager->setCloudContext($cloud_config->getCloudContext());
$route = $this->cloudConfigPluginManager->getInstanceCollectionTemplateName();
$response[] = [
'Name' => $cloud_config->getName(),
'Type' => $cloud_config->bundle(),
'Country' => $country_allowed_values[$cloud_config->field_location_country->value],
'City' => $cloud_config->field_location_city->value,
'Longitude' => $cloud_config->field_location_longitude->value,
'Latitude' => $cloud_config->field_location_latitude->value,
'Url' => Url::fromRoute($route, ['cloud_context' => $cloud_config->getCloudContext()])->toString(),
];
}
}
return new JsonResponse($response);
}
}
