Skip to content
Snippets Groups Projects
Commit 807d2a3c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3512187 by alexpott: When removing a link field we should remove the entire value

parent 097dc8c6
No related branches found
No related tags found
1 merge request!20Resolve #3512187 "When removing a"
Pipeline #445705 passed
......@@ -313,6 +313,7 @@ class EntityUsageUpdater implements EntityUsageUpdaterInterface {
'@old_entity_type' => $old_target->getEntityTypeId(),
'@old_entity_id' => $old_target->id(),
]);
$context['sandbox']['filter_empty_items'] = TRUE;
self::processReferences($old_target, $entity_type_id, $entity_id, $func, $log_message, $usages, $context);
}
......@@ -401,13 +402,18 @@ class EntityUsageUpdater implements EntityUsageUpdaterInterface {
$updater = $updater_manager->createInstance($method);
assert($updater instanceof EntityUsageUpdaterPluginInterface);
foreach ($new_translation->get($reference['field_name']) as $item) {
$field = $new_translation->get($reference['field_name']);
assert($field instanceof FieldItemListInterface);
foreach ($field as $item) {
// Use entity_usage to verify if the target entity is in the field.
$old_target_string = $old_target->getEntityTypeId() . '|' . $old_target->id();
if (in_array($old_target_string, $tracker->getTargetEntities($item), TRUE)) {
$func($updater, $item);
}
}
if ($context['sandbox']['filter_empty_items'] ?? FALSE) {
$field->filterEmptyItems();
}
}
catch (PluginException $e) {
$context['results']['errors'][] = $e->getMessage();
......
......
......@@ -4,7 +4,10 @@ declare(strict_types=1);
namespace Drupal\Tests\entity_usage_updater\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\entity_usage\Traits\EntityUsageLastEntityQueryTrait;
......@@ -27,6 +30,7 @@ class LinkRemoverFormTest extends BrowserTestBase {
'field_ui',
'text',
'path',
'link',
'entity_usage_updater',
];
......@@ -38,6 +42,35 @@ class LinkRemoverFormTest extends BrowserTestBase {
$this->drupalCreateContentType(['type' => 'page']);
// Create a field with settings to validate.
$link_storage = FieldStorageConfig::create([
'field_name' => 'test_links',
'entity_type' => 'node',
'type' => 'link',
'cardinality' => 3,
'settings' => [],
]);
$link_storage->save();
$link_field = FieldConfig::create([
'field_storage' => $link_storage,
'bundle' => 'page',
'settings' => [
'title' => DRUPAL_DISABLED,
'link_type' => LinkItemInterface::LINK_GENERIC,
],
]);
$link_field->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getFormDisplay('node', 'page')
->setComponent('test_links', [
'type' => 'link_default',
])
->save();
$display_repository->getViewDisplay('node', 'page')
->setComponent('test_links', ['type' => 'link'])
->save();
// Set up the filter formats used by this test.
$basic_html_format = FilterFormat::create([
'format' => 'basic_html',
......@@ -100,10 +133,14 @@ class LinkRemoverFormTest extends BrowserTestBase {
$this->drupalGet('node/add/page');
$page->fillField('title[0][value]', 'Node 3');
$page->fillField('body[0][value]', (string) $node1->toLink("Link", options: ['absolute' => TRUE])->toString());
$page->fillField('test_links[0][uri]', '/node/1');
$page->fillField('test_links[1][uri]', 'http://example.org');
$page->fillField('test_links[2][uri]', '/node/1');
$page->pressButton('Save');
$assert_session->pageTextContains('Node 3 has been created.');
$node3 = $this->drupalGetNodeByTitle('Node 3');
$this->assertSession()->linkByHrefExists($node1->toUrl()->toString());
$this->assertSession()->linkByHrefExists('http://example.org');
// Create node 4.
$this->drupalGet('node/add/page');
......@@ -185,6 +222,8 @@ class LinkRemoverFormTest extends BrowserTestBase {
// Check to see if the links have been removed.
$this->drupalGet('node/3');
$this->assertSession()->linkByHrefNotExists($node1->toUrl()->toString());
$this->assertSession()->linkByHrefExists('http://example.org');
$this->drupalGet('node/4');
$this->assertSession()->linkByHrefNotExists($node2->toUrl()->toString());
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment