UserLangcode.php in Drupal 10
File
core/modules/user/src/Plugin/migrate/process/UserLangcode.php
View source
<?php
namespace Drupal\user\Plugin\migrate\process;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserLangcode extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManager $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('language_manager'));
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!isset($this->configuration['fallback_to_site_default'])) {
$this->configuration['fallback_to_site_default'] = TRUE;
}
if (empty($value)) {
if ($this->configuration['fallback_to_site_default']) {
return $this->languageManager
->getDefaultLanguage()
->getId();
}
else {
return 'en';
}
}
elseif ($this->languageManager
->getLanguage($value) === NULL) {
return $this->languageManager
->getDefaultLanguage()
->getId();
}
return $value;
}
}