Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\language\Form\ContentLanguageSettingsForm.
*/
namespace Drupal\language\Form;
Alex Pott
committed
use Drupal\Core\Config\ConfigFactoryInterface;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
Angie Byron
committed
use Drupal\Core\Form\ConfigFormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure the content language settings for this site.
*/
Angie Byron
committed
class ContentLanguageSettingsForm extends ConfigFormBase {
Angie Byron
committed
/**
* The entity manager.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
Angie Byron
committed
*/
protected $entityManager;
/**
* Constructs a ContentLanguageSettingsForm object.
*
Alex Pott
committed
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Angie Byron
committed
* The config factory.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Angie Byron
committed
* The entity manager.
*/
Alex Pott
committed
public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager) {
parent::__construct($config_factory);
Angie Byron
committed
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity.manager')
Angie Byron
committed
);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Angie Byron
committed
return 'language_content_settings_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
Alex Pott
committed
$entity_types = $this->entityManager->getDefinitions();
Angie Byron
committed
$labels = array();
$default = array();
$bundles = $this->entityManager->getAllBundleInfo();
Angie Byron
committed
$language_configuration = array();
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type->isTranslatable()) {
continue;
}
$labels[$entity_type_id] = $entity_type->getLabel() ?: $entity_type_id;
Alex Pott
committed
$default[$entity_type_id] = FALSE;
Angie Byron
committed
// Check whether we have any custom setting.
foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) {
Alex Pott
committed
$conf = language_get_default_configuration($entity_type_id, $bundle);
Angie Byron
committed
if (!empty($conf['language_show']) || $conf['langcode'] != 'site_default') {
Alex Pott
committed
$default[$entity_type_id] = $entity_type_id;
Angie Byron
committed
}
Alex Pott
committed
$language_configuration[$entity_type_id][$bundle] = $conf;
Angie Byron
committed
}
}
asort($labels);
$form = array(
'#labels' => $labels,
'#attached' => array(
'library' => array(
'language/drupal.language.admin',
Angie Byron
committed
),
),
);
$form['entity_types'] = array(
'#title' => $this->t('Custom language settings'),
'#type' => 'checkboxes',
'#options' => $labels,
'#default_value' => $default,
);
$form['settings'] = array('#tree' => TRUE);
Alex Pott
committed
foreach ($labels as $entity_type_id => $label) {
$entity_type = $entity_types[$entity_type_id];
Angie Byron
committed
Alex Pott
committed
$form['settings'][$entity_type_id] = array(
Angie Byron
committed
'#title' => $label,
'#type' => 'container',
Alex Pott
committed
'#entity_type' => $entity_type_id,
Angie Byron
committed
'#theme' => 'language_content_settings_table',
Alex Pott
committed
'#bundle_label' => $entity_type->getBundleLabel() ?: $label,
Angie Byron
committed
'#states' => array(
'visible' => array(
Alex Pott
committed
':input[name="entity_types[' . $entity_type_id . ']"]' => array('checked' => TRUE),
Angie Byron
committed
),
),
);
foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) {
Alex Pott
committed
$form['settings'][$entity_type_id][$bundle]['settings'] = array(
Angie Byron
committed
'#type' => 'item',
'#label' => $bundle_info['label'],
'language' => array(
'#type' => 'language_configuration',
'#entity_information' => array(
Alex Pott
committed
'entity_type' => $entity_type_id,
Angie Byron
committed
'bundle' => $bundle,
),
Alex Pott
committed
'#default_value' => $language_configuration[$entity_type_id][$bundle],
Angie Byron
committed
),
);
}
}
$form = parent::buildForm($form, $form_state);
// @todo Remove this override. There are tests that check for explicitly for
// the button label which need to be adapted for that.
// https://drupal.org/node/2241727
$form['actions']['submit']['#value'] = $this->t('Save');
Angie Byron
committed
return $form;
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
$config = $this->config('language.settings');
Angie Byron
committed
foreach ($form_state['values']['settings'] as $entity_type => $entity_settings) {
foreach ($entity_settings as $bundle => $bundle_settings) {
$config->set(language_get_default_configuration_settings_key($entity_type, $bundle), array(
'langcode' => $bundle_settings['settings']['language']['langcode'],
'language_show' => $bundle_settings['settings']['language']['language_show'],
));
Angie Byron
committed
}
}
$config->save();
drupal_set_message($this->t('Settings successfully updated.'));