You are here

final class ComponentPluginTranslate in Layout Builder Symmetric Translations 8

Translates the plugin configuration if needed.

@internal Tagged services are internal.

Hierarchy

Expanded class hierarchy of ComponentPluginTranslate

1 string reference to 'ComponentPluginTranslate'
layout_builder_st.services.yml in ./layout_builder_st.services.yml
layout_builder_st.services.yml
1 service uses ComponentPluginTranslate
layout_builder.translate_block_component_subscriber in ./layout_builder_st.services.yml
Drupal\layout_builder_st\EventSubscriber\ComponentPluginTranslate

File

src/EventSubscriber/ComponentPluginTranslate.php, line 20

Namespace

Drupal\layout_builder_st\EventSubscriber
View source
final class ComponentPluginTranslate implements EventSubscriberInterface {
  use LayoutEntityHelperTrait;

  //  This trait will be merged into core's.
  use TranslationsHelperTrait;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Creates a ComponentPluginTranslate object.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(LanguageManagerInterface $language_manager, RouteMatchInterface $route_match) {
    $this->languageManager = $language_manager;
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = [
      'onBuildRender',
      200,
    ];
    return $events;
  }

  /**
   * Translates the plugin configuration if needed.
   *
   * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
   *   The section component render event.
   */
  public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    if (!$this->languageManager
      ->isMultilingual()) {
      return;
    }
    $plugin = $event
      ->getPlugin();
    $contexts = $event
      ->getContexts();
    $component = $event
      ->getComponent();
    if (!$plugin instanceof ConfigurableInterface && !isset($contexts['layout_builder.entity'])) {
      return;
    }

    // @todo Change to 'entity' in https://www.drupal.org/node/3018782.
    $entity = $contexts['layout_builder.entity']
      ->getContextValue();
    $configuration = $plugin
      ->getConfiguration();
    if ($event
      ->inPreview()) {
      $section_storage = $this->routeMatch
        ->getParameter('section_storage');
    }
    else {
      $section_storage = $this
        ->getSectionStorageForEntity($entity);
    }
    if (static::isTranslation($section_storage)) {
      if ($translated_plugin_configuration = $section_storage
        ->getTranslatedComponentConfiguration($component
        ->getUuid())) {
        $translated_plugin_configuration += $configuration;
        $plugin
          ->setConfiguration($translated_plugin_configuration);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComponentPluginTranslate::$languageManager protected property The language manager.
ComponentPluginTranslate::$routeMatch protected property The current route match.
ComponentPluginTranslate::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ComponentPluginTranslate::onBuildRender public function Translates the plugin configuration if needed.
ComponentPluginTranslate::__construct public function Creates a ComponentPluginTranslate object.
LayoutEntityHelperTrait::$sectionStorageManager protected property The section storage manager. 1
LayoutEntityHelperTrait::getEntitySections protected function Gets the sections for an entity if any.
LayoutEntityHelperTrait::getInlineBlockComponents protected function Gets components that have Inline Block plugins.
LayoutEntityHelperTrait::getInlineBlockRevisionIdsInSections protected function Gets revision IDs for layout sections.
LayoutEntityHelperTrait::getSectionStorageForEntity protected function Gets the section storage for an entity.
LayoutEntityHelperTrait::isEntityUsingFieldOverride Deprecated protected function Determines if an entity is using a field for the layout override.
LayoutEntityHelperTrait::isLayoutCompatibleEntity protected function Determines if an entity can have a layout.
LayoutEntityHelperTrait::originalEntityUsesDefaultStorage protected function Determines if the original entity used the default section storage.
LayoutEntityHelperTrait::sectionStorageManager private function Gets the section storage manager. 1
TranslationsHelperTrait::isTranslation protected static function Determines if the sections is for a translation.