"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/user/src/Plugin/migrate/process/UserLangcode.php" (26 Nov 2020, 2589 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "UserLangcode.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\user\Plugin\migrate\process;
    4 
    5 use Drupal\Core\Language\LanguageManager;
    6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    7 use Drupal\migrate\MigrateExecutableInterface;
    8 use Drupal\migrate\ProcessPluginBase;
    9 use Drupal\migrate\Row;
   10 use Symfony\Component\DependencyInjection\ContainerInterface;
   11 
   12 /**
   13  * Provides a process plugin for the user langcode.
   14  *
   15  * @MigrateProcessPlugin(
   16  *   id = "user_langcode"
   17  * )
   18  */
   19 class UserLangcode extends ProcessPluginBase implements ContainerFactoryPluginInterface {
   20 
   21   /**
   22    * The language manager.
   23    *
   24    * @var \Drupal\Core\Language\LanguageManager
   25    */
   26   protected $languageManager;
   27 
   28   /**
   29    * Constructs a UserLangcode object.
   30    *
   31    * @param array $configuration
   32    *   Plugin configuration.
   33    * @param string $plugin_id
   34    *   The plugin ID.
   35    * @param mixed $plugin_definition
   36    *   The plugin definition.
   37    * @param \Drupal\Core\Language\LanguageManager $language_manager
   38    *   The language manager service.
   39    */
   40   public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManager $language_manager) {
   41     parent::__construct($configuration, $plugin_id, $plugin_definition);
   42     $this->languageManager = $language_manager;
   43   }
   44 
   45   /**
   46    * {@inheritdoc}
   47    */
   48   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
   49     return new static(
   50       $configuration,
   51       $plugin_id,
   52       $plugin_definition,
   53       $container->get('language_manager')
   54     );
   55   }
   56 
   57   /**
   58    * {@inheritdoc}
   59    */
   60   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
   61     if (!isset($this->configuration['fallback_to_site_default'])) {
   62       $this->configuration['fallback_to_site_default'] = TRUE;
   63     }
   64 
   65     // If the user's language is empty, it means the locale module was not
   66     // installed, so the user's langcode should be English and the user's
   67     // preferred_langcode and preferred_admin_langcode should fallback to the
   68     // default language.
   69     if (empty($value)) {
   70       if ($this->configuration['fallback_to_site_default']) {
   71         return $this->languageManager->getDefaultLanguage()->getId();
   72       }
   73       else {
   74         return 'en';
   75       }
   76     }
   77     // If the user's language does not exist, use the default language.
   78     elseif ($this->languageManager->getLanguage($value) === NULL) {
   79       return $this->languageManager->getDefaultLanguage()->getId();
   80     }
   81 
   82     // If the langcode is a valid one, just return it.
   83     return $value;
   84   }
   85 
   86 }