Commit 2db05b4c authored by webchick's avatar webchick

Issue #2071985 by tim.plunkett: Modernize form_test().module forms.

parent 94f609ea
......@@ -13,14 +13,14 @@
class ConfirmFormArrayPathTestForm extends ConfirmFormTestForm {
/**
* Overrides \Drupal\form_test\ConfirmFormTestForm::getFormID().
* {@inheritdoc}
*/
public function getFormID() {
return 'form_test_confirm_array_path_test_form';
}
/**
* Overrides \Drupal\form_test\ConfirmFormTestForm::getCancelPath().
* {@inheritdoc}
*/
public function getCancelPath() {
return array(
......@@ -32,10 +32,10 @@ public function getCancelPath() {
}
/**
* Overrides \Drupal\form_test\ConfirmFormTestForm::getCancelText().
* {@inheritdoc}
*/
public function getCancelText() {
return t('ConfirmFormArrayPathTestForm::getCancelText().');
return $this->t('ConfirmFormArrayPathTestForm::getCancelText().');
}
}
......@@ -25,7 +25,7 @@ public function getFormID() {
* {@inheritdoc}
*/
public function getQuestion() {
return t('ConfirmFormTestForm::getQuestion().');
return $this->t('ConfirmFormTestForm::getQuestion().');
}
/**
......@@ -39,21 +39,21 @@ public function getCancelPath() {
* {@inheritdoc}
*/
public function getDescription() {
return t('ConfirmFormTestForm::getDescription().');
return $this->t('ConfirmFormTestForm::getDescription().');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('ConfirmFormTestForm::getConfirmText().');
return $this->t('ConfirmFormTestForm::getConfirmText().');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return t('ConfirmFormTestForm::getCancelText().');
return $this->t('ConfirmFormTestForm::getCancelText().');
}
/**
......@@ -69,7 +69,7 @@ public function buildForm(array $form, array &$form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The ConfirmFormTestForm::submitForm() method was used for this form.'));
drupal_set_message($this->t('The ConfirmFormTestForm::submitForm() method was used for this form.'));
$form_state['redirect'] = '';
}
......
......@@ -7,52 +7,53 @@
namespace Drupal\form_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Component\Utility\String;
use Drupal\Core\Form\FormBase;
/**
* Provides a test form object that needs arguments.
*/
class FormTestArgumentsObject implements FormInterface {
class FormTestArgumentsObject extends FormBase {
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
* {@inheritdoc}
*/
public function getFormID() {
return 'form_test_form_test_arguments_object';
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $arg = NULL) {
$form['element'] = array('#markup' => 'The FormTestArgumentsObject::buildForm() method was used for this form.');
$form['bananas'] = array(
'#type' => 'textfield',
'#default_value' => check_plain($arg),
'#title' => t('Bananas'),
'#default_value' => String::checkPlain($arg),
'#title' => $this->t('Bananas'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
);
return $form;
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestArgumentsObject::validateForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestArgumentsObject::validateForm() method was used for this form.'));
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestArgumentsObject::submitForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestArgumentsObject::submitForm() method was used for this form.'));
\Drupal::config('form_test.object')
->set('bananas', $form_state['values']['bananas'])
->save();
......
......@@ -7,12 +7,12 @@
namespace Drupal\form_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormBase;
/**
* Defines a test form using autocomplete textfields.
*/
class FormTestAutocompleteForm implements FormInterface {
class FormTestAutocompleteForm extends FormBase {
/**
* {@inheritdoc}
......@@ -40,12 +40,6 @@ public function buildForm(array $form, array &$form_state) {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
......
......@@ -7,18 +7,34 @@
namespace Drupal\form_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a test form object.
*/
class FormTestControllerObject implements FormInterface, ControllerInterface {
class FormTestControllerObject extends FormBase {
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Constructs a new FormTestControllerObject.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory.
*/
public function __construct(ConfigFactory $config_factory) {
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'form_test_form_test_controller_object';
......@@ -29,44 +45,46 @@ public function getFormID() {
*/
public static function create(ContainerInterface $container) {
drupal_set_message(t('The FormTestControllerObject::create() method was used for this form.'));
return new static();
return new static(
$container->get('config.factory')
);
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $custom_attributes = NULL, Request $request = NULL) {
public function buildForm(array $form, array &$form_state, $custom_attributes = NULL) {
$form['element'] = array('#markup' => 'The FormTestControllerObject::buildForm() method was used for this form.');
$form['custom_attribute']['#markup'] = $custom_attributes;
$form['request_attribute']['#markup'] = $request->attributes->get('request_attribute');
$form['request_attribute']['#markup'] = $this->getRequest()->attributes->get('request_attribute');
$form['bananas'] = array(
'#type' => 'textfield',
'#title' => t('Bananas'),
'#title' => $this->t('Bananas'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
);
return $form;
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestControllerObject::validateForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestControllerObject::validateForm() method was used for this form.'));
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestControllerObject::submitForm() method was used for this form.'));
\Drupal::config('form_test.object')
drupal_set_message($this->t('The FormTestControllerObject::submitForm() method was used for this form.'));
$this->configFactory->get('form_test.object')
->set('bananas', $form_state['values']['bananas'])
->save();
}
......
......@@ -7,51 +7,51 @@
namespace Drupal\form_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormBase;
/**
* Provides a test form object.
*/
class FormTestObject implements FormInterface {
class FormTestObject extends FormBase {
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
* {@inheritdoc}
*/
public function getFormID() {
return 'form_test_form_test_object';
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['element'] = array('#markup' => 'The FormTestObject::buildForm() method was used for this form.');
$form['bananas'] = array(
'#type' => 'textfield',
'#title' => t('Bananas'),
'#title' => $this->t('Bananas'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
);
return $form;
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestObject::validateForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestObject::validateForm() method was used for this form.'));
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestObject::submitForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestObject::submitForm() method was used for this form.'));
\Drupal::config('form_test.object')
->set('bananas', $form_state['values']['bananas'])
->save();
......
......@@ -7,22 +7,22 @@
namespace Drupal\form_test;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormBase;
/**
* Provides a test form object.
*/
class FormTestServiceObject implements FormInterface {
class FormTestServiceObject extends FormBase {
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
* {@inheritdoc}
*/
public function getFormID() {
return 'form_test_form_test_service_object';
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['element'] = array('#markup' => 'The FormTestServiceObject::buildForm() method was used for this form.');
......@@ -30,29 +30,29 @@ public function buildForm(array $form, array &$form_state) {
$form['bananas'] = array(
'#type' => 'textfield',
'#default_value' => 'brown',
'#title' => t('Bananas'),
'#title' => $this->t('Bananas'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
);
return $form;
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestServiceObject::validateForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestServiceObject::validateForm() method was used for this form.'));
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The FormTestServiceObject::submitForm() method was used for this form.'));
drupal_set_message($this->t('The FormTestServiceObject::submitForm() method was used for this form.'));
\Drupal::config('form_test.object')
->set('bananas', $form_state['values']['bananas'])
->save();
......
......@@ -15,7 +15,7 @@
class SystemConfigFormTestForm extends SystemConfigFormBase {
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
* {@inheritdoc}
*/
public function getFormID() {
return 'form_test_system_config_test_form';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment