class SettingsForm in Habitat 8
Defines a form that configures habitat settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\habitat_ui\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
- habitat_ui.routing.yml in habitat_ui/
habitat_ui.routing.yml - habitat_ui/habitat_ui.routing.yml
File
- habitat_ui/
src/ Form/ SettingsForm.php, line 16 - Contains \Drupal\habitat_ui\Form\SettingsForm.
Namespace
Drupal\habitat_ui\FormView source
class SettingsForm extends ConfigFormBase {
/**
* An array of configuration names that should be editable.
*
* @var array
*/
protected $editableConfig = [];
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'habitat_admin_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return $this->editableConfig;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('habitat.settings');
$habitats = $config
->get('habitat_habitats');
$form['variable'] = array(
'#type' => 'textfield',
'#title' => t('Habitat variable name'),
'#required' => TRUE,
'#default_value' => $config
->get('habitat_variable'),
'#description' => t('The habitat variable used in your settings.php files to indicate the habitat. This should be placed into settings.php like $settings[\'!variable\'] = \'dev\'. Defaults to \'fetcher_environment\' which is added to settings.php when sites are built with the !fetcher system.', array(
'!variable' => $config
->get('habitat_variable'),
'!fetcher' => '<a href="http://drupal.org/project/fetcher">fetcher</a>',
)),
);
$form['habitats'] = array(
'#type' => 'textarea',
'#title' => t('Habitats'),
'#description' => t('The habitats to manage. Use machine_name conventions and enter one per line.'),
'#required' => TRUE,
'#default_value' => implode("\n", $habitats),
);
foreach ($habitats as $habitat) {
$form['install_' . $habitat] = array(
'#type' => 'textarea',
'#title' => t('%habitat installed modules', array(
'%habitat' => $habitat,
)),
'#description' => t('The modules to force install in this habitat. Use machine_name conventions and enter one per line.'),
'#default_value' => implode("\n", $config
->get('habitat_install_' . $habitat)),
);
$form['uninstall_' . $habitat] = array(
'#type' => 'textarea',
'#title' => t('%habitat uninstalled modules', array(
'%habitat' => $habitat,
)),
'#description' => t('The modules to force uninstall in this habitat. Use machine_name conventions and enter one per line.'),
'#default_value' => implode("\n", $config
->get('habitat_uninstall_' . $habitat)),
);
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()
->getEditable('habitat.settings');
$config
->set('habitat_variable', $form_state
->getValue('variable'));
$habitats = array_filter(array_map('trim', explode("\n", $form_state
->getValue('habitats'))));
$config
->set('habitat_habitats', $habitats);
$original_habitats = $config
->get('habitat_habitats');
foreach ($original_habitats as $original_habitat) {
if (in_array($original_habitat, $habitats)) {
$config
->set('habitat_install_' . $original_habitat, array_filter(array_map('trim', explode("\n", $form_state
->getValue('install_' . $original_habitat)))));
$config
->set('habitat_uninstall_' . $original_habitat, array_filter(array_map('trim', explode("\n", $form_state
->getValue('uninstall_' . $original_habitat)))));
}
else {
$config
->clear('habitat_install_' . $original_habitat);
$config
->clear('habitat_uninstall_' . $original_habitat);
}
}
$config
->save();
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('habitat.settings');
$habitats = $config
->get('habitat_habitats');
$module_data = system_rebuild_module_data();
foreach ($habitats as $habitat) {
foreach (array(
'install',
'uninstall',
) as $type) {
$modules = array_filter(array_map('trim', explode("\n", $form_state
->getValue($type . '_' . $habitat))));
$module_list = $modules ? array_combine($modules, $modules) : array();
if ($missing_modules = array_diff_key($module_list, $module_data)) {
$form_state
->setErrorByName($type . '_' . $habitat, t('Cannot set @habitat @typeed modules due to missing modules @modules.', array(
'@habitat' => $habitat,
'@type' => $type,
'@modules' => implode(', ', $missing_modules),
)));
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
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. Overrides UrlGeneratorTrait:: |
|
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. | |
FormInterface:: |
public | function | Returns a unique string identifying the form. | 236 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
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. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
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. | |
SettingsForm:: |
protected | property | An array of configuration names that should be editable. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function | ||
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |