class DbtngExampleUpdateForm in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/dbtng_example/src/Form/DbtngExampleUpdateForm.php \Drupal\dbtng_example\Form\DbtngExampleUpdateForm
Sample UI to update a record.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\dbtng_example\Form\DbtngExampleUpdateForm
Expanded class hierarchy of DbtngExampleUpdateForm
Related topics
1 string reference to 'DbtngExampleUpdateForm'
- dbtng_example.routing.yml in dbtng_example/
dbtng_example.routing.yml - dbtng_example/dbtng_example.routing.yml
File
- dbtng_example/
src/ Form/ DbtngExampleUpdateForm.php, line 15
Namespace
Drupal\dbtng_example\FormView source
class DbtngExampleUpdateForm extends FormBase {
/**
* Our database repository service.
*
* @var \Drupal\dbtng_example\DbtngExampleRepository
*/
protected $repository;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'dbtng_update_form';
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$form = new static($container
->get('dbtng_example.repository'));
$form
->setStringTranslation($container
->get('string_translation'));
$form
->setMessenger($container
->get('messenger'));
return $form;
}
/**
* Construct the new form object.
*/
public function __construct(DbtngExampleRepository $repository) {
$this->repository = $repository;
}
/**
* Sample UI to update a record.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Wrap the form in a div.
$form = [
'#prefix' => '<div id="updateform">',
'#suffix' => '</div>',
];
// Add some explanatory text to the form.
$form['message'] = [
'#markup' => $this
->t('Demonstrates a database update operation.'),
];
// Query for items to display.
$entries = $this->repository
->load();
// Tell the user if there is nothing to display.
if (empty($entries)) {
$form['no_values'] = [
'#value' => $this
->t('No entries exist in the table dbtng_example table.'),
];
return $form;
}
$keyed_entries = [];
$options = [];
foreach ($entries as $entry) {
$options[$entry->pid] = $this
->t('@pid: @name @surname (@age)', [
'@pid' => $entry->pid,
'@name' => $entry->name,
'@surname' => $entry->surname,
'@age' => $entry->age,
]);
$keyed_entries[$entry->pid] = $entry;
}
// Grab the pid.
$pid = $form_state
->getValue('pid');
// Use the pid to set the default entry for updating.
$default_entry = !empty($pid) ? $keyed_entries[$pid] : $entries[0];
// Save the entries into the $form_state. We do this so the AJAX callback
// doesn't need to repeat the query.
$form_state
->setValue('entries', $keyed_entries);
$form['pid'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Choose entry to update'),
'#default_value' => $default_entry->pid,
'#ajax' => [
'wrapper' => 'updateform',
'callback' => [
$this,
'updateCallback',
],
],
];
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Updated first name'),
'#size' => 15,
'#default_value' => $default_entry->name,
];
$form['surname'] = [
'#type' => 'textfield',
'#title' => $this
->t('Updated last name'),
'#size' => 15,
'#default_value' => $default_entry->surname,
];
$form['age'] = [
'#type' => 'textfield',
'#title' => $this
->t('Updated age'),
'#size' => 4,
'#default_value' => $default_entry->age,
'#description' => $this
->t('Values greater than 127 will cause an exception'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Update'),
];
return $form;
}
/**
* AJAX callback handler for the pid select.
*
* When the pid changes, populates the defaults from the database in the form.
*/
public function updateCallback(array $form, FormStateInterface $form_state) {
// Gather the DB results from $form_state.
$entries = $form_state
->getValue('entries');
// Use the specific entry for this $form_state.
$entry = $entries[$form_state
->getValue('pid')];
// Setting the #value of items is the only way I was able to figure out
// to get replaced defaults on these items. #default_value will not do it
// and shouldn't.
foreach ([
'name',
'surname',
'age',
] as $item) {
$form[$item]['#value'] = $entry->{$item};
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Confirm that age is numeric.
if (!intval($form_state
->getValue('age'))) {
$form_state
->setErrorByName('age', $this
->t('Age needs to be a number'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Gather the current user so the new record has ownership.
$account = $this
->currentUser();
// Save the submitted entry.
$entry = [
'pid' => $form_state
->getValue('pid'),
'name' => $form_state
->getValue('name'),
'surname' => $form_state
->getValue('surname'),
'age' => $form_state
->getValue('age'),
'uid' => $account
->id(),
];
$count = $this->repository
->update($entry);
$this
->messenger()
->addMessage($this
->t('Updated entry @entry (@count row updated)', [
'@count' => $count,
'@entry' => print_r($entry, TRUE),
]));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DbtngExampleUpdateForm:: |
protected | property | Our database repository service. | |
DbtngExampleUpdateForm:: |
public | function |
Sample UI to update a record. Overrides FormInterface:: |
|
DbtngExampleUpdateForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
DbtngExampleUpdateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
DbtngExampleUpdateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
DbtngExampleUpdateForm:: |
public | function | AJAX callback handler for the pid select. | |
DbtngExampleUpdateForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DbtngExampleUpdateForm:: |
public | function | Construct the new form 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 | Retrieves a configuration object. | |
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. | |
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. | |
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. |