You are here

public function JsonapiHelper::handlePhysicalFiles in Entity Share 8

Same name and namespace in other branches
  1. 8.2 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::handlePhysicalFiles()

Create or update the entity reference field values of an entity.

TODO: Should this method be removed from the interface?

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to update.

array $data: An array of data. Can be modified to change the URI if needed.

Overrides JsonapiHelperInterface::handlePhysicalFiles

1 call to JsonapiHelper::handlePhysicalFiles()
JsonapiHelper::importEntityListData in modules/entity_share_client/src/Service/JsonapiHelper.php
Use data from the JSONAPI to import content.

File

modules/entity_share_client/src/Service/JsonapiHelper.php, line 280

Class

JsonapiHelper
Class JsonapiHelper.

Namespace

Drupal\entity_share_client\Service

Code

public function handlePhysicalFiles(ContentEntityInterface $entity, array &$data) {
  if ($entity instanceof FileInterface) {
    $remote_uri = $data['attributes']['uri']['value'];
    $remote_url = $data['attributes']['uri']['url'];
    $stream_wrapper = $this->streamWrapperManager
      ->getViaUri($remote_uri);
    $directory_uri = $stream_wrapper
      ->dirname($remote_uri);

    // Create the destination folder.
    if (file_prepare_directory($directory_uri, FILE_CREATE_DIRECTORY)) {

      // TODO: Check the case of large files.
      // TODO: Transfer file only if necessary.
      try {
        $file_content = $this
          ->getFileHttpClient()
          ->get($remote_url)
          ->getBody()
          ->getContents();
        file_put_contents($remote_uri, $file_content);
      } catch (ClientException $e) {
        drupal_set_message($this
          ->t('Missing file: %url', [
          '%url' => $remote_url,
        ]), 'warning');
      }
    }
    else {
      drupal_set_message($this
        ->t('Impossible to write in the directory %directory', [
        '%directory' => $directory_uri,
      ]), 'error');
    }
  }
}