Issue #2459753 follow-up by tim.plunkett, alexpott: EntityForm::validate() should be able to modify the form structure

8.0.x
webchick 2015-04-15 13:05:59 -07:00
parent 0670a5fe01
commit fb5ea78b25
24 changed files with 40 additions and 23 deletions

View File

@ -121,7 +121,7 @@ abstract class ContentEntityConfirmFormBase extends ContentEntityForm implements
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
// Override the default validation implementation as it is not necessary // Override the default validation implementation as it is not necessary
// nor possible to validate an entity in a confirmation form. // nor possible to validate an entity in a confirmation form.
} }

View File

@ -74,7 +74,7 @@ class ContentEntityForm extends EntityForm implements ContentEntityFormInterface
* For more information about entity validation, see * For more information about entity validation, see
* https://www.drupal.org/node/2015613. * https://www.drupal.org/node/2015613.
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
$entity = $this->buildEntity($form, $form_state); $entity = $this->buildEntity($form, $form_state);
$this->getFormDisplay($form_state)->validateFormValues($entity, $form, $form_state); $this->getFormDisplay($form_state)->validateFormValues($entity, $form, $form_state);

View File

@ -247,7 +247,7 @@ class EntityForm extends FormBase implements EntityFormInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
// @todo Remove this. // @todo Remove this.
// Execute legacy global validation handlers. // Execute legacy global validation handlers.
$form_state->setValidateHandlers([]); $form_state->setValidateHandlers([]);

View File

@ -105,7 +105,7 @@ interface EntityFormInterface extends BaseFormIdInterface {
* @return \Drupal\Core\Entity\ContentEntityTypeInterface * @return \Drupal\Core\Entity\ContentEntityTypeInterface
* The built entity. * The built entity.
*/ */
public function validate(array $form, FormStateInterface $form_state); public function validate(array &$form, FormStateInterface $form_state);
/** /**
* Form submission handler for the 'save' action. * Form submission handler for the 'save' action.

View File

@ -123,7 +123,7 @@ abstract class ActionFormBase extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
if ($this->plugin instanceof PluginFormInterface) { if ($this->plugin instanceof PluginFormInterface) {

View File

@ -273,7 +273,7 @@ class BlockForm extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// The Block Entity form puts all block plugin form elements in the // The Block Entity form puts all block plugin form elements in the

View File

@ -223,7 +223,7 @@ class BlockContentForm extends ContentEntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateForm(array &$form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
if ($this->entity->isNew()) { if ($this->entity->isNew()) {
$exists = $this->blockContentStorage->loadByProperties(array('info' => $form_state->getValue(['info', 0, 'value']))); $exists = $this->blockContentStorage->loadByProperties(array('info' => $form_state->getValue(['info', 0, 'value'])));
if (!empty($exists)) { if (!empty($exists)) {

View File

@ -314,7 +314,7 @@ class CommentForm extends ContentEntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
$comment = parent::validate($form, $form_state); $comment = parent::validate($form, $form_state);
// Customly trigger validation of manually added fields and add in // Customly trigger validation of manually added fields and add in

View File

@ -112,7 +112,7 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// Validate and each email recipient. // Validate and each email recipient.

View File

@ -187,7 +187,7 @@ class MessageForm extends ContentEntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
$message = $this->entity; $message = $this->entity;

View File

@ -38,7 +38,7 @@ class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
$form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id')); $form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id'));

View File

@ -150,7 +150,7 @@ class FieldConfigEditForm extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
if (isset($form['default_value'])) { if (isset($form['default_value'])) {

View File

@ -146,7 +146,7 @@ class FieldStorageConfigEditForm extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// Validate field cardinality. // Validate field cardinality.

View File

@ -204,7 +204,7 @@ abstract class FilterFormatFormBase extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// @todo Move trimming upstream. // @todo Move trimming upstream.

View File

@ -286,7 +286,7 @@ class NodeForm extends ContentEntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
$node = parent::validate($form, $form_state); $node = parent::validate($form, $form_state);
if ($node->id() && (node_last_changed($node->id(), $this->getFormLangcode($form_state)) > $node->getChangedTime())) { if ($node->id() && (node_last_changed($node->id(), $this->getFormLangcode($form_state)) > $node->getChangedTime())) {

View File

@ -204,7 +204,7 @@ class NodeTypeForm extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
$id = trim($form_state->getValue('type')); $id = trim($form_state->getValue('type'));

View File

@ -133,7 +133,7 @@ class ResponsiveImageStyleForm extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
// Only validate on edit. // Only validate on edit.
if ($form_state->hasValue('keyed_styles')) { if ($form_state->hasValue('keyed_styles')) {
// Check if another breakpoint group is selected. // Check if another breakpoint group is selected.

View File

@ -144,7 +144,7 @@ abstract class SearchPageFormBase extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// Ensure each path is unique. // Ensure each path is unique.

View File

@ -155,7 +155,7 @@ abstract class DateFormatFormBase extends EntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// The machine name field should already check to see if the requested // The machine name field should already check to see if the requested

View File

@ -94,7 +94,7 @@ class TermForm extends ContentEntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
// Ensure numeric values. // Ensure numeric values.

View File

@ -354,7 +354,7 @@ abstract class AccountForm extends ContentEntityForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\user\UserInterface $account */ /** @var \Drupal\user\UserInterface $account */
$account = parent::validate($form, $form_state); $account = parent::validate($form, $form_state);

View File

@ -162,7 +162,7 @@ class ViewAddForm extends ViewFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
$wizard_type = $form_state->getValue(array('show', 'wizard_key')); $wizard_type = $form_state->getValue(array('show', 'wizard_key'));
$wizard_instance = $this->wizardManager->createInstance($wizard_type); $wizard_instance = $this->wizardManager->createInstance($wizard_type);
$form_state->set('wizard', $wizard_instance->getPluginDefinition()); $form_state->set('wizard', $wizard_instance->getPluginDefinition());

View File

@ -258,7 +258,7 @@ class ViewEditForm extends ViewFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(array $form, FormStateInterface $form_state) { public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
$view = $this->entity; $view = $this->entity;

View File

@ -9,6 +9,7 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -123,4 +124,20 @@ class EntityFormTest extends UnitTestCase {
$this->assertNull($result->get('key_controlled_by_plugin_collection')); $this->assertNull($result->get('key_controlled_by_plugin_collection'));
} }
/**
* @covers ::validate
*/
public function testValidate() {
$form_object = $this->getMock('Drupal\Core\Entity\EntityFormInterface');
$form_object->expects($this->once())
->method('validate')
->willReturnCallback(function (array &$form, FormStateInterface $form_state) {
$form['foo'] = 'bar';
});
$form['baz'] = 'bim';
$form_state = new FormState();
$form_object->validate($form, $form_state);
$this->assertArrayHasKey('foo', $form);
}
} }