LanguageWithFallback.php in Search API 8
File
src/Plugin/search_api/processor/LanguageWithFallback.php
View source
<?php
namespace Drupal\search_api\Plugin\search_api\processor;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Drupal\search_api\SearchApiException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LanguageWithFallback extends ProcessorPluginBase {
protected $entityRepository;
protected $languageManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$processor = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$processor
->setEntityRepository($container
->get('entity.repository'));
$processor
->setLanguageManager($container
->get('language_manager'));
return $processor;
}
public function getEntityRepository() {
return $this->entityRepository;
}
public function setEntityRepository(EntityRepositoryInterface $entityRepository) {
$this->entityRepository = $entityRepository;
return $this;
}
public function getLanguageManager() {
return $this->languageManager;
}
public function setLanguageManager(LanguageManagerInterface $languageManager) {
$this->languageManager = $languageManager;
return $this;
}
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this
->t('Language (with fallback)'),
'description' => $this
->t('The item language, or a language the item is a fallback for.'),
'type' => 'string',
'settings' => [
'views_type' => 'language',
],
'processor_id' => $this
->getPluginId(),
'is_list' => TRUE,
];
$properties['language_with_fallback'] = new ProcessorProperty($definition);
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
try {
$entity = $item
->getOriginalObject()
->getValue();
} catch (SearchApiException $e) {
return;
}
if (!$entity instanceof ContentEntityInterface) {
return;
}
$langcodes = $this
->getReverseLanguageFallbacks($entity);
$fields = $item
->getFields();
$fields = $this
->getFieldsHelper()
->filterForPropertyPath($fields, NULL, 'language_with_fallback');
foreach ($fields as $field) {
foreach ($langcodes as $langcode) {
$field
->addValue($langcode);
}
}
}
protected function getReverseLanguageFallbacks(ContentEntityInterface $entity) {
$entityLangcode = $entity
->language()
->getId();
$reverseFallbackLangcodes = [
$entityLangcode,
];
foreach ($this->languageManager
->getLanguages() as $langcode => $language) {
if ($langcode === $entityLangcode) {
continue;
}
$context = [
'fallback_to_passed_entity' => FALSE,
'operation' => 'entity_upcast',
];
$fallback = $this->entityRepository
->getTranslationFromContext($entity, $langcode, $context);
if ($fallback && $fallback
->language()
->getId() === $entityLangcode) {
$reverseFallbackLangcodes[] = $langcode;
}
}
return $reverseFallbackLangcodes;
}
}