Commit 4553018c authored by alexpott's avatar alexpott

Issue #2463029 by Wim Leers: EntityFormDisplay should update $form with cache...

Issue #2463029 by Wim Leers: EntityFormDisplay should update $form with cache tags of FieldConfig, FieldStorageConfig, EntityFormDisplay config entities
parent fdc1c3da
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Entity\Entity;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityDisplayPluginCollection;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
......@@ -33,6 +34,13 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn
*/
protected $displayContext = 'form';
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Returns the entity_form_display object used to build an entity form.
*
......@@ -115,6 +123,7 @@ public static function collectRenderDisplay(FieldableEntityInterface $entity, $f
*/
public function __construct(array $values, $entity_type) {
$this->pluginManager = \Drupal::service('plugin.manager.field.widget');
$this->renderer = \Drupal::service('renderer');
parent::__construct($values, $entity_type);
}
......@@ -165,9 +174,23 @@ public function buildForm(FieldableEntityInterface $entity, array &$form, FormSt
// processForm(), but is needed for other forms calling this method
// directly.
$form[$name]['#weight'] = $options['weight'];
// Associate the cache tags for the field definition & field storage
// definition.
$field_definition = $this->getFieldDefinition($name);
if ($field_definition instanceof CacheableDependencyInterface) {
$this->renderer->addDependency($form[$name], $field_definition);
}
$field_storage_definition = $field_definition->getFieldStorageDefinition();
if ($field_storage_definition instanceof CacheableDependencyInterface) {
$this->renderer->addDependency($form[$name], $field_storage_definition);
}
}
}
// Associate the cache tags for the form display.
$this->renderer->addDependency($form, $this);
// Add a process callback so we can assign weights and hide extra fields.
$form['#process'][] = array($this, 'processForm');
}
......
......@@ -220,6 +220,8 @@ function testRegistrationWithUserFields() {
// Check that the field does not appear on the registration form.
$this->drupalGet('user/register');
$this->assertNoText($field->label(), 'The field does not appear on user registration form');
$this->assertCacheTag('config:core.entity_form_display.user.user.register');
$this->assertCacheTag('config:user.settings');
// Have the field appear on the registration form.
entity_get_form_display('user', 'user', 'register')
......@@ -228,6 +230,7 @@ function testRegistrationWithUserFields() {
$this->drupalGet('user/register');
$this->assertText($field->label(), 'The field appears on user registration form');
$this->assertRegistrationFormCacheTagsWithUserFields();
// Check that validation errors are correctly reported.
$edit = array();
......@@ -236,10 +239,12 @@ function testRegistrationWithUserFields() {
// Missing input in required field.
$edit['test_user_field[0][value]'] = '';
$this->drupalPostForm(NULL, $edit, t('Create new account'));
$this->assertRegistrationFormCacheTagsWithUserFields();
$this->assertRaw(t('@name field is required.', array('@name' => $field->label())), 'Field validation error was correctly reported.');
// Invalid input.
$edit['test_user_field[0][value]'] = '-1';
$this->drupalPostForm(NULL, $edit, t('Create new account'));
$this->assertRegistrationFormCacheTagsWithUserFields();
$this->assertRaw(t('%name does not accept the value -1.', array('%name' => $field->label())), 'Field validation error was correctly reported.');
// Submit with valid data.
......@@ -256,6 +261,7 @@ function testRegistrationWithUserFields() {
$field_storage->save();
foreach (array('js', 'nojs') as $js) {
$this->drupalGet('user/register');
$this->assertRegistrationFormCacheTagsWithUserFields();
// Add two inputs.
$value = rand(1, 255);
$edit = array();
......@@ -283,4 +289,14 @@ function testRegistrationWithUserFields() {
}
}
/**
* Asserts the presence of cache tags on registration form with user fields.
*/
protected function assertRegistrationFormCacheTagsWithUserFields() {
$this->assertCacheTag('config:core.entity_form_display.user.user.register');
$this->assertCacheTag('config:field.field.user.user.test_user_field');
$this->assertCacheTag('config:field.storage.user.test_user_field');
$this->assertCacheTag('config:user.settings');
}
}
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