class AjaxColorForm in Examples for Developers 3.x
Same name and namespace in other branches
- 8 form_api_example/src/Form/AjaxColorForm.php \Drupal\form_api_example\Form\AjaxColorForm
Implements the ajax demo form controller.
This example demonstrates using ajax callbacks to populate the options of a color select element dynamically based on the value selected in another select element in the form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\form_api_example\Form\DemoBase
- class \Drupal\form_api_example\Form\AjaxColorForm
- class \Drupal\form_api_example\Form\DemoBase
Expanded class hierarchy of AjaxColorForm
See also
\Drupal\Core\Form\ConfigFormBase
1 string reference to 'AjaxColorForm'
- form_api_example.routing.yml in modules/
form_api_example/ form_api_example.routing.yml - modules/form_api_example/form_api_example.routing.yml
File
- modules/
form_api_example/ src/ Form/ AjaxColorForm.php, line 17
Namespace
Drupal\form_api_example\FormView source
class AjaxColorForm extends DemoBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_api_example_ajax_color_demo';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#type' => 'item',
'#markup' => $this
->t('This form example demonstrates functioning of an AJAX callback.'),
];
// The #ajax attribute used in the temperature input element defines an ajax
// callback that will invoke the 'updateColor' method on this form object.
// Whenever the temperature element changes, it will invoke this callback
// and replace the contents of the 'color_wrapper' container with the
// results of this method call.
$form['temperature'] = [
'#title' => $this
->t('Temperature'),
'#type' => 'select',
'#options' => $this
->getColorTemperatures(),
'#empty_option' => $this
->t('- Select a color temperature -'),
'#ajax' => [
// Could also use [get_class($this), 'updateColor'].
'callback' => '::updateColor',
'wrapper' => 'color-wrapper',
],
];
// Add a wrapper that can be replaced with new HTML by the ajax callback.
// This is given the ID that was passed to the ajax callback in the '#ajax'
// element above.
$form['color_wrapper'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'color-wrapper',
],
];
// Add a color element to the color_wrapper container using the value
// from temperature to determine which colors to include in the select
// element.
$temperature = $form_state
->getValue('temperature');
if (!empty($temperature)) {
$form['color_wrapper']['color'] = [
'#type' => 'select',
'#title' => $this
->t('Color'),
'#options' => $this
->getColorsByTemperature($temperature),
];
}
// Add a submit button that handles the submission of the form.
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
],
];
return $form;
}
/**
* Ajax callback for the color dropdown.
*/
public function updateColor(array $form, FormStateInterface $form_state) {
return $form['color_wrapper'];
}
/**
* Returns colors that correspond with the given temperature.
*
* @param string $temperature
* The color temperature for which to return a list of colors. Can be either
* 'warm' or 'cool'.
*
* @return array
* An associative array of colors that correspond to the given color
* temperature, suitable to use as form options.
*/
protected function getColorsByTemperature($temperature) {
return $this
->getColors()[$temperature]['colors'];
}
/**
* Returns a list of color temperatures.
*
* @return array
* An associative array of color temperatures, suitable to use as form
* options.
*/
protected function getColorTemperatures() {
return array_map(function ($color_data) {
return $color_data['name'];
}, $this
->getColors());
}
/**
* Returns an array of colors grouped by color temperature.
*
* @return array
* An associative array of color data, keyed by color temperature.
*/
protected function getColors() {
return [
'warm' => [
'name' => $this
->t('Warm'),
'colors' => [
'red' => $this
->t('Red'),
'orange' => $this
->t('Orange'),
'yellow' => $this
->t('Yellow'),
],
],
'cool' => [
'name' => $this
->t('Cool'),
'colors' => [
'blue' => $this
->t('Blue'),
'purple' => $this
->t('Purple'),
'green' => $this
->t('Green'),
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxColorForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
AjaxColorForm:: |
protected | function | Returns an array of colors grouped by color temperature. | |
AjaxColorForm:: |
protected | function | Returns colors that correspond with the given temperature. | |
AjaxColorForm:: |
protected | function | Returns a list of color temperatures. | |
AjaxColorForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AjaxColorForm:: |
public | function | Ajax callback for the color dropdown. | |
DemoBase:: |
public | function |
Implements a form submit handler. Overrides FormInterface:: |
2 |
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 | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
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. |