You are here

public function DefaultFileHandler::pull in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/cms_content_sync/field_handler/DefaultFileHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultFileHandler::pull()
  2. 2.1.x src/Plugin/cms_content_sync/field_handler/DefaultFileHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultFileHandler::pull()

Parameters

\Drupal\cms_content_sync\SyncIntent $intent: The request containing all pushed data

Return value

bool Whether or not the content has been pulled. FALSE is a desired state, meaning the entity should not be pulled according to config.

Throws

\Drupal\cms_content_sync\Exception\SyncException

Overrides FieldHandlerBase::pull

File

src/Plugin/cms_content_sync/field_handler/DefaultFileHandler.php, line 37

Class

DefaultFileHandler
Providing a minimalistic implementation for any field type.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler

Code

public function pull(PullIntent $intent) {
  $action = $intent
    ->getAction();

  /**
   * @var \Drupal\Core\Entity\FieldableEntityInterface $entity
   */
  $entity = $intent
    ->getEntity();

  // Deletion doesn't require any action on field basis for static data.
  if (SyncIntent::ACTION_DELETE == $action) {
    return false;
  }
  if ($intent
    ->shouldMergeChanges()) {
    return false;
  }
  $data = $intent
    ->getProperty($this->fieldName);
  if (empty($data)) {
    $entity
      ->set($this->fieldName, null);
  }
  else {
    $file_ids = [];
    foreach ($data as $value) {

      /**
       * @var \Drupal\file\Entity\File $file
       */
      $file = $intent
        ->loadEmbeddedEntity($value);
      $meta = $intent
        ->getEmbeddedEntityData($value);
      if ($file) {
        $meta['target_id'] = $file
          ->id();
        $file_ids[] = $meta;
      }
    }
    $entity
      ->set($this->fieldName, $file_ids);
  }
  return true;
}