Chris@0: get('entity_type.manager')->getStorage($entity_type->id()), Chris@0: $container->get('language_manager'), Chris@17: $container->get('config.factory'), Chris@17: $container->get('messenger') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Constructs a new LanguageListBuilder object. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type Chris@0: * The entity type definition. Chris@0: * @param \Drupal\Core\Entity\EntityStorageInterface $storage Chris@0: * The entity storage handler class. Chris@0: * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager Chris@0: * The language manager. Chris@0: * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory Chris@0: * The factory for configuration objects. Chris@17: * @param \Drupal\Core\Messenger\MessengerInterface $messenger Chris@17: * The messenger. Chris@0: */ Chris@17: public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) { Chris@0: parent::__construct($entity_type, $storage); Chris@0: $this->languageManager = $language_manager; Chris@0: $this->configFactory = $config_factory; Chris@17: $this->messenger = $messenger; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function load() { Chris@0: $entities = $this->storage->loadByProperties(['locked' => FALSE]); Chris@0: Chris@0: // Sort the entities using the entity class's sort() method. Chris@0: // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort(). Chris@0: uasort($entities, [$this->entityType->getClass(), 'sort']); Chris@0: return $entities; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormId() { Chris@0: return 'language_admin_overview_form'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildHeader() { Chris@0: $header = [ Chris@0: 'label' => t('Name'), Chris@0: 'default' => t('Default'), Chris@0: ] + parent::buildHeader(); Chris@0: return $header; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildRow(EntityInterface $entity) { Chris@0: $row['label'] = $entity->label(); Chris@0: $row['default'] = [ Chris@0: '#type' => 'radio', Chris@0: '#parents' => ['site_default_language'], Chris@0: '#title' => t('Set @title as default', ['@title' => $entity->label()]), Chris@0: '#title_display' => 'invisible', Chris@0: '#return_value' => $entity->id(), Chris@0: '#id' => 'edit-site-default-language-' . $entity->id(), Chris@0: ]; Chris@0: // Mark the right language as default in the form. Chris@0: if ($entity->id() == $this->languageManager->getDefaultLanguage()->getId()) { Chris@0: $row['default']['#default_value'] = $entity->id(); Chris@0: } Chris@0: return $row + parent::buildRow($entity); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state) { Chris@0: $form = parent::buildForm($form, $form_state); Chris@0: Chris@0: $form[$this->entitiesKey]['#languages'] = $this->entities; Chris@0: $form['actions']['submit']['#value'] = t('Save configuration'); Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateForm(array &$form, FormStateInterface $form_state) { Chris@0: if (!isset($this->entities[$form_state->getValue('site_default_language')])) { Chris@0: $form_state->setErrorByName('site_default_language', $this->t('Selected default language no longer exists.')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: parent::submitForm($form, $form_state); Chris@0: Chris@0: // Save the default language if changed. Chris@0: $new_id = $form_state->getValue('site_default_language'); Chris@0: if ($new_id != $this->languageManager->getDefaultLanguage()->getId()) { Chris@0: $this->configFactory->getEditable('system.site')->set('default_langcode', $new_id)->save(); Chris@0: $this->languageManager->reset(); Chris@0: } Chris@0: Chris@0: if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) { Chris@0: $this->languageManager->updateLockedLanguageWeights(); Chris@0: } Chris@0: Chris@17: $this->messenger->addStatus($this->t('Configuration saved.')); Chris@0: // Force the redirection to the page with the language we have just Chris@0: // selected as default. Chris@18: $form_state->setRedirectUrl($this->entities[$new_id]->toUrl('collection', ['language' => $this->entities[$new_id]])); Chris@0: } Chris@0: Chris@0: }