You are here

public function Channel::getQuery in Entity Share 8

Generate URL query.

Return value

array The query options to use to request JSON API.

Overrides ChannelInterface::getQuery

File

modules/entity_share_server/src/Entity/Channel.php, line 143

Class

Channel
Defines the Channel entity.

Namespace

Drupal\entity_share_server\Entity

Code

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

  // In case of translatable entities. Add a filter on the langcode to
  // only get entities in the channel language.
  if ($this->channel_langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    $query['filter']['langcode-filter'] = [
      'condition' => [
        'path' => 'langcode',
        'operator' => '=',
        'value' => $this->channel_langcode,
      ],
    ];
  }

  // Add groups.
  if (!is_null($this->channel_groups)) {
    foreach ($this->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($this->channel_filters)) {
    foreach ($this->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($this->channel_sorts)) {
    $sorts = $this->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;
}