FormTestServiceObject.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php
  2. 8.9.x core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php
  3. 11.x core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php

Namespace

Drupal\form_test

File

core/modules/system/tests/modules/form_test/src/FormTestServiceObject.php

View source
<?php

namespace Drupal\form_test;

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

/**
 * Provides a test form object.
 *
 * @internal
 */
class FormTestServiceObject extends ConfigFormBase {
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'form_test_form_test_service_object';
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getEditableConfigNames() {
        return [
            'form_test.object',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $form['element'] = [
            '#markup' => 'The FormTestServiceObject::buildForm() method was used for this form.',
        ];
        $form['bananas'] = [
            '#type' => 'textfield',
            '#default_value' => 'brown',
            '#title' => $this->t('Bananas'),
        ];
        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = [
            '#type' => 'submit',
            '#value' => $this->t('Save'),
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
        $this->messenger()
            ->addStatus($this->t('The FormTestServiceObject::validateForm() method was used for this form.'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $this->messenger()
            ->addStatus($this->t('The FormTestServiceObject::submitForm() method was used for this form.'));
        $this->config('form_test.object')
            ->set('bananas', $form_state->getValue('bananas'))
            ->save();
    }

}

Classes

Title Deprecated Summary
FormTestServiceObject Provides a test form object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.