.../Drupal/Core/Entity/Entity/EntityFormDisplay.php | 18 ++++++++++++++++++ core/modules/simpletest/src/WebTestBase.php | 2 +- core/modules/user/src/Tests/UserRegistrationTest.php | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index cecedd4..abb00d9 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -7,10 +7,12 @@ namespace Drupal\Core\Entity\Entity; +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityDisplayPluginCollection; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Entity\EntityDisplayBase; +use Drupal\Core\Field\FieldConfigInterface; use Drupal\Core\Form\FormStateInterface; /** @@ -165,9 +167,25 @@ 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 FieldConfigInterface) { + // @todo: Use RendererInterface::addDependency() once https://www.drupal.org/node/2444231 lands. + $form['name']['#cache']['tags'] = $field_definition->getCacheTags(); + $field_storage_definition = $field_definition->getFieldStorageDefinition(); + if ($field_storage_definition instanceof FieldConfigInterface) { + $form['name']['#cache']['tags'] = Cache::mergeTags($form['name']['#cache']['tags'], $field_storage_definition->getCacheTags()); + } + } } } + // Associate the cache tags for the form display. + // @todo: Use RendererInterface::addDependency() once https://www.drupal.org/node/2444231 lands. + $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $this->getCacheTags()); + // Add a process callback so we can assign weights and hide extra fields. $form['#process'][] = array($this, 'processForm'); } diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 29271ee..5a05016 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -2748,7 +2748,7 @@ protected function buildUrl($path, array $options = array()) { */ protected function assertCacheTag($expected_cache_tag) { $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); - $this->assertTrue(in_array($expected_cache_tag, $cache_tags)); + $this->assertTrue(in_array($expected_cache_tag, $cache_tags), sprintf("'%s' cache tag is present.", $expected_cache_tag)); } } diff --git a/core/modules/user/src/Tests/UserRegistrationTest.php b/core/modules/user/src/Tests/UserRegistrationTest.php index 2666665..24e6972 100644 --- a/core/modules/user/src/Tests/UserRegistrationTest.php +++ b/core/modules/user/src/Tests/UserRegistrationTest.php @@ -220,14 +220,23 @@ 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') ->setComponent('test_user_field', array('type' => 'test_field_widget')) ->save(); + $check_cache_tags = function() { + $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'); + }; $this->drupalGet('user/register'); $this->assertText($field->label(), 'The field appears on user registration form'); + $check_cache_tags(); // Check that validation errors are correctly reported. $edit = array(); @@ -236,10 +245,12 @@ function testRegistrationWithUserFields() { // Missing input in required field. $edit['test_user_field[0][value]'] = ''; $this->drupalPostForm(NULL, $edit, t('Create new account')); + $check_cache_tags(); $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')); + $check_cache_tags(); $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 +267,7 @@ function testRegistrationWithUserFields() { $field_storage->save(); foreach (array('js', 'nojs') as $js) { $this->drupalGet('user/register'); + $check_cache_tags(); // Add two inputs. $value = rand(1, 255); $edit = array();