You are here

public function DefaultFormattedTextHandler::push in CMS Content Sync 2.1.x

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

Class

DefaultFormattedTextHandler
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) {
  if (!parent::push($intent)) {
    return false;
  }
  $entity = $intent
    ->getEntity();
  $add_file_uri_dependency = function ($matches) use ($intent) {

    // PDF files can have a #page=... anchor attached that we want to keep.
    $path = preg_replace('@#.*$@', '', $matches[1]);
    $uri = 'public://' . urldecode($path);

    /** @var FileInterface[] $files */
    $files = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->loadByProperties([
      'uri' => $uri,
    ]);
    if (!count($files)) {
      \Drupal::logger('cms_content_sync')
        ->error('Failed to push referenced file by URI in the formatted text as the file is missing on this site: @uri<br>Flow: @flow_id | Pool: @pool_id', [
        '@uri' => $uri,
        '@flow_id' => $intent
          ->getFlow()
          ->id(),
        '@pool_id' => $intent
          ->getPool()
          ->id(),
      ]);
      return '';
    }
    $file = reset($files);
    $intent
      ->addDependency($file);
    return '';
  };
  $base_path = PublicStream::basePath();
  foreach ($entity
    ->get($this->fieldName)
    ->getValue() as $item) {
    $text = $item['value'];

    // Simple image embedding (default ckeditor + IMCE images)
    preg_replace_callback('@<img[^>]+src="/' . $base_path . '/([^"]+)"@', $add_file_uri_dependency, $text);

    // Other file embedding (IMCE files)
    preg_replace_callback('@<a[^>]+href="/' . $base_path . '/([^"]+)"@', $add_file_uri_dependency, $text);

    // Entity embedding (especially media)
    preg_replace_callback('@<drupal-(entity|media)[^>]+data-entity-type="([^"]+)"\\s+data-entity-uuid="([^"]+)"@', function ($matches) use ($intent) {
      $type = $matches[2];
      $uuid = $matches[3];
      $entity = \Drupal::service('entity.repository')
        ->loadEntityByUuid($type, $uuid);
      if (!$entity) {
        return '';
      }
      $intent
        ->addDependency($entity);
      return '';
    }, $text);
  }
  return true;
}