public function WeatherDisplayPlaceForm::buildForm in Weather 8
Same name and namespace in other branches
- 2.0.x src/Form/WeatherDisplayPlaceForm.php \Drupal\weather\Form\WeatherDisplayPlaceForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides EntityForm::buildForm
File
- src/
Form/ WeatherDisplayPlaceForm.php, line 68
Class
- WeatherDisplayPlaceForm
- Form controller for the weather_display_place entity edit forms.
Namespace
Drupal\weather\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $display_type = NULL, $display_number = NULL, WeatherDisplayPlaceInterface $weather_display_place = NULL) {
// Check if we have weather places to connect with displays.
$weatherPlacesExists = $this->weatherPlaceStorage
->getQuery()
->range(0, 1)
->execute();
if (empty($weatherPlacesExists)) {
$form['no_places_added'] = [
'#markup' => $this
->t('You do not have any weather places in system. Go to <a href="@url">settings page</a> and run weather places import', [
'@url' => Url::fromRoute('weather.settings')
->toString(),
]),
];
return $form;
}
if ($display_type == WeatherDisplayInterface::USER_TYPE) {
$display_number = $this
->currentUser()
->id();
}
$form = parent::buildForm($form, $form_state);
// If we are on edit form, display type and number not passed here.
if ($weather_display_place instanceof WeatherDisplayPlaceInterface) {
$display_type = $weather_display_place->display_type->value;
$display_number = $weather_display_place->display_number->value;
}
// If the place exists, get the configuration.
// If it does not exist - get the default place configuration.
$settings = $this
->getLocationSettings($weather_display_place);
if (!empty($form_state
->getValue('country'))) {
$settings['country'] = $form_state
->getValue('country');
}
$settings['places'] = $this
->getAvailablePlacesOptions($settings['country']);
$form['country'] = [
'#type' => 'select',
'#title' => $this
->t('Country'),
'#description' => $this
->t('Select a country to narrow down your search.'),
'#default_value' => $settings['country'],
'#options' => $this
->getAvailableCountriesOptions(),
'#ajax' => [
'callback' => '::countryAjaxCallback',
'wrapper' => 'weather_place_replace',
],
];
$form['geoid'] = [
'#type' => 'select',
'#title' => $this
->t('Place'),
'#description' => $this
->t('Select a place in that country for the weather display.'),
'#default_value' => $settings['geoid'],
'#options' => $settings['places'],
'#prefix' => '<div id="weather_place_replace">',
'#ajax' => [
'callback' => '::placeAjaxCallback',
'wrapper' => 'weather_displayed_name_replace',
],
];
$form['displayed_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Displayed name for the selected place'),
'#default_value' => $settings['displayed_name'],
'#description' => $this
->t('You may enter another name for the place selected above.'),
'#required' => TRUE,
'#size' => '30',
'#prefix' => '<div id="weather_displayed_name_replace">',
'#suffix' => '</div></div>',
];
$form['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#default_value' => $settings['weight'],
'#description' => $this
->t('Optional. In the block, the heavier locations will sink and the lighter locations will be positioned nearer the top. Locations with equal weights are sorted alphabetically.'),
];
$form['display_type'] = [
'#type' => 'value',
'#value' => $display_type,
];
$form['display_number'] = [
'#type' => 'value',
'#value' => $display_number,
];
// If the form is regenerated during an AJAX callback, get the
// country selected by the user.
if ($triggeredBy = $form_state
->getTriggeringElement()) {
$settings['country'] = $form_state
->getValue('country');
if ($triggeredBy["#name"] == 'country') {
$settings['places'] = $this
->getAvailablePlacesOptions($settings['country']);
$settings['geoid'] = key($settings['places']);
$settings['displayed_name'] = $settings['places'][$settings['geoid']];
$form['geoid']['#options'] = $settings['places'];
$form['geoid']['#value'] = $settings['geoid'];
$form['displayed_name']['#value'] = $settings['displayed_name'];
}
if ($triggeredBy["#name"] == 'geoid') {
$settings['displayed_name'] = $settings['places'][$form_state
->getValue('geoid')];
$form['displayed_name']['#value'] = $settings['displayed_name'];
}
}
// Use different path for delete form for non-admin user.
if ($display_type == WeatherDisplayInterface::USER_TYPE && $weather_display_place instanceof WeatherDisplayPlaceInterface) {
$form["actions"]["delete"]["#url"] = Url::fromRoute('weather.user.weather_display_place.delete_form', [
'user' => $display_number,
'weather_display_place' => $weather_display_place
->id(),
]);
}
return $form;
}