RemoveNonPublicFiles.php in Acquia Content Hub 8.2
File
src/EventSubscriber/FilterDeps/RemoveNonPublicFiles.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\FilterDeps;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent;
use Drupal\acquia_contenthub\Plugin\FileSchemeHandler\FileSchemeHandlerManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\Event\FilterDependencyCalculationFieldsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RemoveNonPublicFiles implements EventSubscriberInterface {
protected $manager;
public function __construct(FileSchemeHandlerManagerInterface $manager) {
$this->manager = $manager;
}
public static function getSubscribedEvents() {
$events[DependencyCalculatorEvents::FILTER_FIELDS][] = [
'onFilterFields',
1001,
];
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
1001,
];
return $events;
}
public function onFilterFields(FilterDependencyCalculationFieldsEvent $event) {
$fields = array_filter($event
->getFields(), function ($field) {
return $this
->includeField($field);
}, ARRAY_FILTER_USE_BOTH);
$event
->setFields(...$fields);
}
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
$field = $event
->getField();
if (!$this
->includeField($field)) {
$event
->setExcluded();
$event
->stopPropagation();
}
}
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'));
}
}