Problem/Motivation
If you call Editor::getFilterFormat() to see if it's set, it doesn't work, and the error message isn't very helpful:
$format = $editor->getFilterFormat();
if (empty($format)) {
return;
}
If you do this before an editor object is saved, you get a frustrating fatal error about trying to load an entity with a NULL id.
This usually happens calling code in a subroutine at '/admin/config/content/formats/add'.
The error message should be more helpful. The correct way to use this code is to check first with ::hasAssociatedFilterFormat().
if (!$editor->hasAssociatedFilterFormat()) {
return;
}
$format = $editor->getFilterFormat();
if (empty($format)) {
return;
}
Proposed resolution
Throw a \DomainException with a helpful message if $editor->getFilterFormat() called without a text format assigned to the Editor.
Remaining tasks
Fix the tests as described in #46.
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3083746
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
oknateComment #3
oknateComment #4
oknateComment #5
oknatecancelled the test, because I posted a new patch soon after. See #6.
Comment #6
oknateComment #8
wim leersI think it'd be better to throw a
\DomainExceptioninstead.The test coverage doesn't actually match the issue title. Either the test or the issue title is inaccurate.
Comment #10
wim leersComment #11
oknateUpdated the title, IS and patch
Comment #12
oknateComment #13
wim leers👍
Comment #14
alexpottThis no longer sets $this->filterFormat... I think
would work better.
This is not testing what you think it is. After the exception is thrown nothing else in the test is executed. So if you change assertInstanceOf to assertNotInstanceOf the test still passes.
Should test the exception message too just in case there are multiple domain exceptions.
Comment #15
oknateAddressing #14
1) Nice catch, updated.
2) Reversed the order so the exception is tested last.
3) Added assertion for the exception message.
Comment #16
oknateAdding credit for Alex Pott. Update: It didn't seem to take. So when this is committed, if not committed by Alex, he should get credit for his very good feedback.
Comment #17
alexpottThinking about this some more... I think including the ID of the editor entity would be helpful. Also can you use
__METHOD__instead of hardcoding the method name. It makes the code a bit more portable.@oknate unfortunately the credit checkboxes only work for maintainers.
Comment #18
oknateSo what are you thinking for the message? How's this?
"You cannot call getFilterFormat() on the HelloWorld Editor since it does not have an assigned text format."
or
"You cannot call getFilterFormat() on the editor HelloWorld since it does not have an assigned text format."
Comment #19
alexpottI think
You cannot call getFilterFormat() on the editor HelloWorld since it does not have an assigned text format.works for me. Not sure - either version looks fine to be honest. In exception messages you can use sprintf() and not FormattableMarkup() because error mostly not going to HTML (think syslog). So variables are often put in quotes.Comment #20
oknateUpdated the output. I wasn't sure if I should put quotes around the editor ID or the method. Let me know if I should remove the quotes from either.
Comment #21
alexpott@oknate sorry I should have been clearer. I think only the editor ID needs quotes. Other than that this looks great.
Comment #22
oknateUpdated to remove the first set of double quotes.
Comment #23
phenaproximaI have a feeling that this is going to be contentious. The documentation for EditorInterface::getFilterFormat() explicitly says this:
That makes me think that this behavior, unhelpful though it may be, was intentional and that adding this exception constitutes an API/behavior change. Therefore, I'm tagging this for framework manager review.
This change seems out of scope?
Comment #24
phenaproximaOh wait -- looking up in the issue, I see that @alexpott already reviewed this patch. If he thought this was okay, I guess this doesn't technically need framework manager review. Still...the interface documentation says something different, so I'm going to keep the tag.
Comment #25
oknateRe:
The problem is with this code. It doesn't verify that
$this->formatis!empty:So this part of the documentation was wrong:
It wouldn't return NULL. It would throw an error when trying to load the entity.
Comment #26
oknateAddressing #23
1) Updating the interface documentation, to update the returns NULL, and adding the throws.
2) Removing the out of scope change (formatting).
Comment #27
alexpott+1 on fixing the docs and throwing a proper exception.
Comment #28
jhedstromExplicit UI tests were added in #2207777: Can not configure editor whilst creating a new text format, so I think this change won't break/regress as those are still passing here with this change.
This looks good to me. +1 RTBC.
Comment #29
oknateAll three places touched in the patch in #28 use this pattern before calling getFilterFormat():
That's the right way to do it, but the documentation, as @phenaproxima pointed out, said it could return NULL, which wouldn't happen, because if the filter format was missing, it would throw an error trying to load the filter format with a null value.
Comment #33
wim leersWhat remains to be done here? This seems very ready per #27, #28 and #29? 🤔
Comment #34
longwaveAgree with #33, there seems nothing left to do here, the patch is easy to follow and adds a test. The patch still applies cleanly to 9.3.x as well.
Comment #35
catchI think this could do with a bit more explanation why the editor wouldn't have an associate format (only when it's being created?) (which the original comment being removed has).
Comment #39
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
#35 still needs to be addressed
Tagging for change record so others are aware of the new change.
Rest looks good!
Comment #40
wim leersI think this would be a good novice issue 😊
Comment #42
wim leersAny takers? I promise reviews :)
Comment #45
keshav patel commentedCreated the MR using the #26 patch and addressed comment #35 as well, but the pipeline failed. keeping it on "Needs work".., please review the changes.
Change as per #35:
Comment #46
wim leersThose are all broken tests. Each of them is creating an
Editorconfig entity without a correspondingFilterFormatalready existing. We should fix those tests.Comment #47
anicotoComment #48
xjmUpdating the remaining tasks to point to @Wim Leers' feedback. Thanks!
Comment #49
bibliophileaxeComment #50
bibliophileaxeThe Drupal Contribution Mentoring team is triaging issues for DrupalCon Barcelona 2024, and we are reserving this issue for Mentored Contribution during the event.
After September 27, 2024, this issue returns to being open to all. Thanks!
Comment #51
stefdewa commentedI looked into updating the tests, specifically the failing tests from
core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest::testInvalidPluginDefinitionswhich calls
core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest::providerTestInvalidPluginDefinitions.The provider returns YAML with invalid ckeditor5 plugin definitions. The way I understand it, this YAML should define a
FilterFormatthat the ckeditor5Editoruses. But how... that is not clear to me. Anyone have an idea on how this is done?