You are here

public function EntityIndex::processRequest in Services 8.4

Same name and namespace in other branches
  1. 9.0.x src/Plugin/ServiceDefinition/EntityIndex.php \Drupal\services\Plugin\ServiceDefinition\EntityIndex::processRequest()

Processes the request and returns an array of data as appropriate.

Parameters

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

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object.

\Symfony\Component\Serializer\SerializerInterface $serializer: The serializer. Some methods might require the plugin to leverage the serializer after extracting the request contents.

Return value

array The response.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

Overrides ServiceDefinitionInterface::processRequest

File

src/Plugin/ServiceDefinition/EntityIndex.php, line 58

Class

EntityIndex
Plugin annotation @ServiceDefinition( id = "entity_index", methods = { "GET" }, translatable = true, deriver = "\Drupal\services\Plugin\Deriver\EntityIndex" )

Namespace

Drupal\services\Plugin\ServiceDefinition

Code

public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
  $entity_type_id = $this
    ->getDerivativeId();
  $start = 0;
  $limit = 30;
  if ($request->query
    ->has('start') && is_numeric($request->query
    ->get('start'))) {
    $start = $request->query
      ->get('start');
  }
  if ($request->query
    ->has('limit') && is_numeric($request->query
    ->get('limit'))) {
    $limit = $request->query
      ->get('limit');
  }
  $result = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->getQuery()
    ->range($start, $limit)
    ->execute();
  $map_function = function ($id) use ($entity_type_id) {
    return $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($id)
      ->label();
  };
  return array_map($map_function, $result);
}