Problem/Motivation
If you define an entity reference field as a base field in code, and the entity type that the field references doesn't exist, you get an exception about the missing entity type. But the exception message doesn't tell you anything about the field.
This means that a developer who has messed up their field definitions, or who hasn't enabled all the right modules in a Kernel test, doesn't know where to look for the problem.
Steps to reproduce
Either:
- Define an entity reference base field on an entity type with a made-up referenced entity type
- Create a kernel test, enable a module that provides and entity type with an entity reference base field, but don't enable the module that provides the referenced entity type
You get this exception:
> Drupal\Component\Plugin\Exception\PluginNotFoundException: The "taxonomy_term" entity type does not exist.
Proposed resolution
EntityReferenceItem should catch this exception and throw a FieldException instead, stating the location of the problem, e.g.:
> Drupal\Core\Field\FieldException: Field 'foo' on entity type 'my_entity' references a target entity type 'taxonomy_term' which does not exist.
Remaining tasks
Do it.
Update any tests that expect the exception.
User interface changes
None.
API changes
A different type of exception is now thrown.
Data model changes
None.
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #46 | 3174924-MR-2948.patch | 4.09 KB | smustgrave |
Issue fork drupal-3174924
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
joachim commentedSomething like this would do the job.
Comment #3
kiwimind commentedWhile I realise that this hasn't made it into official coding standards yet, isn't it good practice to alphabetise the use statements?
https://www.drupal.org/project/coding_standards/issues/1624564
Otherwise, good job. 👍
Comment #5
joachim commentedFixed order of imports.
Comment #6
kiwimind commentedNice quality of life fix, considerably more useful than existing. Thanks.
Comment #7
catchMinor but it would be good to add a @throws to the phpdoc. However, it's currently doing @inheritdoc and I'm not sure it's worth breaking that to add the one line... what do you think?
Comment #8
joachim commentedThe Drupal documentation standards aren't clear about whether @throws documents that the *actual* function's code throws an exception, or whether it should also document that an exception could be thrown by something else that the function calls.
I'd argue for the latter, because as a consumer of a function, you don't care what it does internally: you just want to know that calling foo() could cause an exception to be thrown.
In which case, the interface definition of schema() should document that it can throws exceptions, and it's an existing documentation bug that should be fixed as a separate issue.
Comment #11
catch#8 is reasonable - let's open a follow-up to improve the ::schema() docs.
Looks like it ought to be possible to add some test coverage here.
Comment #12
joachim commented> In which case, the interface definition of schema() should document that it can throws exceptions, and it's an existing documentation bug that should be fixed as a separate issue.
Filed #3268120: FieldItemInterface::schema() should document that it can throw multiple exceptions for that.
Rerolled the patch for 9.4.
Comment #13
ranjith_kumar_k_u commentedFixed CS errors.
Comment #15
joachim commentedLooks good!
Comment #16
catchCould still use some test coverage.
Comment #17
rakhi soni commentedKindly review patch for version 9.5x,,
Comment #18
joachim commentedStill needs work.
And also, latest patch has added some indentation errors.
Comment #19
sourabhjainI will work on this.
Comment #20
sourabhjainI have resolved the patch #17 indentation issues.
Please review.
Comment #21
sourabhjainComment #22
joachim commentedAdded a kernel test.
Comment #23
bnjmnmPatch #13 applies fine to 9.5.x, there was no reason for the reroll in #17. The only difference is changed indentations that fail PHP coding standards. Removing credit.
Comment #24
kiwimind commentedCan I please clarify: you're removing credit from everyone that hasn't submitted what you deem a relevant patch?
I understand that we need to be careful of people gaming the system, however all too often I see reviewers and commenters on issues that don't receive credit (yes, myself included on this issue).
Arguably a patch that does nothing but introduce errors without moving the code forwards should be considered, but have the contributions that Catch and I made not been valuable to the conversation?
I believe that this can only harm the community as there is a chance that people may not continue reviewing if they're not recognised for it.
Comment #25
bnjmnmI will remove credit for patches that claim to be rerolls when a reroll wasn't actually needed. I suppose that means I don't "deem it relevant", but that phrasing makes it seem like a more subjective decision than it is. #17 claims to be a reroll of #13, despite that patch not needing a reroll. The testbot proves that the patch in #13 applies to 9.5 with all tests passing, so the reroll that followed it was clearly not needed.
Credit isn't actually granted until an issue is committed, and committers will make sure to add anyone who left useful comments. The state of the credit checkboxes before a commit is not indicative of anyone being denied credit.
Committers rarely miss adding people who added productive comments before the commit. Because providing an attachment automatically switches the credit checkbox "on" for that user, and the uploads can look similar to legitimate contributions, it's easier for a committer to miss the not-legitimate contributions. I preemptively remove credit so ensure it's not missed during the commit process, and so the user is aware they're not properly contributing to the issue so they can change their behavior moving forward.
I didn't deny you or Catch credit. Is there a wording in #23 that would have made it clearer that this was specific to just the user that uploaded an unnecessary reroll?
Comment #26
kiwimind commentedThank you for the clarification, appreciate your response.
This has not been my experience, which is why I raised the question about the comments and reviews that have been left so far. In my experience people that don't add patches are more often than not omitted from being credited. I realise that without a patch people are not automatically added to the credit list, however I (incorrectly it seems) assumed that if you were doing a tidy up in the credits that the list might have changed. I apologise if I've misconstrued.
For those of us that aren't able to spend as much time in the issue queues as we'd like, the odd credit here and there does have meaning and I've missed out on a good amount of credits when not adding patches.
Thanks for your time, appreciate it.
Comment #27
bnjmnm@kiwimind If you (or anyone reading this, TBH) see a completed issue you believe you should have received credit on, you can contact the committer on Drupal Slack. There's a chance it was a mistake, and even if it wasn't they can provide additional explanation. As careful as I try to be when committing, I can sometimes miss someone, so I appreciate when someone alerts me to that.
Comment #28
kiwimind commentedThank you for your gracious reply. Realised I've hijacked this issue now, apologies.
I'll bear that in mind for future reference.
All the best.
Comment #29
joachim commentedYes, a bit ;) Could either of you take a couple of minutes to review this please?
Comment #30
sourabhjainComment #32
joachim commentedRebased on 10.1.x.
Comment #34
shubham chandra commentedAdded patch against #22 in Drupal 10.1.x
Comment #35
joachim commented@Shubham Chandra we're using an MR on this issue, not patches. And at a glance, the patch you've uploaded looks identical to the current state of the MR?
Comment #36
smustgrave commentedCan confirm that #34 is a copy of the MR minus the index numbers.
But tested the MR.
The tests fail without the fix and pass with the fix.
Looks like a great update to have and will be useful!
Comment #38
smustgrave commentedPutting back as the MR was reviewed not the patch
Comment #39
xjmRemoving credit for the unnecessary patch in #34.
Looking back, we haven't had a clean test-only run proving the test coverage. I'm opening a separate MR branch for that.
Comment #40
xjmComment #42
xjmComment #43
xjmSaving issue credits as follows according to the core issue credit guidelines:
Comment #44
xjmThis looks pretty good, except we also need to update the
@throwsdocumentation for the method. Thanks everyone!Comment #45
joachim commentedDone, but bear in mind that the @throw docs for schema() need more detail anyway: #3268120: FieldItemInterface::schema() should document that it can throw multiple exceptions.
Comment #46
smustgrave commentedRemoving credit from myself as I just rebased to see if the tests passed, will let the committer decide to add credit back :)
The changes look good to me. The changes requested by @xjm to be addressed.
Unfortunately this seems to have been bitten by the random composer error so the tests won't run.
Uploading a patch for the tests to run
For any failures please do not reroll this issue is being worked on in the MR this is just for tests.
Comment #47
smustgrave commentedSeeing green.
Not sure this is blocked until the composer issue is resolved.
Comment #48
xjmI wonder if we should have a change record for this? Someone could have code handling the old plugin exception.
Comment #49
smustgrave commentedTook a crack at the CR.
Comment #50
xjmMake a few small edits to the CR.
This looks good to me now, thanks! Committed to 10.1.x and published the CR. Since a change to a thrown exception is a disruptive change, I did not backport it to the production branches.
Comment #51
papagrande@smustgrave @xjm Sorry to be nitpicky and pedantic, but don’t you need a comma before “which” in the error message?
https://www.thepunctuationguide.com/comma.html#settingoffnonrestrictivei...