You are here

public function FileUrlFieldItemList::postSave in File URL 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldType/FileUrlFieldItemList.php \Drupal\file_url\Plugin\Field\FieldType\FileUrlFieldItemList::postSave()

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after item values are written into storage.

Parameters

bool $update: Specifies whether the entity is being updated or created.

Return value

bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

Overrides FieldItemList::postSave

See also

\Drupal\Core\Field\FieldItemInterface::postSave()

File

src/Plugin/Field/FieldType/FileUrlFieldItemList.php, line 44

Class

FileUrlFieldItemList
Represents a configurable entity file URL field.

Namespace

Drupal\file_url\Plugin\Field\FieldType

Code

public function postSave($update) {

  /** @var \Drupal\file_url\FileUrlHandler $file_handler */
  $file_handler = \Drupal::service('file_url.handler');
  $entity = $this
    ->getEntity();
  if (!$update) {

    // Add a new usage for newly uploaded files.
    foreach ($this
      ->referencedEntities() as $file) {
      if (!$file_handler
        ->isRemote($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) {
        if (!$file_handler
          ->isRemote($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) {
        $file = $file_handler::urlToFile($item->target_id);
        if (!$file_handler
          ->isRemote($file)) {
          $original_ids[] = $file
            ->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::service('entity_type.manager')
      ->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) && !$file_handler
        ->isRemote($file)) {
        \Drupal::service('file.usage')
          ->add($file, 'file', $entity
          ->getEntityTypeId(), $entity
          ->id());
      }
    }
  }
}