You are here

class RemoveNonPublicFiles in Acquia Content Hub 8.2

Class RemoveNonPublicFiles.

Removes dependency on non-public files.

@package Drupal\acquia_contenthub\EventSubscriber\FilterDeps

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\FilterDeps\RemoveNonPublicFiles implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RemoveNonPublicFiles

1 string reference to 'RemoveNonPublicFiles'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses RemoveNonPublicFiles
file_entity.remove.non_public.filter in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\FilterDeps\RemoveNonPublicFiles

File

src/EventSubscriber/FilterDeps/RemoveNonPublicFiles.php, line 20

Namespace

Drupal\acquia_contenthub\EventSubscriber\FilterDeps
View source
class RemoveNonPublicFiles implements EventSubscriberInterface {

  /**
   * The file scheme handler manager.
   *
   * @var \Drupal\acquia_contenthub\Plugin\FileSchemeHandler\FileSchemeHandlerManagerInterface
   */
  protected $manager;

  /**
   * RemoveNonPublicFiles constructor.
   *
   * @param \Drupal\acquia_contenthub\Plugin\FileSchemeHandler\FileSchemeHandlerManagerInterface $manager
   *   The file scheme handler manager service.
   */
  public function __construct(FileSchemeHandlerManagerInterface $manager) {
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[DependencyCalculatorEvents::FILTER_FIELDS][] = [
      'onFilterFields',
      1001,
    ];
    $events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
      'onSerializeContentField',
      1001,
    ];
    return $events;
  }

  /**
   * Filter fields.
   *
   * @param \Drupal\depcalc\Event\FilterDependencyCalculationFieldsEvent $event
   *   Filter Dependency Calculation Fields.
   */
  public function onFilterFields(FilterDependencyCalculationFieldsEvent $event) {
    $fields = array_filter($event
      ->getFields(), function ($field) {
      return $this
        ->includeField($field);
    }, ARRAY_FILTER_USE_BOTH);
    $event
      ->setFields(...$fields);
  }

  /**
   * Serialize content field event.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   Serialized CDF Entity Field event.
   */
  public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
    $field = $event
      ->getField();
    if (!$this
      ->includeField($field)) {
      $event
        ->setExcluded();
      $event
        ->stopPropagation();
    }
  }

  /**
   * Whether we should include this field in the dependency calculation.
   *
   * @param \Drupal\Core\Field\FieldItemListInterface $field
   *   The entity field.
   *
   * @return bool
   *   TRUE if we should include the field, FALSE otherwise.
   */
  protected function includeField(FieldItemListInterface $field) {
    $definition = $field
      ->getFieldDefinition();
    if (!in_array($definition
      ->getType(), [
      'file',
      'image',
    ], TRUE)) {
      return TRUE;
    }
    return $this->manager
      ->hasDefinition($definition
      ->getFieldStorageDefinition()
      ->getSetting('uri_scheme'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RemoveNonPublicFiles::$manager protected property The file scheme handler manager.
RemoveNonPublicFiles::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RemoveNonPublicFiles::includeField protected function Whether we should include this field in the dependency calculation.
RemoveNonPublicFiles::onFilterFields public function Filter fields.
RemoveNonPublicFiles::onSerializeContentField public function Serialize content field event.
RemoveNonPublicFiles::__construct public function RemoveNonPublicFiles constructor.