Commit 70716f2e authored by johnchque's avatar johnchque Committed by miro_dietiker

Issue #2807411 by jmuzz, yongt9412, kevinquillen, miro_dietiker: Deleting a...

Issue #2807411 by jmuzz, yongt9412, kevinquillen, miro_dietiker: Deleting a paragraph type can delete a field holding multiple paragraphs, not just that one reference
parent 02e9f65a
......@@ -361,11 +361,52 @@ class EntityReferenceRevisionsItem extends EntityReferenceItem implements Option
}
}
}
/**
* {@inheritdoc}
*/
/**
* {@inheritdoc}
*/
public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
return FALSE;
$changed = FALSE;
$entity_manager = \Drupal::entityManager();
$target_entity_type = $entity_manager->getDefinition($field_definition->getFieldStorageDefinition()
->getSetting('target_type'));
$handler_settings = $field_definition->getSetting('handler_settings');
// Update the 'target_bundles' handler setting if a bundle config dependency
// has been removed.
if (!empty($handler_settings['target_bundles'])) {
if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) {
if ($storage = $entity_manager->getStorage($bundle_entity_type_id)) {
foreach ($storage->loadMultiple($handler_settings['target_bundles']) as $bundle) {
if (isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) {
unset($handler_settings['target_bundles'][$bundle->id()]);
$changed = TRUE;
// In case we deleted the only target bundle allowed by the field
// we can log a message because the behaviour of the field will
// have changed.
if ($handler_settings['target_bundles'] === []) {
\Drupal::logger('entity_reference_revisions')
->notice('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference revisions field (entity_type: %entity_type, bundle: %bundle) no longer specifies a specific target bundle. The field will now accept any bundle and may need to be adjusted.', [
'%target_bundle' => $bundle->label(),
'%target_entity_type' => $bundle->getEntityType()
->getBundleOf(),
'%field_name' => $field_definition->getName(),
'%entity_type' => $field_definition->getTargetEntityTypeId(),
'%bundle' => $field_definition->getTargetBundle()
]);
}
}
}
}
}
}
if ($changed) {
$field_definition->setSetting('handler_settings', $handler_settings);
}
return $changed;
}
/**
......
......@@ -39,12 +39,6 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
// Place the breadcrumb, tested in fieldUIAddNewField().
$this->drupalPlaceBlock('system_breadcrumb_block');
}
/**
* Tests the entity reference revisions configuration.
*/
public function testEntityReferenceRevisions() {
$admin_user = $this->drupalCreateUser(array(
'administer site configuration',
'administer nodes',
......@@ -56,7 +50,12 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
'edit any article content',
));
$this->drupalLogin($admin_user);
}
/**
* Tests the entity reference revisions configuration.
*/
public function testEntityReferenceRevisions() {
// Create a test target node used as entity reference by another test node.
$node_target = Node::create([
'title' => 'Target node',
......@@ -149,4 +148,37 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
$this->assertEqual((string) $properties['entity']->getLabel(), 'Content');
}
/**
* Tests target bundle settings for an entity reference revisions field.
*/
public function testMultipleTargetBundles() {
// Create a couple of content types for the ERR field to point to.
$target_types = [];
for ($i = 0; $i < 2; $i++) {
$target_types[$i] = $this->drupalCreateContentType([
'type' => strtolower($this->randomMachineName()),
'name' => 'Test type ' . $i
]);
}
// Create a new field that can point to either target content type.
$node_type_path = 'admin/structure/types/manage/entity_revisions';
// Generate a random field name, must be only lowercase characters.
$field_name = strtolower($this->randomMachineName());
$field_edit = [];
$storage_edit = ['settings[target_type]' => 'node', 'cardinality' => '-1'];
$field_edit['settings[handler_settings][target_bundles][' . $target_types[0]->id() . ']'] = TRUE;
$field_edit['settings[handler_settings][target_bundles][' . $target_types[1]->id() . ']'] = TRUE;
$this->fieldUIAddNewField($node_type_path, $field_name, 'Entity reference revisions', 'entity_reference_revisions', $storage_edit, $field_edit);
// Deleting one of these content bundles at this point should only delete
// that bundle's body field. Test that there is no second field that will
// be deleted.
$this->drupalGet('/admin/structure/types/manage/' . $target_types[0]->id() . '/delete');
$this->assertNoFieldByXPath('(//details[@id="edit-entity-deletes"]//ul[@data-drupal-selector="edit-field-config"]/li)[2]');
}
}
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