View source
<?php
namespace Drupal\file_url\Plugin\Field\FieldType;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file_url\FileUrlHandler;
class FileUrlFieldItemList extends EntityReferenceFieldItemList {
public function getConstraints() {
$constraints = [];
$cardinality = $this
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$constraints[] = $this
->getTypedDataManager()
->getValidationConstraintManager()
->create('Count', [
'max' => $cardinality,
'maxMessage' => t('%name: this field cannot hold more than @count values.', [
'%name' => $this
->getFieldDefinition()
->getLabel(),
'@count' => $cardinality,
]),
]);
}
return $constraints;
}
public function defaultValuesForm(array &$form, FormStateInterface $form_state) {
}
public function postSave($update) {
$file_handler = \Drupal::service('file_url.handler');
$entity = $this
->getEntity();
if (!$update) {
foreach ($this
->referencedEntities() as $file) {
if (!$file_handler
->isRemote($file)) {
\Drupal::service('file.usage')
->add($file, 'file', $entity
->getEntityTypeId(), $entity
->id());
}
}
}
else {
$files = $this
->referencedEntities();
$ids = [];
foreach ($files as $file) {
$ids[] = $file
->id();
}
if (!empty($entity->original) && $entity
->getRevisionId() != $entity->original
->getRevisionId()) {
foreach ($files as $file) {
if (!$file_handler
->isRemote($file)) {
\Drupal::service('file.usage')
->add($file, 'file', $entity
->getEntityTypeId(), $entity
->id());
}
}
return;
}
$field_name = $this
->getFieldDefinition()
->getName();
$original_ids = [];
$langcode = $this
->getLangcode();
$original = $entity->original;
if ($original
->hasTranslation($langcode)) {
$original_items = $original
->getTranslation($langcode)->{$field_name};
foreach ($original_items as $item) {
$file = $file_handler::urlToFile($item->target_id);
if (!$file_handler
->isRemote($file)) {
$original_ids[] = $file
->id();
}
}
}
$removed_ids = array_filter(array_diff($original_ids, $ids));
$removed_files = \Drupal::entityManager()
->getStorage('file')
->loadMultiple($removed_ids);
foreach ($removed_files as $file) {
\Drupal::service('file.usage')
->delete($file, 'file', $entity
->getEntityTypeId(), $entity
->id());
}
foreach ($files as $file) {
if (!in_array($file
->id(), $original_ids) && !$file_handler
->isRemote($file)) {
\Drupal::service('file.usage')
->add($file, 'file', $entity
->getEntityTypeId(), $entity
->id());
}
}
}
}
public function referencedEntities() {
$file_handler = \Drupal::service('file_url.handler');
if (empty($this->list)) {
return [];
}
$target_entities = [];
foreach ($this->list as $delta => $item) {
if ($item->target_id !== NULL) {
$file = $file_handler::urlToFile($item->target_id);
$target_entities[$delta] = $file;
}
elseif ($item
->hasNewEntity()) {
$target_entities[$delta] = $item->entity;
}
}
ksort($target_entities);
return $target_entities;
}
public function delete() {
parent::delete();
$entity = $this
->getEntity();
$count = $entity
->isDefaultTranslation() ? 0 : 1;
foreach ($this
->referencedEntities() as $file) {
if (!FileUrlHandler::isRemote($file)) {
\Drupal::service('file.usage')
->delete($file, 'file', $entity
->getEntityTypeId(), $entity
->id(), $count);
}
}
}
public function deleteRevision() {
parent::deleteRevision();
$entity = $this
->getEntity();
foreach ($this
->referencedEntities() as $file) {
if (!FileUrlHandler::isRemote($file)) {
\Drupal::service('file.usage')
->delete($file, 'file', $entity
->getEntityTypeId(), $entity
->id());
}
}
}
}