You are here

public function EntryPoint::index in Entity Share 8.3

Same name and namespace in other branches
  1. 8 modules/entity_share_server/src/Controller/EntryPoint.php \Drupal\entity_share_server\Controller\EntryPoint::index()
  2. 8.2 modules/entity_share_server/src/Controller/EntryPoint.php \Drupal\entity_share_server\Controller\EntryPoint::index()

Controller to list all the resources.

File

modules/entity_share_server/src/Controller/EntryPoint.php, line 54

Class

EntryPoint
Controller to generate list of channels URLs.

Namespace

Drupal\entity_share_server\Controller

Code

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()) {

    // Load the user to ensure with have a user entity.

    /** @var \Drupal\user\UserInterface $account */
    $account = $this
      ->entityTypeManager()
      ->getStorage('user')
      ->load($this
      ->currentUser()
      ->id());
    if (!is_null($account)) {
      $uuid = $account
        ->uuid();
    }
  }

  /** @var \Drupal\entity_share_server\Entity\ChannelInterface[] $channels */
  $channels = $this
    ->entityTypeManager()
    ->getStorage('channel')
    ->loadMultiple();
  $languages = $this
    ->languageManager()
    ->getLanguages(LanguageInterface::STATE_ALL);
  foreach ($channels as $channel) {

    // Check access for this user.
    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));

      // Prepare an URL to get only the UUIDs.
      $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),
      ];
    }
  }

  // Collect other channel definitions.
  $event = new ChannelListEvent($data);
  $this->eventDispatcher
    ->dispatch(ChannelListEvent::EVENT_NAME, $event);
  return new JsonResponse([
    'data' => $event
      ->getChannelList(),
    'links' => $urls,
  ]);
}