Пример вывода текущей даты в textfield:
<?php
namespace Drupal\example_module\Form;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
class ExampleForm extends FormBase implements TrustedCallbackInterface {
/**
* {@inheritDoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
/** @see \Drupal\Core\Form\FormBuilder::prepareForm:764 */
$date_value_placeholder = 'form_date_placeholder_' . Crypt::hashBase64($this->getFormId());
$form['date'] = [
'#type' => 'textfield',
'#title' => 'Date',
'#default_value' => $date_value_placeholder,
'#attached' => [
'placeholders' => [
$date_value_placeholder => [
'#lazy_builder' => [
[self::class, 'dateValueLazyBuilder'],
[],
],
],
],
],
];
return $form;
}
/**
* {@inheritDoc}
*/
public static function trustedCallbacks(): array {
return [
'dateValueLazyBuilder',
];
}
/**
* Lazy builder callback.
*/
public static function dateValueLazyBuilder(): array {
return [
'#markup' => date('r'),
];
}
}
Написанное актуально для
Drupal 8+
Добавить комментарий