class FarmSettingsFarmInfoForm in farmOS 2.x
Form for configuring basic farm info.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\farm_settings\Form\FarmSettingsFarmInfoForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of FarmSettingsFarmInfoForm
1 string reference to 'FarmSettingsFarmInfoForm'
- farm_settings.routing.yml in modules/
core/ settings/ farm_settings.routing.yml - modules/core/settings/farm_settings.routing.yml
File
- modules/
core/ settings/ src/ Form/ FarmSettingsFarmInfoForm.php, line 16
Namespace
Drupal\farm_settings\FormView source
class FarmSettingsFarmInfoForm extends ConfigFormBase {
/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;
/**
* Constructs a RegionalForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
* The country manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, CountryManagerInterface $country_manager) {
parent::__construct($config_factory);
$this->countryManager = $country_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('country_manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'farm_settings_farm_info';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'system.date',
'system.site',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Set the form title.
$form['#title'] = $this
->t('Configure Farm Info');
// Get the system.site config.
$site = $this
->config('system.site');
// Textfield to edit site name.
$form['site_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Farm name'),
'#default_value' => $site
->get('name'),
'#maxlength' => 128,
'#required' => TRUE,
];
// Get the system.date config.
$system_date = $this
->config('system.date');
// Get countries.
$countries = $this->countryManager
->getList();
// Default country select.
$form['default_country'] = [
'#type' => 'select',
'#title' => $this
->t('Default country'),
'#empty_value' => '',
'#default_value' => $system_date
->get('country.default'),
'#options' => $countries,
'#attributes' => [
'class' => [
'country-detect',
],
],
];
// Get list of timezones.
$timezones = system_time_zones();
// Dropdown to select default timezone.
$form['default_timezone'] = [
'#type' => 'select',
'#title' => $this
->t('Default timezone'),
'#description' => $this
->t('The default timezone of the farmOS server. Note that users can configure individual timezones later.'),
'#options' => $timezones,
'#default_value' => $system_date
->get('timezone.default'),
'#required' => TRUE,
];
// Submit button.
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the submitted site name.
$site_name = $form_state
->getvalue('site_name');
// Update system.site config.
$this->configFactory
->getEditable('system.site')
->set('name', $site_name)
->save();
// Get the submitted country.
$default_country = $form_state
->getValue('default_country');
// Get the submitted timezone.
$default_timezone = $form_state
->getValue('default_timezone');
// Update system.date config.
$this->configFactory
->getEditable('system.date')
->set('timezone.default', $default_timezone)
->set('country.default', $default_country)
->save();
// Display message from parent submitForm.
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FarmSettingsFarmInfoForm:: |
protected | property | The country manager. | |
FarmSettingsFarmInfoForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
FarmSettingsFarmInfoForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
FarmSettingsFarmInfoForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
FarmSettingsFarmInfoForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FarmSettingsFarmInfoForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
FarmSettingsFarmInfoForm:: |
public | function |
Constructs a RegionalForm object. Overrides ConfigFormBase:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |