You are here

public function MediaService::search in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/Service/MediaService.php \Drupal\gutenberg\Service\MediaService::search()

Search for file entities.

Parameters

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

string $type: Mime type of searched files.

string $search: Specific filename to search for.

Return value

array The found file entities.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Service/MediaService.php, line 341

Class

MediaService
The media service class.

Namespace

Drupal\gutenberg\Service

Code

public function search(Request $request, string $type = '', string $search = '') {
  $query = $this->entityTypeManager
    ->getStorage('file')
    ->getQuery();
  $query
    ->addTag('gutenberg_media_search');
  if ($search !== '*') {
    $query
      ->condition('filename', $search, 'CONTAINS');
  }
  if ($type !== '*') {
    $group = $query
      ->orConditionGroup();
    foreach (explode(' ', $type) as $key => $type_item) {
      $group
        ->condition('filemime', $type_item, 'CONTAINS');
    }
    $query
      ->condition($group);
  }
  $query
    ->sort('created', 'DESC');
  $this->moduleHandler
    ->invokeAll('gutenberg_media_search_query_alter', [
    $request,
    $type,
    $search,
    $query,
  ]);
  $files = $this->entityTypeManager
    ->getStorage('file')
    ->loadMultiple($query
    ->execute());
  $result = [];
  foreach ($files as $key => $file) {
    $result[] = $this->entityDataProviderManager
      ->getData('file', $file);
  }
  return $result;
}