You are here

protected function DataProviderPlug::applyFilters in RESTful 7.2

Removes plugins from the list based on the request options.

Parameters

\Drupal\restful\Plugin\resource\ResourceInterface[] $plugins: The array of resource plugins keyed by instance ID.

Return value

\Drupal\restful\Plugin\resource\ResourceInterface[] The same array minus the filtered plugins.

1 call to DataProviderPlug::applyFilters()
DataProviderPlug::getIndexIds in src/Plugin/resource/DataProvider/DataProviderPlug.php
Returns the ID to render for the current index GET request.

File

src/Plugin/resource/DataProvider/DataProviderPlug.php, line 150
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderPlug.

Class

DataProviderPlug
Class DataProviderPlug.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

protected function applyFilters(array $plugins) {
  $resource_manager = restful()
    ->getResourceManager();
  $filters = $this
    ->parseRequestForListFilter();

  // If the 'all' option is not present, then add a filters to retrieve only
  // the last resource.
  $input = $this
    ->getRequest()
    ->getParsedInput();
  $all = !empty($input['all']);

  // Apply the filter to the list of plugins.
  foreach ($plugins as $instance_id => $plugin) {
    if (!$all) {

      // Remove the plugin if it's not the latest version.
      $version = $plugin
        ->getVersion();
      list($last_major, $last_minor) = $resource_manager
        ->getResourceLastVersion($plugin
        ->getResourceMachineName());
      if ($version['major'] != $last_major || $version['minor'] != $last_minor) {

        // We don't add the major and minor versions to filters because we
        // cannot depend on the presence of the versions as public fields.
        unset($plugins[$instance_id]);
        continue;
      }
    }

    // If the discovery is turned off for the resource, unset it.
    $definition = $plugin
      ->getPluginDefinition();
    if (!$definition['discoverable']) {
      unset($plugins[$instance_id]);
      continue;
    }

    // A filter on a result needs the ResourceFieldCollection representing the
    // result to return.
    $interpreter = new DataInterpreterPlug($this
      ->getAccount(), new PluginWrapper($plugin));
    $this->fieldDefinitions
      ->setInterpreter($interpreter);
    foreach ($filters as $filter) {
      if (!$this->fieldDefinitions
        ->evalFilter($filter)) {
        unset($plugins[$instance_id]);
      }
    }
  }
  $this->fieldDefinitions
    ->setInterpreter(NULL);
  return $plugins;
}