Problem/Motivation
There's a following code found at \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::processStubRow():
$bundle_key = $this->getKey('bundle');
if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) {
if (empty($this->bundles)) {
throw new MigrateException('Stubbing failed, no bundles available for entity type: ' . $this->storage->getEntityTypeId());
}
$row->setDestinationProperty($bundle_key, reset($this->bundles));
}
// Populate any required fields not already populated.
$fields = $this->entityManager
->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle_key);
The $bundle_key
variable contains bundle property name (e.g. 'type' for a node) while getFieldDefinitions()
expects bundle name.
The effects are following:
1) The $fields
variable is not correctly populated.
2) Some entity types may throw an exception or error; in my particular case, \Drupal\commerce_order\Entity\OrderItem::bundleFieldDefinitions()
was throwing an Error: Call to a member function getPurchasableEntityTypeId()
on null error since it failed to load a bundle.
Proposed resolution
I think we should pass $row->getDestinationProperty($bundle_key)
(which is a bundle property value) rather than just $bundle_key
.
Remaining tasks
Review.
Commit.
Comment | File | Size | Author |
---|---|---|---|
#13 | 2954982-13.patch | 3.17 KB | Matroskeen |
#13 | 2954982-13-test_only.patch | 2.24 KB | Matroskeen |
Comments
Comment #2
abrammHere's a patch for 8.6.x-dev.
Comment #3
MatroskeenLooks good to me.
Btw, was wondering that it still uses EntityManager which is deprecated since Drupal 8.0.0.
But I think it should be handled in another issue, right?
Comment #4
alexpottIn order to commit a bug fix we need an automated to test to prove that we've fixed the bug and ensure that we don't break it again in the future. For more information about writing tests in Drupal 8 see the following links:
Comment #5
joachim CreditAttribution: joachim at Torchbox commentedI wonder how this has escaped detection.
There are subclasses for some entity types that override EntityContentBase::processStubRow(), but they call the parent.
Do we maybe not have any tests for stubbing at all?
Comment #10
Rob230 CreditAttribution: Rob230 as a volunteer and at CTI Digital commentedRerolled for 8.8. This patch also applies to 8.9.
Comment #12
MatroskeenAccording to #4, uploading two patches for 9.2.x to prove that issue still exists. Also, tagging with "Bug Smash Initiative".
Comment #13
MatroskeenRe-uploading with cs fix.
Comment #16
MatroskeenReady for review 🙂
Comment #17
quietone CreditAttribution: quietone as a volunteer commentedI am not familiar with the whole stubbing process so I applied the patch and walked through the test. This works as expected and fixes the problem in the IS and the proposed resolution agrees with the patch. And we have a fail test that shows the problem.
So, this is good to go.
Comment #18
alexpottCommitted and pushed 9d96600683 to 9.2.x and 027f08e7ed to 9.1.x. Thanks!
Comment #21
Wim LeersWoah! 🙈🤯
Comment #23
michal.k CreditAttribution: michal.k commentedHello,
Could you explain me what I am doing wrong trying to use `dom_migration_lookup` plugin. It breaks on this particular piece of code in EntityContentBase::processStubRow().
Scenario:
- It's migration from D7 to D9
- Migrating several content types that have links in body field to each other
- These links in body field are preprocessed by migration in a way that in the output they look like following pattern: /node/[node-type]/[nid]
The problem:
The problem is that in this condition
To be exact $row->getDestinationProperty($bundle_key) is empty BUT should have proper destination content type machine name from `dom_migration_lookup` therefore following the logic it takes the first content type from the stack:
$row->setDestinationProperty($bundle_key, reset($this->bundles));
and populate wrong default filelds in code below this condition.
Body field migration definition:
Workaround:
I have found this workaround, but I believe there is a better solution for my case?
In file: `../core/modules/migrate/src/MigrateStub.php`
I'd be appreciated for any tips!
Best regards,
Michał.