You are here

public function EntityHandlerBase::push in CMS Content Sync 2.0.x

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

Parameters

\Drupal\cms_content_sync\PushIntent $intent: The request to store all relevant info at

Return value

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

Throws

\Drupal\cms_content_sync\Exception\SyncException

Overrides EntityHandlerInterface::push

5 calls to EntityHandlerBase::push()
DefaultConfigEntityHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultConfigEntityHandler.php
DefaultMediaHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultMediaHandler.php
DefaultMenuLinkContentHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultMenuLinkContentHandler.php
DefaultNodeHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultNodeHandler.php
DefaultTaxonomyHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php
5 methods override EntityHandlerBase::push()
DefaultConfigEntityHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultConfigEntityHandler.php
DefaultMediaHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultMediaHandler.php
DefaultMenuLinkContentHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultMenuLinkContentHandler.php
DefaultNodeHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultNodeHandler.php
DefaultTaxonomyHandler::push in src/Plugin/cms_content_sync/entity_handler/DefaultTaxonomyHandler.php

File

src/Plugin/EntityHandlerBase.php, line 274

Class

EntityHandlerBase
Common base class for entity handler plugins.

Namespace

Drupal\cms_content_sync\Plugin

Code

public function push(PushIntent $intent, EntityInterface $entity = null) {
  if ($this
    ->ignorePush($intent)) {
    return false;
  }
  if (!$entity) {
    $entity = $intent
      ->getEntity();
  }

  // Base info.
  $name = $this
    ->getEntityName($entity, $intent);

  // Focal point for example has no label.
  if (!$name) {
    $name = 'Unnamed ' . $entity
      ->getEntityTypeId() . '.' . $entity
      ->bundle();
  }
  $intent
    ->getOperation()
    ->setName($name, $intent
    ->getActiveLanguage());

  // Menu items.
  if ($this
    ->pushReferencedMenuItems()) {
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');

    /**
     * @var \Drupal\Core\Menu\MenuLinkManager $menu_link_manager
     */
    $menu_items = $menu_link_manager
      ->loadLinksByRoute('entity.' . $this->entityTypeName . '.canonical', [
      $this->entityTypeName => $entity
        ->id(),
    ]);
    $values = [];
    $form_values = _cms_content_sync_submit_cache($entity
      ->getEntityTypeId(), $entity
      ->uuid());
    foreach ($menu_items as $menu_item) {
      if (!$menu_item instanceof MenuLinkContent) {
        continue;
      }

      /**
       * @var \Drupal\menu_link_content\Entity\MenuLinkContent $item
       */
      $item = \Drupal::service('entity.repository')
        ->loadEntityByUuid('menu_link_content', $menu_item
        ->getDerivativeId());
      if (!$item) {
        continue;
      }

      // Menu item has just been disabled => Ignore push in this case.
      if (isset($form_values['menu']) && $form_values['menu']['id'] == 'menu_link_content:' . $item
        ->uuid()) {
        if (!$form_values['menu']['enabled']) {
          continue;
        }
      }
      $details = [];
      $details['enabled'] = $item
        ->get('enabled')->value;
      $values[] = $intent
        ->addDependency($item, $details);
    }
    $intent
      ->setProperty('menu_items', $values);
  }

  // Preview.
  $view_mode = $this->flow
    ->getPreviewType($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  if (Flow::PREVIEW_DISABLED != $view_mode) {
    $entityTypeManager = \Drupal::entityTypeManager();
    $view_builder = $entityTypeManager
      ->getViewBuilder($this->entityTypeName);
    $preview = $view_builder
      ->view($entity, $view_mode);
    $rendered = \Drupal::service('renderer');
    $html = $rendered
      ->executeInRenderContext(new RenderContext(), function () use ($rendered, $preview) {
      return $rendered
        ->render($preview);
    });
    $this
      ->setPreviewHtml($html, $intent);
  }

  // Source URL.
  $intent
    ->getOperation()
    ->setSourceDeepLink($this
    ->getViewUrl($entity), $intent
    ->getActiveLanguage());

  // Fields.
  if ($entity instanceof FieldableEntityInterface) {

    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */
    $entityFieldManager = \Drupal::service('entity_field.manager');
    $type = $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();
    $field_definitions = $entityFieldManager
      ->getFieldDefinitions($type, $bundle);
    foreach ($field_definitions as $key => $field) {
      $handler = $this->flow
        ->getFieldHandler($type, $bundle, $key);
      if (!$handler) {
        continue;
      }
      $handler
        ->push($intent);
    }
  }

  // Translations.
  if (!$intent
    ->getActiveLanguage() && $this
    ->isEntityTypeTranslatable($entity)) {
    $languages = array_keys($entity
      ->getTranslationLanguages(false));
    foreach ($languages as $language) {
      $intent
        ->changeTranslationLanguage($language);

      /**
       * @var \Drupal\Core\Entity\FieldableEntityInterface $translation
       */
      $translation = $entity
        ->getTranslation($language);
      $this
        ->push($intent, $translation);
    }
    $intent
      ->changeTranslationLanguage();
  }

  // Allow other modules to extend the EntityHandlerBase push.
  // Dispatch entity push event.
  \Drupal::service('event_dispatcher')
    ->dispatch(BeforeEntityPush::EVENT_NAME, new BeforeEntityPush($entity, $intent));
  return true;
}