class FileFieldItemList

Same name and namespace in other branches
  1. 11.x core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php \Drupal\file\Plugin\Field\FieldType\FileFieldItemList
  2. 10 core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php \Drupal\file\Plugin\Field\FieldType\FileFieldItemList
  3. 8.9.x core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php \Drupal\file\Plugin\Field\FieldType\FileFieldItemList

Represents a configurable entity file field.

Hierarchy

Expanded class hierarchy of FileFieldItemList

2 files declare their use of FileFieldItemList
FileUploadForm.php in core/modules/media_library/src/Form/FileUploadForm.php
TemporaryJsonapiFileFieldUploader.php in core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php

File

core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php, line 11

Namespace

Drupal\file\Plugin\Field\FieldType
View source
class FileFieldItemList extends EntityReferenceFieldItemList {
  
  /**
   * {@inheritdoc}
   */
  public function defaultValuesForm(array &$form, FormStateInterface $form_state) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function postSave($update) {
    $entity = $this->getEntity();
    if (!$update) {
      // Add a new usage for newly uploaded files.
      foreach ($this->referencedEntities() as $file) {
        \Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
      }
    }
    else {
      // Get current target file entities and file IDs.
      $files = $this->referencedEntities();
      $ids = [];
      /** @var \Drupal\file\FileInterface $file */
      foreach ($files as $file) {
        $ids[] = $file->id();
      }
      // On new revisions, all files are considered to be a new usage and no
      // deletion of previous file usages are necessary.
      if (!empty($entity->original) && $entity->getRevisionId() != $entity->original
        ->getRevisionId()) {
        foreach ($files as $file) {
          \Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
        }
        return;
      }
      // Get the file IDs attached to the field before this update.
      $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) {
          $original_ids[] = $item->target_id;
        }
      }
      // Decrement file usage by 1 for files that were removed from the field.
      $removed_ids = array_filter(array_diff($original_ids, $ids));
      $removed_files = \Drupal::entityTypeManager()->getStorage('file')
        ->loadMultiple($removed_ids);
      foreach ($removed_files as $file) {
        \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id());
      }
      // Add new usage entries for newly added files.
      foreach ($files as $file) {
        if (!in_array($file->id(), $original_ids)) {
          \Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
        }
      }
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function delete() {
    parent::delete();
    $entity = $this->getEntity();
    // If a translation is deleted only decrement the file usage by one. If the
    // default translation is deleted remove all file usages within this entity.
    $count = $entity->isDefaultTranslation() ? 0 : 1;
    foreach ($this->referencedEntities() as $file) {
      \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id(), $count);
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function deleteRevision() {
    parent::deleteRevision();
    $entity = $this->getEntity();
    // Decrement the file usage by 1.
    foreach ($this->referencedEntities() as $file) {
      \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id());
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function
DependencySerializationTrait::__wakeup public function #[\ReturnTypeWillChange]
EntityReferenceFieldItemList::defaultValuesFormSubmit public function
EntityReferenceFieldItemList::getConstraints public function
EntityReferenceFieldItemList::processDefaultValue public static function
EntityReferenceFieldItemList::referencedEntities public function
FieldItemList::$langcode protected property The langcode of the field values held in the object.
FieldItemList::$list protected property Numerically indexed array of field items. 1
FieldItemList::$list protected property Numerically indexed array of field items. 1
FieldItemList::access public function
FieldItemList::applyDefaultValue public function
FieldItemList::createItem protected function
FieldItemList::defaultAccess public function 1
FieldItemList::defaultAccess public function 1
FieldItemList::defaultValuesFormValidate public function
FieldItemList::defaultValueWidget protected function Returns the widget object used in default value form.
FieldItemList::delegateMethod protected function Calls a method on each FieldItem.
FieldItemList::equals public function 1
FieldItemList::equals public function 1
FieldItemList::filterEmptyItems public function
FieldItemList::generateSampleItems public function
FieldItemList::getEntity public function 1
FieldItemList::getEntity public function 1
FieldItemList::getFieldDefinition public function
FieldItemList::getLangcode public function
FieldItemList::getSetting public function
FieldItemList::getSettings public function
FieldItemList::hasAffectingChanges public function
FieldItemList::preSave public function 1
FieldItemList::preSave public function 1
FieldItemList::setLangcode public function
FieldItemList::setValue public function
FieldItemList::view public function
FieldItemList::__get public function
FieldItemList::__isset public function
FieldItemList::__set public function
FieldItemList::__unset public function
FileFieldItemList::defaultValuesForm public function Returns a form for the default value input. Overrides FieldItemList::defaultValuesForm
FileFieldItemList::delete public function Defines custom delete behavior for field values. Overrides FieldItemList::delete
FileFieldItemList::deleteRevision public function Defines custom revision delete behavior for field values. Overrides FieldItemList::deleteRevision
FileFieldItemList::postSave public function Defines custom post-save behavior for field values. Overrides FieldItemList::postSave
ItemList::appendItem public function
ItemList::count public function #[\ReturnTypeWillChange]
ItemList::filter public function
ItemList::first public function
ItemList::get public function
ItemList::getItemDefinition public function
ItemList::getIterator public function #[\ReturnTypeWillChange]
ItemList::getString public function
ItemList::getValue public function
ItemList::isEmpty public function
ItemList::offsetExists public function #[\ReturnTypeWillChange]
ItemList::offsetGet public function #[\ReturnTypeWillChange]
ItemList::offsetSet public function #[\ReturnTypeWillChange]
ItemList::offsetUnset public function #[\ReturnTypeWillChange]
ItemList::onChange public function
ItemList::rekey protected function Renumbers the items in the list.
ItemList::removeItem public function
ItemList::set public function
ItemList::__clone public function Magic method: Implements a deep clone.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::createInstance public static function
TypedData::getDataDefinition public function
TypedData::getName public function
TypedData::getParent public function
TypedData::getPluginDefinition public function
TypedData::getPluginId public function
TypedData::getPropertyPath public function
TypedData::getRoot public function
TypedData::setContext public function
TypedData::validate public function
TypedData::__construct public function Constructs a TypedData object given its definition and context.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager.
TypedDataTrait::setTypedDataManager public function Sets the typed data manager.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.