Issue #2064191 by yched, claudiu.cristea: Fix the 'target_bundle' field setting - 'target_bundles' are not validated when entity_reference is enabled

8.0.x
Nathaniel Catchpole 2015-09-30 19:39:44 +01:00
parent 33cce8a54c
commit bfa2458e12
9 changed files with 108 additions and 28 deletions

View File

@ -355,9 +355,6 @@ base_entity_reference_field_settings:
target_type:
type: string
label: 'Type of item to reference'
target_bundle:
type: string
label: 'Bundle of item to reference'
field_config_base:
type: config_entity

View File

@ -45,7 +45,6 @@ class EntityReferenceItem extends FieldItemBase {
public static function defaultStorageSettings() {
return array(
'target_type' => \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user',
'target_bundle' => NULL,
) + parent::defaultStorageSettings();
}
@ -93,16 +92,11 @@ class EntityReferenceItem extends FieldItemBase {
->setComputed(TRUE)
->setReadOnly(FALSE)
->setTargetDefinition(EntityDataDefinition::create($settings['target_type']))
// We can add a constraint for the target entity type. The list of
// referenceable bundles is a field setting, so the corresponding
// constraint is added dynamically in ::getConstraints().
->addConstraint('EntityType', $settings['target_type']);
if (isset($settings['target_bundle'])) {
$properties['entity']->addConstraint('Bundle', $settings['target_bundle']);
// Set any further bundle constraints on the target definition as well,
// such that it can derive more special data types if possible. For
// example, "entity:node:page" instead of "entity:node".
$properties['entity']->getTargetDefinition()
->addConstraint('Bundle', $settings['target_bundle']);
}
return $properties;
}
@ -151,6 +145,28 @@ class EntityReferenceItem extends FieldItemBase {
return $schema;
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
list($current_handler) = explode(':', $this->getSetting('handler'), 2);
if ($current_handler === 'default') {
$handler_settings = $this->getSetting('handler_settings');
if (!empty($handler_settings['target_bundles'])) {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints[] = $constraint_manager->create('ComplexData', [
'entity' => [
'Bundle' => [
'bundle' => $handler_settings['target_bundles'],
],
],
]);
}
}
return $constraints;
}
/**
* {@inheritdoc}
*/

View File

@ -30,17 +30,6 @@ use Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint;
*/
class ConfigurableEntityReferenceItem extends EntityReferenceItem implements OptionsProviderInterface, PreconfiguredFieldUiOptionsInterface {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
// The target bundle is handled by the 'target_bundles' property in the
// 'handler_settings' instance setting.
unset($settings['target_bundle']);
return $settings;
}
/**
* {@inheritdoc}
*/

View File

@ -0,0 +1,28 @@
<?php
/**
* @file
* Install, update and uninstall functions for the field module.
*/
/**
* Removes the stale 'target_bundle' storage setting on entity_reference fields.
*/
function field_update_8001() {
$config = \Drupal::configFactory();
/** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$item_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
// Iterate on all fields storage.
foreach ($config->listAll('field.storage.') as $field_id) {
$field_storage = $config->getEditable($field_id);
$class = $field_type_manager->getPluginClass($field_storage->get('type'));
// Deal only with entity reference fields and descendants.
if ($class == $item_class || is_subclass_of($class, $item_class)) {
// Remove 'target_bundle' from settings.
$field_storage->clear('settings.target_bundle')->save(TRUE);
}
}
}

View File

@ -83,7 +83,6 @@ class MigrateFieldInstanceTest extends MigrateDrupal6TestBase {
// storages so we end up with the default value for this setting.
'handler' => 'default:node',
'handler_settings' => array(),
'target_bundle' => NULL,
);
$field_settings = $field->getSettings();
ksort($expected);

View File

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\field\Tests\Update\EntityReferenceTargetBundleUpdateTest.
*/
namespace Drupal\field\Tests\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* Tests that field settings are properly updated during database updates.
*
* @group field
*/
class EntityReferenceTargetBundleUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
];
}
/**
* Tests field_update_8001().
*
* @see field_update_8001()
*/
public function testFieldUpdate8001() {
$configFactory = $this->container->get('config.factory');
// Load the 'node.field_image' field storage config, and check that is has
// a 'target_bundle' setting.
/** @var \Drupal\Core\Config\Config */
$config = $configFactory->get('field.storage.node.field_image');
$settings = $config->get('settings');
$this->assertTrue(array_key_exists('target_bundle', $settings));
// Run updates.
$this->runUpdates();
// Reload the config, and check that the 'target_bundle' setting has been
// removed.
$config = $configFactory->get('field.storage.node.field_image');
$settings = $config->get('settings');
$this->assertFalse(array_key_exists('target_bundle', $settings));
}
}

View File

@ -689,9 +689,9 @@ class EntityFieldTest extends EntityUnitTestBase {
$definition = BaseFieldDefinition::create('entity_reference')
->setLabel('Test entity')
->setSetting('target_type', 'node')
->setSetting('target_bundle', 'article');
->setSetting('handler_settings', ['target_bundles' => ['article' => 'article']]);
$reference_field = \Drupal::TypedDataManager()->create($definition);
$reference = $reference_field->appendItem(array('entity' => $node))->get('entity');
$reference = $reference_field->appendItem(array('entity' => $node));
$violations = $reference->validate();
$this->assertEqual($violations->count(), 1);

View File

@ -19,7 +19,6 @@ settings:
target_type: file
display_field: false
display_default: false
target_bundle: null
module: image
locked: false
cardinality: 1

View File

@ -19,7 +19,6 @@ settings:
target_type: file
display_field: false
display_default: false
target_bundle: null
module: image
locked: false
cardinality: 1