You are here

public function DefaultFileHandler::push in CMS Content Sync 2.1.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::push()
  2. 2.0.x src/Plugin/cms_content_sync/field_handler/DefaultFileHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultFileHandler::push()

Parameters

\Drupal\cms_content_sync\SyncIntent $intent:

Return value

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

Throws

\Drupal\cms_content_sync\Exception\SyncException

Overrides FieldHandlerBase::push

File

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

Class

DefaultFileHandler
Providing a minimalistic implementation for any field type.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler

Code

public function push(PushIntent $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;
  }
  $result = [];
  $file = null;
  $invalid_subfields = [
    '_accessCacheability',
    '_attributes',
    '_loaded',
    'top',
    'target_revision_id',
    'subform',
  ];
  if ('uri' == $this->fieldDefinition
    ->getType()) {
    $data = $entity
      ->get($this->fieldName)
      ->getValue();
    foreach ($data as $i => $value) {
      $files = \Drupal::entityTypeManager()
        ->getStorage('file')
        ->loadByProperties([
        'uri' => $value['value'],
      ]);
      $file = empty($files) ? null : reset($files);
      if ($file) {
        foreach ($invalid_subfields as $invalid_subfield) {
          unset($value[$invalid_subfield]);
        }
        unset($value['value']);
        $result[] = $this
          ->embedFile($intent, $file, $value, $entity->{$this->fieldName}[$i]);
      }
    }
  }
  else {
    $data = $entity
      ->get($this->fieldName)
      ->getValue();
    foreach ($data as $i => $value) {
      if (empty($value['target_id'])) {
        continue;
      }
      $file = File::load($value['target_id']);
      if ($file) {
        foreach ($invalid_subfields as $invalid_subfield) {
          unset($value[$invalid_subfield]);
        }
        unset($value['target_id']);
        $result[] = $this
          ->embedFile($intent, $file, $value, $entity->{$this->fieldName}[$i]);
      }
    }
  }
  $intent
    ->setProperty($this->fieldName, $result);
  return true;
}