EntryPoint.php in Entity Share 8.3
File
modules/entity_share_server/src/Controller/EntryPoint.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_server\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\entity_share_server\Event\ChannelListEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class EntryPoint extends ControllerBase {
protected $channelManipulator;
protected $resourceTypeRepository;
protected $eventDispatcher;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->channelManipulator = $container
->get('entity_share_server.channel_manipulator');
$instance->resourceTypeRepository = $container
->get('jsonapi.resource_type.repository');
$instance->eventDispatcher = $container
->get('event_dispatcher');
return $instance;
}
public function index() {
$self = Url::fromRoute('entity_share_server.resource_list')
->setOption('absolute', TRUE)
->toString();
$urls = [
'self' => $self,
];
$data = [
'channels' => [],
'field_mappings' => $this
->getFieldMappings(),
];
$uuid = 'anonymous';
if ($this
->currentUser()
->isAuthenticated()) {
$account = $this
->entityTypeManager()
->getStorage('user')
->load($this
->currentUser()
->id());
if (!is_null($account)) {
$uuid = $account
->uuid();
}
}
$channels = $this
->entityTypeManager()
->getStorage('channel')
->loadMultiple();
$languages = $this
->languageManager()
->getLanguages(LanguageInterface::STATE_ALL);
foreach ($channels as $channel) {
if (in_array($uuid, $channel
->get('authorized_users'))) {
$channel_entity_type = $channel
->get('channel_entity_type');
$channel_bundle = $channel
->get('channel_bundle');
$channel_langcode = $channel
->get('channel_langcode');
$route_name = sprintf('jsonapi.%s--%s.collection', $channel_entity_type, $channel_bundle);
$url = Url::fromRoute($route_name)
->setOption('language', $languages[$channel_langcode])
->setOption('absolute', TRUE)
->setOption('query', $this->channelManipulator
->getQuery($channel));
$url_uuid = clone $url;
$query = $url_uuid
->getOption('query');
$query = !is_null($query) ? $query : [];
$url_uuid
->setOption('query', $query + [
'fields' => [
$channel_entity_type . '--' . $channel_bundle => 'changed',
],
]);
$data['channels'][$channel
->id()] = [
'label' => $channel
->label(),
'url' => $url
->toString(),
'url_uuid' => $url_uuid
->toString(),
'channel_entity_type' => $channel_entity_type,
'channel_bundle' => $channel_bundle,
'search_configuration' => $this->channelManipulator
->getSearchConfiguration($channel),
];
}
}
$event = new ChannelListEvent($data);
$this->eventDispatcher
->dispatch(ChannelListEvent::EVENT_NAME, $event);
return new JsonResponse([
'data' => $event
->getChannelList(),
'links' => $urls,
]);
}
protected function getFieldMappings() {
$mapping = [];
$definitions = $this
->entityTypeManager()
->getDefinitions();
$resource_types = $this->resourceTypeRepository
->all();
foreach ($resource_types as $resource_type) {
$entity_type_id = $resource_type
->getEntityTypeId();
if ($entity_type_id == 'user' || $definitions[$entity_type_id]
->getGroup() != 'content') {
continue;
}
$bundle = $resource_type
->getBundle();
$resource_type_fields = $resource_type
->getFields();
foreach ($resource_type_fields as $resource_type_field) {
$mapping[$entity_type_id][$bundle][$resource_type_field
->getInternalName()] = $resource_type_field
->getPublicName();
}
}
return $mapping;
}
}
Classes
Name |
Description |
EntryPoint |
Controller to generate list of channels URLs. |