class SettingsForm in Weather 2.0.x
Same name and namespace in other branches
- 8 src/Form/SettingsForm.php \Drupal\weather\Form\SettingsForm
Configure Weather settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\weather\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
File
- src/
Form/ SettingsForm.php, line 18
Namespace
Drupal\weather\FormView source
class SettingsForm extends ConfigFormBase {
/**
* Entity Type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Weather displays storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $weatherDisplayStorage;
/**
* Weather display places storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $weatherDisplayPlaceStorage;
/**
* Weather Data service.
*
* @var \Drupal\weather\Service\DataService
*/
protected $weatherDataService;
/**
* Constructs a \Drupal\weather\Form\SettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager storage.
* @param \Drupal\weather\Service\DataService $weatherDataService
* Weather data service.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entityTypeManager, DataService $weatherDataService) {
parent::__construct($config_factory);
$this->entityTypeManager = $entityTypeManager;
$this->weatherDisplayStorage = $entityTypeManager
->getStorage('weather_display');
$this->weatherDisplayPlaceStorage = $entityTypeManager
->getStorage('weather_display_place');
$this->weatherDataService = $weatherDataService;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('weather.data_service'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'weather_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'weather.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$this
->addWeatherDisplayOverview($form, $form_state);
// Import/Reimport places.
$form['import_places'] = [
'#type' => 'details',
'#title' => $this
->t('Import/Reimport places'),
];
$form['import_places']['description'] = [
'#type' => 'markup',
'#prefix' => '<span>',
'#markup' => $this
->t('After the installation, you need to import weather places into the system.'),
'#suffix' => '</span>',
];
$form['import_places']['import_places_submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Import places'),
];
// Additional weather settings.
$theme = $this
->config('system.theme')
->get('default');
$theme_path = drupal_get_path('theme', $theme);
$config = $this
->config('weather.settings');
$form['weather_image_directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('Directory for custom images'),
'#description' => $this
->t('Use custom images for displaying weather conditions. The name of this directory can be chosen freely. It will be searched in your active theme (currently %theme_path).', [
'%theme_path' => $theme_path,
]),
'#default_value' => $config
->get('weather_image_directory', ''),
];
$options = [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
];
$form['weather_forecast_days'] = [
'#type' => 'select',
'#title' => $this
->t('Number of forecast days'),
'#description' => $this
->t('You can configure the number of days for the forecast displays in blocks.'),
'#default_value' => $config
->get('weather_forecast_days', '2'),
'#options' => array_combine($options, $options),
];
return parent::buildForm($form, $form_state);
}
/**
* Builds system wide weather displays overview.
*/
protected function addWeatherDisplayOverview(array &$form, FormStateInterface $form_state) {
$displays = $this->weatherDisplayStorage
->loadByProperties([
'type' => WeatherDisplayInterface::SYSTEM_WIDE_TYPE,
]);
foreach ($displays as $display) {
$display_number = $display->number->value;
$form['system_displays'][$display_number] = [
'#type' => 'table',
'#header' => [
Link::fromTextAndUrl($this
->t('System-wide display (#@number)', [
'@number' => $display_number,
]), Url::fromRoute('entity.weather_display.edit_form', [
'display_number' => $display_number,
])),
$this
->t('Weight'),
],
];
$locations = $this->weatherDisplayPlaceStorage
->getQuery()
->condition('display_type', WeatherDisplayInterface::SYSTEM_WIDE_TYPE)
->condition('display_number', $display_number)
->sort('weight', 'ASC')
->sort('displayed_name', 'ASC')
->execute();
foreach ($locations as $locationId) {
$location = $this->weatherDisplayPlaceStorage
->load($locationId);
$form['system_displays'][$display_number]['#rows'][] = [
Link::fromTextAndUrl($location->displayed_name->value, Url::fromRoute('entity.weather_display_place.edit_form', [
'display_type' => $location->display_type->value,
'display_number' => $display_number,
'weather_display_place' => $locationId,
])),
$location->weight->value,
];
}
// Insert link for adding locations into the table as last row.
$form['system_displays'][$display_number]['#rows'][] = [
'link' => Link::fromTextAndUrl($this
->t('Add location to this display'), Url::fromRoute('entity.weather_display_place.add_form', [
'display_type' => WeatherDisplayInterface::SYSTEM_WIDE_TYPE,
'display_number' => $display_number,
])),
'#wrapper_attributes' => [
'colspan' => 2,
],
];
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$triggeredBy = $form_state
->getTriggeringElement();
if ($triggeredBy && $triggeredBy['#id'] == 'edit-import-places-submit') {
$this->weatherDataService
->weatherDataInstallation();
}
else {
$this
->config('weather.settings')
->set('weather_image_directory', $form_state
->getValue('weather_image_directory'))
->set('weather_forecast_days', $form_state
->getValue('weather_forecast_days'))
->save();
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 | |
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. | |
SettingsForm:: |
protected | property | Entity Type manager service. | |
SettingsForm:: |
protected | property | Weather Data service. | |
SettingsForm:: |
protected | property | Weather display places storage. | |
SettingsForm:: |
protected | property | Weather displays storage. | |
SettingsForm:: |
protected | function | Builds system wide weather displays overview. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Constructs a \Drupal\weather\Form\SettingsForm object. Overrides ConfigFormBase:: |
|
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. |