You are here

public function DefaultVideoHandler::pull in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/cms_content_sync/field_handler/DefaultVideoHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultVideoHandler::pull()
  2. 2.0.x src/Plugin/cms_content_sync/field_handler/DefaultVideoHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultVideoHandler::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/DefaultVideoHandler.php, line 36

Class

DefaultVideoHandler
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 {
    $result = [];
    foreach ($data as $value) {
      $meta = $intent
        ->getEmbeddedEntityData($value);
      $file = null;
      if (empty($value['uri']) || empty($value['uuid'])) {
        $file = $intent
          ->loadEmbeddedEntity($value);
      }
      else {
        $file = \Drupal::service('entity.repository')
          ->loadEntityByUuid('file', $value['uuid']);
        if (empty($file)) {
          $file = File::create([
            'uuid' => $value['uuid'],
            'uri' => $value['uri'],
            'filemime' => $value['mimetype'],
            'filesize' => 1,
          ]);
          $file
            ->setPermanent();
          $file
            ->save();
        }
      }
      if ($file) {
        $meta['target_id'] = $file
          ->id();
        $result[] = $meta;
      }
    }
    $entity
      ->set($this->fieldName, $result);
  }
  return true;
}