acquia_vwo-1.0.x-dev/modules/acquia_vwo_content/src/Service/Helper/EntityHelper.php

modules/acquia_vwo_content/src/Service/Helper/EntityHelper.php
<?php

namespace Drupal\acquia_vwo_content\Service\Helper;

use Drupal\acquia_vwo_content\Session\AcquiaVwoContentUserSession;
use Drupal\acquia_vwo_content\Storage\AcquiaVwoStorage;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\UserSession;

/**
 * Entity helper service.
 */
class EntityHelper {

  const ENTITY_CONFIG_NAME = 'acquia_vwo_content.entity_config';

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;


  /**
   * The rendered user.
   *
   * @var \Drupal\Core\Session\UserSession
   */
  protected $renderUser;

  /**
   * The Acquia VWO Storage service.
   *
   * @var \Drupal\acquia_vwo_content\Storage\AcquiaVwoStorage
   */
  protected $acquiaVwoStorage;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\acquia_vwo_content\Storage\AcquiaVwoStorage $acquia_vwo_storage
   *   The Acquia VWO Storage service.
   */
  public function __construct(
    RendererInterface $renderer,
    ConfigFactoryInterface $config_factory,
    AcquiaVwoStorage $acquia_vwo_storage,
  ) {
    $this->renderer = $renderer;
    $this->configFactory = $config_factory;
    $this->acquiaVwoStorage = $acquia_vwo_storage;
  }

  /**
   * Is Eligible for export.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The current entity.
   */
  public function isEligibleForExport(EntityInterface $entity): bool {
    $entity_config = $this->getEntityConfigValue($entity);
    if (empty($entity_config)) {
      return FALSE;
    }
    if ($entity_config['export_all']) {
      return TRUE;
    }

    return $this->acquiaVwoStorage->isMarkedForExport($entity->uuid());
  }

  /**
   * Returns the value of the entity view modes setting.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The current entity.
   *
   * @return array
   *   The setting value.
   */
  public function getEntityConfigValue(EntityInterface $entity): array {
    $entity_config = $this->configFactory
      ->get('acquia_vwo_content.entity_config')->get("entity_config.{$entity->getEntityTypeId()}.{$entity->bundle()}");
    if ($entity_config) {
      return $entity_config;
    }
    return [];
  }

  /**
   * Get rendered content.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The current entity.
   * @param array $entity_config
   *   The view mode.
   * @param string $langcode
   *   The language code.
   */
  public function getRenderedContent(EntityInterface $entity, array $entity_config, string $langcode): string {
    $render_role = $entity_config['render_role'];
    $account = $this->getRenderUser($render_role);

    Util::switchAccountTo($account);

    if (!$entity->access('view', $account, FALSE)) {
      Util::switchAccountBack();
      return '';
    }

    $elements = Util::getViewModeMinimalHtml($entity, $entity_config['view_mode'], $langcode);
    $render_content = $this->renderElements($elements);

    $this->saveAttachedLibraries($entity, $elements, $render_content);

    Util::switchAccountBack();

    return $render_content;
  }

  /**
   * Renders the elements based on the Drupal version.
   *
   * @param array $elements
   *   The renderable elements.
   *
   * @return string
   *   The rendered content.
   */
  private function renderElements(array &$elements): string {
    $currentVersion = \Drupal::VERSION;
    $normalizedVersion = str_ends_with($currentVersion, '-dev') ? str_replace(['.x-dev', '-dev'], '.0', $currentVersion) : $currentVersion;

    if (version_compare($normalizedVersion, '10.3', '>=')) {
      return $this->renderer->renderInIsolation($elements);
    }

    return $this->renderer->renderPlain($elements);
  }

  /**
   * Saves attached libraries for VWO.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param array $elements
   *   The renderable elements.
   * @param string $render_content
   *   The rendered content.
   *
   * @return string
   *   The updated rendered content.
   */
  private function saveAttachedLibraries(EntityInterface $entity, array &$elements, string $render_content): string {
    if (!empty($elements['#attached']['library'])) {
      Util::saveLibrariesForPerz($entity->uuid(), $elements['#attached']['library']);
    }

    if (!empty($elements['#attached']['cohesion'])) {
      $attachment = implode('', $elements['#attached']['cohesion']);
      $render_content .= $attachment;
    }

    return $render_content;
  }

  /**
   * Export entity by view mode.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The current entity.
   * @param array $entity_config
   *   The view mode.
   * @param string $langcode
   *   The language code.
   *
   * @return array
   *   Array of entities.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getEntityVariation(EntityInterface $entity, array $entity_config, string $langcode): array {
    $render_role = $entity_config['render_role'] ?? 'anonymous';
    $rendered_data = $this->getRenderedContent($entity, $entity_config, $langcode);
    $entity_label = !empty($rendered_data) ? $entity->label() : $entity->label() . ' (no content)';

    $payload_data = [
      "name" => $entity_label,
      "view_mode" => $entity_config['view_mode'],
      "description" => $entity_label,
      "code" => [
        "html" => !empty($rendered_data) ? $rendered_data : '<!-- ACQUIA DEBUG: this content cannot be accessed by the render role ' . $render_role . '  -->',
        "js" => [
          "data" => "Drupal.attachBehaviors();",
        ],
      ],
    ];

    return $payload_data;
  }

  /**
   * Get rendered user.
   *
   * @param string $render_role
   *   The render user role.
   *
   * @return \Drupal\acquia_vwo_content\Session\AcquiaVwoContentUserSession|\Drupal\Core\Session\UserSession
   *   The rendered user.
   */
  public function getRenderUser(string $render_role): AcquiaVwoContentUserSession|UserSession {
    if (!$this->renderUser) {
      $this->renderUser = new AcquiaVwoContentUserSession($render_role);
    }
    return $this->renderUser;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc