You are here

FileUrlSelection.php in File URL 2.0.x

Same filename and directory in other branches
  1. 8 src/Plugin/EntityReferenceSelection/FileUrlSelection.php

File

src/Plugin/EntityReferenceSelection/FileUrlSelection.php
View source
<?php

namespace Drupal\file_url\Plugin\EntityReferenceSelection;

use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;

/**
 * Default plugin implementation of the Entity Reference Selection plugin.
 *
 * @EntityReferenceSelection(
 *   id = "file_url_default:file",
 *   label = @Translation("File URL selection"),
 *   entity_types = {"file"},
 *   group = "file_url_default",
 *   weight = 0,
 * )
 */
class FileUrlSelection extends DefaultSelection {

  /**
   * {@inheritdoc}
   */
  public function validateReferenceableEntities(array $ids) {

    /** @var \Drupal\file_url\FileUrlHandler $file_handler */
    $file_handler = \Drupal::service('file_url.handler');
    $found = [];
    foreach ($ids as $id) {
      $file = $file_handler::urlToFile($id);

      // External file are always found.
      if ($file_handler
        ->isRemote($file)) {
        $uri = $file
          ->getFileUri();
      }
      else {
        $uri = $file_handler::fileToUrl($file);
      }
      if (in_array($uri, $ids)) {
        $found[] = $uri;
      }
    }
    return $found;
  }

}

Classes

Namesort descending Description
FileUrlSelection Default plugin implementation of the Entity Reference Selection plugin.