You are here

protected function MenuItemsResource::applyFiltersToParams in JSON:API Menu Items 1.2.x

Apply filters to the menu parameters.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

\Drupal\Core\Menu\MenuTreeParameters $parameters: The cache metadata.

Return value

\Drupal\Core\Menu\MenuTreeParameters The Menu Tree Parameters object.

1 call to MenuItemsResource::applyFiltersToParams()
MenuItemsResource::process in src/Resource/MenuItemsResource.php
Process the resource request.

File

src/Resource/MenuItemsResource.php, line 109

Class

MenuItemsResource
Processes a request for a collection of featured nodes.

Namespace

Drupal\jsonapi_menu_items\Resource

Code

protected function applyFiltersToParams(Request $request, MenuTreeParameters $parameters) {
  $filter = $request->query
    ->get('filter');
  if (!empty($filter['min_depth'])) {
    $parameters
      ->setMinDepth((int) $filter['min_depth']);
  }
  if (!empty($filter['max_depth'])) {
    $parameters
      ->setMaxDepth((int) $filter['max_depth']);
  }
  if (!empty($filter['parent'])) {
    $parameters
      ->setRoot($filter['parent']);
    $parameters
      ->excludeRoot();
  }
  if (!empty($filter['parents'])) {
    $parents = explode(',', preg_replace("/\\s+/", "", $filter['parents']));
    $parameters
      ->addExpandedParents($parents);
  }
  if (!empty($filter['conditions']) && is_array($filter['conditions'])) {
    $condition_fields = array_keys($filter['conditions']);
    foreach ($condition_fields as $definition_field) {
      $value = !empty($filter['conditions'][$definition_field]['value']) ? $filter['conditions'][$definition_field]['value'] : '';
      $operator = !empty($filter['conditions'][$definition_field]['operator']) ? $filter['conditions'][$definition_field]['operator'] : '=';
      $parameters
        ->addCondition($definition_field, $value, $operator);
    }
  }
  return $parameters;
}