diff --git a/src/Plugin/search_api/processor/RenderedItem.php b/src/Plugin/search_api/processor/RenderedItem.php index 828e8e73..1598a86b 100644 --- a/src/Plugin/search_api/processor/RenderedItem.php +++ b/src/Plugin/search_api/processor/RenderedItem.php @@ -2,15 +2,20 @@ namespace Drupal\search_api\Plugin\search_api\processor; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\Entity\EntityViewMode; +use Drupal\Core\Language\LanguageDefault; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Link; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountSwitcherInterface; use Drupal\Core\Session\UserSession; +use Drupal\Core\StringTranslation\TranslationManager; use Drupal\Core\Url; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\ItemInterface; use Drupal\search_api\LoggerTrait; +use Drupal\search_api\Plugin\search_api\datasource\ContentEntity; use Drupal\search_api\Plugin\search_api\processor\Property\RenderedItemProperty; use Drupal\search_api\Processor\ProcessorPluginBase; use Drupal\search_api\Utility\ThemeSwitcherInterface; @@ -58,6 +63,27 @@ class RenderedItem extends ProcessorPluginBase { */ protected $themeSwitcher; + /** + * Theme settings config. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + + /** + * The language default. + * + * @var \Drupal\Core\Language\LanguageDefault + */ + protected $languageDefault; + /** * {@inheritdoc} */ @@ -69,6 +95,9 @@ class RenderedItem extends ProcessorPluginBase { $plugin->setRenderer($container->get('renderer')); $plugin->setThemeSwitcher($container->get('search_api.theme_switcher')); $plugin->setLogger($container->get('logger.channel.search_api')); + $plugin->setConfigFactory($container->get('config.factory')); + $plugin->setLanguageManager($container->get('language_manager')); + $plugin->setLanguageDefault($container->get('language.default')); return $plugin; } @@ -142,6 +171,55 @@ class RenderedItem extends ProcessorPluginBase { return $this; } + /** + * Retrieves the config factory service. + * + * @return \Drupal\Core\Config\ConfigFactoryInterface + * The config factory. + */ + protected function getConfigFactory() { + return $this->configFactory ?: \Drupal::configFactory(); + } + + /** + * Sets the config factory service. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + * + * @return $this + */ + protected function setConfigFactory(ConfigFactoryInterface $config_factory) { + $this->configFactory = $config_factory; + return $this; + } + + /** + * Sets the language manager. + * + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. + * + * @return $this + */ + protected function setLanguageManager(LanguageManagerInterface $language_manager) { + $this->languageManager = $language_manager; + return $this; + } + + /** + * Sets the default language. + * + * @param \Drupal\Core\Language\LanguageDefault $language_default + * The default language. + * + * @return $this + */ + protected function setLanguageDefault(LanguageDefault $language_default) { + $this->languageDefault = $language_default; + return $this; + } + // @todo Add a supportsIndex() implementation that checks whether there is // actually any datasource present which supports viewing. @@ -212,6 +290,11 @@ class RenderedItem extends ProcessorPluginBase { if ($build) { // Add the excerpt to the render array to allow adding it to view modes. $build['#search_api_excerpt'] = $item->getExcerpt(); + + if ($item->getDatasource() instanceof ContentEntity) { + $this->changeActiveLanguage($item->getLanguage()); + } + $value = (string) $this->getRenderer()->renderPlain($build); if ($value) { $field->addValue($value); @@ -326,4 +409,38 @@ class RenderedItem extends ProcessorPluginBase { return TRUE; } + /** + * Changes the active language for translations. + * + * @param string $langcode + * The langcode. + */ + protected function changeActiveLanguage($langcode) { + if (!$this->languageManager->isMultilingual()) { + return; + } + $language = $this->languageManager->getLanguage($langcode); + if (!$language) { + return; + } + // The language manager has no method for overriding the default + // language, like it does for config overrides. We have to change the + // default language service's current language. + // @see https://www.drupal.org/project/drupal/issues/3029010 + $this->languageDefault->set($language); + $this->languageManager->setConfigOverrideLanguage($language); + $this->languageManager->reset(); + + // The default string_translation service, TranslationManager, has a + // setDefaultLangcode method. However, this method is not present on + // either of its interfaces. Therefore we check for the concrete class + // here so that any swapped service does not break the application. + // @see https://www.drupal.org/project/drupal/issues/3029003 + $string_translation = $this->getStringTranslation(); + if ($string_translation instanceof TranslationManager) { + $string_translation->setDefaultLangcode($language->getId()); + $string_translation->reset(); + } + } + }