You are here

public function DefaultLayoutBuilderHandler::push in CMS Content Sync 8

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

Class

DefaultLayoutBuilderHandler
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();
  $entity = $intent
    ->getEntity();
  if (PushIntent::PUSH_AUTOMATICALLY != $this->settings['export']) {
    return false;
  }

  // Deletion doesn't require any action on field basis for static data.
  if (SyncIntent::ACTION_DELETE == $action) {
    return false;
  }
  $layout_builder_elements = $entity
    ->get($this->fieldName)
    ->getValue();
  $layout_builder_array = [];
  foreach ($layout_builder_elements as $key => $layout_builder_element) {

    /**
     * @var \Drupal\layout_builder\Section $layout_builder_element
     */
    $serialize = $layout_builder_element['section']
      ->toArray();
    if (isset($serialize['components'])) {
      foreach ($serialize['components'] as &$component) {
        if (isset($component['configuration']['provider'])) {
          if ('block_content' == $component['configuration']['provider']) {
            list($provider, $uuid) = explode(':', $component['configuration']['id']);
            $block_storage = \Drupal::entityTypeManager()
              ->getStorage('block_content');
            $block = $block_storage
              ->loadByProperties([
              'uuid' => $uuid,
            ]);
            if (!empty($block)) {
              $component['configuration']['block_reference'] = $intent
                ->addDependency(reset($block));
            }
          }
          elseif ('layout_builder' == $component['configuration']['provider']) {
            if (isset($component['configuration']['block_revision_id'])) {
              $block_storage = \Drupal::entityTypeManager()
                ->getStorage('block_content');
              $block = $block_storage
                ->loadByProperties([
                'revision_id' => $component['configuration']['block_revision_id'],
              ]);
              unset($component['configuration']['block_revision_id']);
              if (!empty($block)) {
                $component['configuration']['block_reference'] = $intent
                  ->addDependency(reset($block));
              }
            }
          }
        }
      }
    }
    $layout_builder_array[$key] = $serialize;
  }
  if (!empty($layout_builder_array)) {
    $intent
      ->setProperty($this->fieldName, $layout_builder_array);
    return true;
  }
  return false;
}