View source
<?php
namespace Drupal\ingredient\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\language\Entity\ContentLanguageSettings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class IngredientSettingsForm extends ConfigFormBase {
protected $moduleHandler;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'));
}
public function getFormId() {
return 'ingredient_admin_settings';
}
protected function getEditableConfigNames() {
return [
'ingredient.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('ingredient.settings');
$form['ingredient_name_normalize'] = [
'#type' => 'radios',
'#title' => $this
->t('Ingredient name normalization'),
'#default_value' => $config
->get('ingredient_name_normalize'),
'#options' => [
$this
->t('Leave as entered'),
$this
->t('Convert to lowercase'),
],
'#description' => $this
->t('If enabled, the names of <em>new</em> ingredients will be converted to lowercase when they are entered. The names of registered trademarks, any ingredient name containing the ® symbol, will be excluded from normalization.'),
'#required' => TRUE,
];
if ($this->moduleHandler
->moduleExists('language')) {
$form['default_ingredient_language'] = [
'#type' => 'details',
'#title' => $this
->t('Ingredients language'),
'#open' => TRUE,
];
$form['default_ingredient_language']['default_language'] = [
'#type' => 'language_configuration',
'#entity_information' => [
'entity_type' => 'ingredient',
'bundle' => 'ingredient',
],
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('ingredient', 'ingredient'),
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this
->config('ingredient.settings')
->set('ingredient_name_normalize', $values['ingredient_name_normalize'])
->save();
parent::submitForm($form, $form_state);
}
}