devel_wizard-2.x-dev/templates/spell/config_object/form.php.twig

templates/spell/config_object/form.php.twig
{%
  include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
  with {
    'namespace': form.namespace,
  }
%}

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\ConfigTarget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

class {{ form.class }} extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return '{{ form.id }}';
  }

  /**
   * {@inheritdoc}
   *
   * @phpstan-return array{{ '<' }}string>
   */
  protected function getEditableConfigNames(): array {
    return [
      '{{ configSchema.id }}',
    ];
  }

  /**
   * {@inheritdoc}
   *
   * @phpstan-param array{{ '<' }}string, mixed> $form
   *
   * @phpstan-return array{{ '<' }}string, mixed>
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $configName = $this->getEditableConfigNames()[0];

    $form['foo'] = [
      '#config_target' => new ConfigTarget($configName, 'foo'),
      '#type' => 'textfield',
      '#title' => $this->t('Foo'),
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * @phpstan-param array{{ '<' }}string, mixed> $form
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    parent::submitForm($form, $form_state);

    $configName = $this->getEditableConfigNames()[0];
    $configExportUrl = Url::fromRoute(
      'config.export_single',
      [
        'config_type' => 'system.simple',
        'config_name' => $configName,
      ],
    );

    if ($configExportUrl->access()) {
      $this
        ->messenger()
        ->addStatus($this->t(
          'The saved configuration can be checked <a href=":uri_to_config_export">here</a>.',
          [
            ':uri_to_config_export' => $configExportUrl->toString(),
          ],
        ));
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc