class ContentEntityFallback in Entity Language Fallback 8
Represents a datasource which exposes the content entities.
In addition to default ContentEntity behavior, indexes content in languages that don't have translations, but have translations in fallback language(s).
Plugin annotation
@SearchApiDatasource(
id = "entity_language_fallback",
deriver = "Drupal\entity_language_fallback\Plugin\search_api\datasource\ContentEntityFallbackDeriver"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\search_api\Plugin\HideablePluginBase implements HideablePluginInterface
- class \Drupal\search_api\Plugin\ConfigurablePluginBase implements ConfigurablePluginInterface uses PluginDependencyTrait
- class \Drupal\search_api\Plugin\IndexPluginBase implements IndexPluginInterface
- class \Drupal\search_api\Datasource\DatasourcePluginBase implements DatasourceInterface
- class \Drupal\search_api\Plugin\search_api\datasource\ContentEntity implements PluginFormInterface uses LoggerTrait, PluginFormTrait
- class \Drupal\entity_language_fallback\Plugin\search_api\datasource\ContentEntityFallback
- class \Drupal\search_api\Plugin\search_api\datasource\ContentEntity implements PluginFormInterface uses LoggerTrait, PluginFormTrait
- class \Drupal\search_api\Datasource\DatasourcePluginBase implements DatasourceInterface
- class \Drupal\search_api\Plugin\IndexPluginBase implements IndexPluginInterface
- class \Drupal\search_api\Plugin\ConfigurablePluginBase implements ConfigurablePluginInterface uses PluginDependencyTrait
- class \Drupal\search_api\Plugin\HideablePluginBase implements HideablePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ContentEntityFallback
1 file declares its use of ContentEntityFallback
- entity_language_fallback.module in ./
entity_language_fallback.module - Add fallback languages to entities.
File
- src/
Plugin/ search_api/ datasource/ ContentEntityFallback.php, line 24
Namespace
Drupal\entity_language_fallback\Plugin\search_api\datasourceView source
class ContentEntityFallback extends ContentEntity {
/**
* Fallback controller
*
* @var \Drupal\entity_language_fallback\FallbackControllerInterface
*
*/
protected $fallbackController;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var static $datasource */
$datasource = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$datasource
->setFallbackController($container
->get('language_fallback.controller'));
return $datasource;
}
/**
* {@inheritdoc}
*/
public function loadMultiple(array $ids) {
$allowed_languages = $this
->getLanguages();
$entity_ids = [];
foreach ($ids as $item_id) {
$pos = strrpos($item_id, ':');
// This can only happen if someone passes an invalid ID, since we always
// include a language code. Still, no harm in guarding against bad input.
if ($pos === FALSE) {
continue;
}
$entity_id = substr($item_id, 0, $pos);
$langcode = substr($item_id, $pos + 1);
if (isset($allowed_languages[$langcode])) {
$entity_ids[$entity_id][$item_id] = $langcode;
}
}
/** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
$entities = $this
->getEntityStorage()
->loadMultiple(array_keys($entity_ids));
$items = [];
$allowed_bundles = $this
->getBundles();
foreach ($entity_ids as $entity_id => $langcodes) {
if (empty($entities[$entity_id]) || !isset($allowed_bundles[$entities[$entity_id]
->bundle()])) {
continue;
}
foreach ($this->languages as $langcode => $language) {
$item_id = $entity_id . ':' . $langcode;
if (!in_array($item_id, $ids)) {
continue;
}
if ($entities[$entity_id]
->hasTranslation($langcode)) {
$items[$item_id] = $entities[$entity_id]
->getTranslation($langcode)
->getTypedData();
$items[$item_id]->language = $langcode;
}
else {
$source = $this->fallbackController
->getTranslation($langcode, $entities[$entity_id]);
if (!$source) {
continue;
}
$translation = $entities[$entity_id]
->addTranslation($langcode, $source
->toArray());
$items[$item_id] = $translation
->getTypedData();
$items[$item_id]->language = $langcode;
}
}
}
return $items;
}
/**
* {@inheritdoc}
*/
public function getPartialItemIds($page = NULL, array $bundles = NULL, array $languages = NULL) {
$parent_items = parent::getPartialItemIds($page, $bundles, $languages);
if (empty($parent_items)) {
return $parent_items;
}
$entity_ids = [];
foreach ($parent_items as $parent_item) {
list($id, ) = Utility::splitPropertyPath($parent_item);
$entity_ids[$id] = 1;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
foreach ($this
->getEntityStorage()
->loadMultiple(array_keys($entity_ids)) as $entity_id => $entity) {
foreach ($this->languages as $langcode => $language) {
if ($entity
->hasTranslation($langcode)) {
$item_ids[] = "{$entity_id}:{$langcode}";
}
else {
$fallback_found = FALSE;
foreach ($this->fallback_chain[$langcode] as $candidate) {
if ($entity
->hasTranslation($candidate)) {
$fallback_found = TRUE;
break;
}
}
if ($fallback_found) {
$item_ids[] = "{$entity_id}:{$langcode}";
}
}
}
}
return $item_ids;
}
/**
* {@inheritdoc}
*/
protected function getLanguages() {
$parent_languages = parent::getLanguages();
if (!isset($this->languages)) {
$this->languages = ConfigurableLanguage::loadMultiple(array_keys($parent_languages));
foreach ($this->languages as $langcode => $language) {
$this->fallback_chain[$langcode] = $language
->getThirdPartySetting('entity_language_fallback', 'fallback_langcodes', []);
}
}
return $parent_languages;
}
/**
* {@inheritdoc}
*/
public static function getIndexesForEntity(ContentEntityInterface $entity) {
$datasource_id = 'entity_language_fallback:' . $entity
->getEntityTypeId();
$entity_bundle = $entity
->bundle();
$has_bundles = $entity
->getEntityType()
->hasKey('bundle');
$indexes = Index::loadMultiple();
foreach ($indexes as $index_id => $index) {
// Filter our indexes that don't contain the datasource in question.
if (!$index
->isValidDatasource($datasource_id)) {
unset($indexes[$index_id]);
}
elseif ($has_bundles) {
// If the entity type supports bundles, we also have to filter out
// indexes that exclude the entity's bundle.
$config = $index
->getDatasource($datasource_id)
->getConfiguration();
$default = !empty($config['bundles']['default']);
$bundle_set = in_array($entity_bundle, $config['bundles']['selected']);
if ($default == $bundle_set) {
unset($indexes[$index_id]);
}
}
}
return $indexes;
}
/**
* Set fallback controller instance.
*
* @param \Drupal\entity_language_fallback\FallbackControllerInterface $controller
*/
public function setFallbackController(FallbackControllerInterface $controller) {
$this->fallbackController = $controller;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigurablePluginBase:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | |
ConfigurablePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ConfigurablePluginBase:: |
public | function |
Returns the plugin's description. Overrides ConfigurablePluginInterface:: |
|
ConfigurablePluginBase:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
ConfigurablePluginBase:: |
public | function |
Returns the label for use on the administration pages. Overrides ConfigurablePluginInterface:: |
|
ConfigurablePluginBase:: |
protected | function | Wraps the module handler. | |
ConfigurablePluginBase:: |
public | function |
Informs the plugin that some of its dependencies are being removed. Overrides ConfigurablePluginInterface:: |
5 |
ConfigurablePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
3 |
ConfigurablePluginBase:: |
protected | function | Wraps the theme handler. | |
ContentEntity:: |
protected | property | The config factory. | |
ContentEntity:: |
protected | property | The database connection. | |
ContentEntity:: |
protected | property | The entity display repository manager. | |
ContentEntity:: |
protected | property | The entity field manager. | |
ContentEntity:: |
protected | property | The entity type bundle info. | |
ContentEntity:: |
protected | property | The entity type manager. | |
ContentEntity:: |
protected | property | The fields helper. | |
ContentEntity:: |
protected | property | The language manager. | |
ContentEntity:: |
protected | property | The entity memory cache. | |
ContentEntity:: |
protected | property | The state service. | |
ContentEntity:: |
protected | property | The typed data manager. | |
ContentEntity:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
ContentEntity:: |
public | function |
Calculates dependencies for the configured plugin. Overrides ConfigurablePluginBase:: |
|
ContentEntity:: |
public | function |
Determines whether this datasource can contain entity references. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurablePluginBase:: |
|
ContentEntity:: |
public | function |
Identifies items affected by a change to a referenced entity. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Retrieves the bundles associated to this datasource. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function | Retrieves the config factory. | |
ContentEntity:: |
protected | function | Retrieves the config value for a certain key in the Search API settings. | |
ContentEntity:: |
public | function | Retrieves the database connection. | |
ContentEntity:: |
protected | function | Retrieves the entity from a search item. | |
ContentEntity:: |
protected | function | Retrieves the available bundles of this entity type as an options list. | |
ContentEntity:: |
protected | function | Retrieves all bundles of this datasource's entity type. | |
ContentEntity:: |
public | function | Retrieves the entity display repository. | |
ContentEntity:: |
public | function | Retrieves the entity field manager. | |
ContentEntity:: |
public | function | Retrieves the entity memory cache service. | |
ContentEntity:: |
protected | function | Retrieves the entity storage. | |
ContentEntity:: |
protected | function | Returns the definition of this datasource's entity type. | |
ContentEntity:: |
public | function | Retrieves the entity display repository. | |
ContentEntity:: |
public | function |
Retrieves the entity type ID of items from this datasource, if any. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function | Retrieves the entity type manager. | |
ContentEntity:: |
public | function |
Retrieves any dependencies of the given fields. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function | Retrieves the fields helper. | |
ContentEntity:: |
public | function |
Checks whether a user has permission to view the given item. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Retrieves the item's bundle. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Retrieves the unique ID of an object from this datasource. Overrides DatasourceInterface:: |
|
ContentEntity:: |
public | function |
Returns a list of IDs of items from this datasource. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Retrieves a human-readable label for an item. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Retrieves a URL at which the item can be viewed on the web. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function | Retrieves the language manager. | |
ContentEntity:: |
public | function |
Returns the list cache contexts associated with this datasource. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Retrieves the properties exposed by the underlying complex data type. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
protected | function | Computes all dependencies of the given property path. | |
ContentEntity:: |
public | function | Retrieves the state service. | |
ContentEntity:: |
protected | function | Retrieves the available languages of this entity type as an options list. | |
ContentEntity:: |
public | function | Retrieves the typed data manager. | |
ContentEntity:: |
public | function |
Returns the available view modes for this datasource. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
protected | function | Determines whether the entity type supports bundles. | |
ContentEntity:: |
protected | function | Determines whether the entity type supports translations. | |
ContentEntity:: |
public | function | Sets the config factory. | |
ContentEntity:: |
public | function | Sets the database connection. | |
ContentEntity:: |
public | function | Sets the entity display repository. | |
ContentEntity:: |
public | function | Sets the entity field manager. | |
ContentEntity:: |
public | function | Sets the entity memory cache service. | |
ContentEntity:: |
public | function | Sets the entity type bundle info. | |
ContentEntity:: |
public | function | Sets the entity type manager. | |
ContentEntity:: |
public | function | Sets the fields helper. | |
ContentEntity:: |
public | function | Sets the language manager. | |
ContentEntity:: |
public | function | Sets the state service. | |
ContentEntity:: |
public | function | Sets the typed data manager. | |
ContentEntity:: |
public | function |
Form submission handler. Overrides PluginFormTrait:: |
|
ContentEntity:: |
constant | The key for accessing last tracked ID information in site state. | ||
ContentEntity:: |
public | function |
Returns the render array for the provided item and view mode. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Returns the render array for the provided items and view mode. Overrides DatasourcePluginBase:: |
|
ContentEntity:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides IndexPluginBase:: |
|
ContentEntityFallback:: |
protected | property | Fallback controller | |
ContentEntityFallback:: |
public static | function |
Creates an instance of the plugin. Overrides ContentEntity:: |
|
ContentEntityFallback:: |
public static | function |
Overrides ContentEntity:: |
|
ContentEntityFallback:: |
protected | function |
Retrieves the enabled languages. Overrides ContentEntity:: |
|
ContentEntityFallback:: |
public | function |
Overrides ContentEntity:: |
|
ContentEntityFallback:: |
public | function |
Loads multiple items. Overrides ContentEntity:: |
|
ContentEntityFallback:: |
public | function | Set fallback controller instance. | |
DatasourcePluginBase:: |
public | function |
Checks whether a user has permission to view the given item. Overrides DatasourceInterface:: |
|
DatasourcePluginBase:: |
public | function |
Retrieves the item's language. Overrides DatasourceInterface:: |
1 |
DatasourcePluginBase:: |
public | function |
Loads an item. Overrides DatasourceInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
HideablePluginBase:: |
public | function |
Determines whether this plugin should be hidden in the UI. Overrides HideablePluginInterface:: |
1 |
IndexPluginBase:: |
protected | property | The index this processor is configured for. | |
IndexPluginBase:: |
public | function |
Retrieves the index this plugin is configured for. Overrides IndexPluginInterface:: |
|
IndexPluginBase:: |
public | function |
Sets the index this plugin is configured for. Overrides IndexPluginInterface:: |
|
LoggerTrait:: |
protected | property | The logging channel to use. | |
LoggerTrait:: |
public | function | Retrieves the logger. | |
LoggerTrait:: |
protected | function | Logs an exception. | |
LoggerTrait:: |
public | function | Sets the logger. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. Aliased as: traitCalculatePluginDependencies | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. Aliased as: traitGetPluginDependencies | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. Aliased as: traitModuleHandler | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. Aliased as: traitThemeHandler | 1 |
PluginFormTrait:: |
public | function | Form validation handler. | 2 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |