You are here

public function ChannelManipulator::getQuery in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 modules/entity_share_server/src/Service/ChannelManipulator.php \Drupal\entity_share_server\Service\ChannelManipulator::getQuery()

Generate URL query.

Parameters

\Drupal\entity_share_server\Entity\ChannelInterface $channel: The channel entity.

Return value

array The query options to use to request JSON:API.

Overrides ChannelManipulatorInterface::getQuery

File

modules/entity_share_server/src/Service/ChannelManipulator.php, line 57

Class

ChannelManipulator
Class ChannelManipulator.

Namespace

Drupal\entity_share_server\Service

Code

public function getQuery(ChannelInterface $channel) {
  $query = [];

  // In case of translatable entities. Add a filter on the langcode to
  // only get entities in the channel language.
  if ($channel
    ->get('channel_langcode') != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    $entity_type = $channel
      ->get('channel_entity_type');
    $entity_keys = $this->entityTypeManager
      ->getStorage($entity_type)
      ->getEntityType()
      ->getKeys();
    $resource_type = $this->resourceTypeRepository
      ->get($entity_type, $channel
      ->get('channel_bundle'));
    $langcode_path = 'langcode';
    if (isset($entity_keys['langcode']) && !empty($entity_keys['langcode'])) {
      $langcode_path = $resource_type
        ->getPublicName($entity_keys['langcode']);
    }
    $query['filter']['langcode-filter'] = [
      'condition' => [
        'path' => $langcode_path,
        'operator' => '=',
        'value' => $channel
          ->get('channel_langcode'),
      ],
    ];
  }

  // Add groups.
  if (!is_null($channel
    ->get('channel_groups'))) {
    foreach ($channel
      ->get('channel_groups') as $group_id => $group) {
      $query['filter'][$group_id] = [
        'group' => [
          'conjunction' => $group['conjunction'],
        ],
      ];
      if (isset($group['memberof'])) {
        $query['filter'][$group_id]['group']['memberOf'] = $group['memberof'];
      }
    }
  }

  // Add filters.
  if (!is_null($channel
    ->get('channel_filters'))) {
    foreach ($channel
      ->get('channel_filters') as $filter_id => $filter) {
      $query['filter'][$filter_id] = [
        'condition' => [
          'path' => $filter['path'],
          'operator' => $filter['operator'],
        ],
      ];
      if (isset($filter['value'])) {

        // Multiple values operators.
        if (in_array($filter['operator'], OperatorsHelper::getMultipleValuesOperators())) {
          $query['filter'][$filter_id]['condition']['value'] = $filter['value'];
        }
        else {
          $query['filter'][$filter_id]['condition']['value'] = implode($filter['value']);
        }
      }
      if (isset($filter['memberof'])) {
        $query['filter'][$filter_id]['condition']['memberOf'] = $filter['memberof'];
      }
    }
  }

  // Add sorts.
  if (!is_null($channel
    ->get('channel_sorts'))) {
    $sorts = $channel
      ->get('channel_sorts');
    uasort($sorts, [
      SortArray::class,
      'sortByWeightElement',
    ]);
    foreach ($sorts as $sort_id => $sort) {
      $query['sort'][$sort_id] = [
        'path' => $sort['path'],
        'direction' => $sort['direction'],
      ];
    }
  }
  return $query;
}