Newer
Older
<?php
/**
* @file
* Contains \Drupal\Language\Form\NegotiationConfigureForm.
*/
namespace Drupal\language\Form;
Dries Buytaert
committed
use Drupal\block\BlockManagerInterface;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\Xss;
Alex Pott
committed
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\LanguageNegotiatorInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure the selected language negotiation method for this site.
*/
class NegotiationConfigureForm extends FormBase {
/**
* Stores the configuration object for language.types.
*
* @var \Drupal\Core\Config\Config
*/
protected $languageTypes;
* The language manager.
* @var \Drupal\language\ConfigurableLanguageManagerInterface
protected $languageManager;
* The language negotiator.
* @var \Drupal\language\LanguageNegotiatorInterface
protected $negotiator;
/**
* The block manager.
*
Dries Buytaert
committed
* @var \Drupal\block\BlockManagerInterface
*/
protected $blockManager;
/**
* Constructs a NegotiationConfigureForm object.
*
Alex Pott
committed
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\language\LanguageNegotiatorInterface $negotiator
* The language negotiation methods manager.
Dries Buytaert
committed
* @param \Drupal\block\BlockManagerInterface $block_manager
* The block manager, or NULL if not available.
*/
Dries Buytaert
committed
public function __construct(ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, BlockManagerInterface $block_manager = NULL) {
$this->languageTypes = $config_factory->get('language.types');
$this->languageManager = $language_manager;
$this->negotiator = $negotiator;
$this->blockManager = $block_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('language_manager'),
$container->get('language_negotiator'),
$container->has('plugin.manager.block') ? $container->get('plugin.manager.block') : NULL
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'language_negotiation_configure_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
$configurable = $this->languageTypes->get('configurable');
$form = array(
'#theme' => 'language_negotiation_configure_form',
'#language_types_info' => $this->languageManager->getDefinedLanguageTypesInfo(),
'#language_negotiation_info' => $this->negotiator->getNegotiationMethods(),
);
$form['#language_types'] = array();
foreach ($form['#language_types_info'] as $type => $info) {
// Show locked language types only if they are configurable.
if (empty($info['locked']) || in_array($type, $configurable)) {
$form['#language_types'][] = $type;
}
}
foreach ($form['#language_types'] as $type) {
$this->configureFormTable($form, $type);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
Alex Pott
committed
'#button_type' => 'primary',
'#value' => $this->t('Save settings'),
);
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
$configurable_types = $form['#language_types'];
$stored_values = $this->languageTypes->get('configurable');
$customized = array();
$method_weights_type = array();
foreach ($configurable_types as $type) {
$customized[$type] = in_array($type, $stored_values);
$method_weights = array();
Alex Pott
committed
$enabled_methods = $form_state->getValue(array($type, 'enabled'));
$enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE;
Alex Pott
committed
$method_weights_input = $form_state->getValue(array($type, 'weight'));
if ($form_state->hasValue(array($type, 'configurable'))) {
$customized[$type] = !$form_state->isValueEmpty(array($type, 'configurable'));
}
foreach ($method_weights_input as $method_id => $weight) {
if ($enabled_methods[$method_id]) {
$method_weights[$method_id] = $weight;
}
}
$method_weights_type[$type] = $method_weights;
$this->config('language.types')->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save();
}
// Update non-configurable language types and the related language
// negotiation configuration.
$this->negotiator->updateConfiguration(array_keys(array_filter($customized)));
// Update the language negotiations after setting the configurability.
foreach ($method_weights_type as $type => $method_weights) {
$this->negotiator->saveConfiguration($type, $method_weights);
}
// Clear block definitions cache since the available blocks and their names
// may have been changed based on the configurable types.
if ($this->blockManager) {
// If there is an active language switcher for a language type that has
// been made not configurable, deactivate it first.
$non_configurable = array_keys(array_diff($customized, array_filter($customized)));
$this->disableLanguageSwitcher($non_configurable);
$this->blockManager->clearCachedDefinitions();
}
Alex Pott
committed
$form_state->setRedirect('language.negotiation');
drupal_set_message($this->t('Language negotiation configuration saved.'));
}
/**
* Builds a language negotiation method configuration table.
*
* @param array $form
* The language negotiation configuration form.
* @param string $type
* The language type to generate the table for.
*/
protected function configureFormTable(array &$form, $type) {
$info = $form['#language_types_info'][$type];
$table_form = array(
'#title' => $this->t('@type language detection', array('@type' => $info['name'])),
'#tree' => TRUE,
'#description' => $info['description'],
'#language_negotiation_info' => array(),
'#show_operations' => FALSE,
'weight' => array('#tree' => TRUE),
);
// Only show configurability checkbox for the unlocked language types.
if (empty($info['locked'])) {
$configurable = $this->languageTypes->get('configurable');
$table_form['configurable'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Customize %language_name language detection to differ from User interface text language detection settings.', array('%language_name' => $info['name'])),
'#default_value' => in_array($type, $configurable),
'#attributes' => array('class' => array('language-customization-checkbox')),
'#attached' => array(
'library' => array(
Alex Pott
committed
'language/drupal.language.admin'
),
),
);
}
$negotiation_info = $form['#language_negotiation_info'];
$enabled_methods = $this->config('language.types')->get('negotiation.' . $type . '.enabled') ?: array();
$methods_weight = $this->config('language.types')->get('negotiation.' . $type . '.method_weights') ?: array();
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Add missing data to the methods lists.
foreach ($negotiation_info as $method_id => $method) {
if (!isset($methods_weight[$method_id])) {
$methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
}
}
// Order methods list by weight.
asort($methods_weight);
foreach ($methods_weight as $method_id => $weight) {
// A language method might be no more available if the defining module has
// been disabled after the last configuration saving.
if (!isset($negotiation_info[$method_id])) {
continue;
}
$enabled = isset($enabled_methods[$method_id]);
$method = $negotiation_info[$method_id];
// List the method only if the current type is defined in its 'types' key.
// If it is not defined default to all the configurable language types.
$types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);
if (isset($types[$type])) {
$table_form['#language_negotiation_info'][$method_id] = $method;
$method_name = String::checkPlain($method['name']);
$table_form['weight'][$method_id] = array(
'#type' => 'weight',
'#title' => $this->t('Weight for !title language detection method', array('!title' => Unicode::strtolower($method_name))),
'#title_display' => 'invisible',
'#default_value' => $weight,
'#attributes' => array('class' => array("language-method-weight-$type")),
'#delta' => 20,
);
$table_form['title'][$method_id] = array('#markup' => $method_name);
$table_form['enabled'][$method_id] = array(
'#type' => 'checkbox',
'#title' => $this->t('Enable !title language detection method', array('!title' => Unicode::strtolower($method_name))),
'#title_display' => 'invisible',
'#default_value' => $enabled,
);
if ($method_id === LanguageNegotiationSelected::METHOD_ID) {
$table_form['enabled'][$method_id]['#default_value'] = TRUE;
$table_form['enabled'][$method_id]['#attributes'] = array('disabled' => 'disabled');
}
$table_form['description'][$method_id] = array('#markup' => Xss::filterAdmin($method['description']));
$config_op = array();
if (isset($method['config_path'])) {
$config_op['configure'] = array(
'title' => $this->t('Configure'),
'href' => $method['config_path'],
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
);
// If there is at least one operation enabled show the operation
// column.
$table_form['#show_operations'] = TRUE;
}
$table_form['operation'][$method_id] = array(
'#type' => 'operations',
'#links' => $config_op,
);
}
}
$form[$type] = $table_form;
}
/**
* Disables the language switcher blocks.
*
* @param array $language_types
* An array containing all language types whose language switchers need to
* be disabled.
*/
protected function disableLanguageSwitcher(array $language_types) {
$blocks = _block_rehash();
foreach ($language_types as $language_type) {
foreach ($blocks as $block) {
if (strpos($block->id, 'language_switcher_' . substr($language_type, 9)) !== FALSE) {
$block->delete();
}
}
}
}
}