salesforce-8.x-4.x-dev/modules/salesforce_mapping_ui/src/Controller/MappedObjectController.php
modules/salesforce_mapping_ui/src/Controller/MappedObjectController.php
<?php
namespace Drupal\salesforce_mapping_ui\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns responses for devel module routes.
*/
class MappedObjectController extends ControllerBase {
/**
* Current route.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $route;
/**
* {@inheritdoc}
*/
public function __construct(CurrentRouteMatch $route) {
$this->route = $route;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('current_route_match'));
}
/**
* Access callback for Mapped Object entity.
*/
public function access(AccountInterface $account) {
if (!$account->hasPermission('administer salesforce')) {
return AccessResult::forbidden();
}
$param = null;
$parameter_info = $this->route->getRouteObject()->getOption('parameters');
foreach ($parameter_info as $name => $options) {
if (isset($options['type']) && str_starts_with($options['type'], 'entity:')) {
$param = $this->route->getParameter($name);
continue 1;
}
}
if (!is_object($param)) {
return AccessResult::forbidden();
}
$implements = class_implements($param);
if (empty($implements['Drupal\Core\Entity\EntityInterface'])) {
return AccessResult::forbidden();
}
// Only allow access for entities with mappings.
return $this->entityTypeManager()
->getStorage('salesforce_mapping')
->loadByEntity($param)
? AccessResult::allowed()
: AccessResult::forbidden();
}
/**
* Helper function to get entity from router path.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match service.
*
* @return \Drupal\Core\Entity\EntityInterface
* The Drupal entity mapped by the given mapped object.
*
* @throws \Exception
* If an EntityInterface is not found at the given route.
*
* @todo find a more specific exception class
*/
private function getEntity(RouteMatchInterface $route_match) {
$parameter_name = $route_match->getRouteObject()
->getOption('_salesforce_entity_type_id');
if (empty($parameter_name)) {
throw new \Exception('Entity type parameter not found.');
}
$entity = $route_match->getParameter($parameter_name);
if (!$entity || !($entity instanceof EntityInterface)) {
throw new \Exception('Entity is not of type EntityInterface');
}
return $entity;
}
/**
* Helper function to fetch existing MappedObject or create a new one.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be mapped.
*
* @return \Drupal\salesforce_mapping\Entity\MappedObject[]
* The Mapped Objects corresponding to the given entity.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function getMappedObjects(EntityInterface $entity) {
// @todo this probably belongs in a service
return $this
->entityTypeManager()
->getStorage('salesforce_mapped_object')
->loadByEntity($entity);
}
/**
* List mapped objects for the entity along the current route.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* A RouteMatch object.
*
* @return array
* Array of page elements to render.
*
* @throws \Exception
*/
public function listing(RouteMatchInterface $route_match) {
$entity = $this->getEntity($route_match);
$salesforce_mapped_objects = $this->getMappedObjects($entity);
if (empty($salesforce_mapped_objects)) {
return [
'#markup' => $this->t('No mapped objects for %label.', ['%label' => $entity->label()]),
];
}
// Show the entity view for the mapped object.
$builder = $this->entityTypeManager()
->getListBuilder('salesforce_mapped_object');
return $builder->setEntityIds(array_keys($salesforce_mapped_objects))
->render();
}
}
