This is a followup to #2782309: Refactor File and Image related image field creation logic into a new trait.
Sadly, ImageFieldCreationTrait::createImageField() currently only supports node entities, so for testing image fields on any other fieldable entity, we largely have to duplicate the code in our tests.
The actually needed change is very minor. We however need a way to provide the $entity_type in the arguments. I can see the following ways to do this in a BC way:
- Add an optional $entity_type to the end of the argument list. Ugly and not good for DX, but easy to do.
- Allow supplying "entity_type:bundle" in the $type_name parameter, defaulting to 'node:$type_name'. Fully BC, still easy to do, but a bit of an anti-pattern, so not much better DX wise.
- Add a new method to the trait, turning the original createImageField() into a deprecated wrapper. Good. Except for the fact that "createImageField" is the best method name and I can't come up with an equally good one.
- Create a totally new trait, deprecating the original one. Allows us all freedoms, just as 3. Except for the fact that ImageFieldCreationTrait is the best trait name and I can't come up with an equally good one.
While I need some input on how to do it best, I'll rightaway provide a first patch for review.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3057070
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
panchoSo while I'm waiting for some input which BC strategy we should choose here, here's a first patch following the lines of (3).
Obviously
createImageField2()isn't meant to be the final name.Comment #3
panchoWith this patch, the following three Core tests:
may start using ImageFieldCreationTrait::createImageField(), thereby saving many lines of code.
So here's another patch simplifying these three tests.
Again, please note that createImageField2() obviously isn't meant to be the final name. And of course, we need to deprecate the old one. I'm just waiting for input for what is the preferred approach, see the OP.
Comment #4
joachim commentedThe BC policy says:
> Test traits and abstract base classes should generally use deprecation where possible rather than breaking backwards compatibility, but they are still considered internal API and may change if necessary.
I'd say having a '2' at the end of a method name is pretty ugly, and we should break BC here.
Comment #12
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #15
joachim commentedTrying a new approach.
Comment #16
joachim commentedComment #17
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #18
joachim commentedComment #19
smustgrave commentedAppears to have test failures. Also MR was draft was that on purpose?
Hiding patches as fix is in MR.
Comment #20
joachim commentedYeah, I wanted to see what people thought of the approach I've taken for BC.
Any feedback?
Comment #21
joachim commentedNo feedback here or on slack, so updated all the existing calls.
Comment #22
nicxvan commentedThat's a really clever way to handle inserting a new argument I think.
I'm leaving it in needs work since there are so many failures, though that might be related to the gitlab update.
Let me rerun it and see if it passed before moving status.
Comment #23
nicxvan commentedAfter rerunning tests they are failing. There are a bunch of Image related failures so they are probably relevant.
Comment #24
joachim commentedArgh I did a search and replace of the whole codebase! I could have SWORN I did that, and I don't see the commit!!!!
In case I have to do this again, the search and replace is:
>createImageField\(([^,]+), ([^,]+)\)
>createImageField($1, 'node', $2)
Comment #25
nicxvan commentedYeah I saw your comment, maybe you pushed it to a different branch or fork?
Anyway, there seem to be some relevant failing tests still. For example:
There are a bunch of other related to images, I suspect there is an indirect call missing in the testing somewhere.
Comment #26
joachim commented> There are a bunch of other related to images, I suspect there is an indirect call missing in the testing somewhere.
Nothing as complicated as that, just that last night I was tired and I forgot to also replace the
>createImageField\(([^,]+), ([^,]+),
>createImageField($1, 'node', $2,
uses as well :/
Comment #27
nicxvan commentedLooks good now!
I've also confirmed all instances have been updated now.
Comment #28
nicxvan commentedSorry forgot to mention I also looked at the CR, looks good too.
Comment #29
nicxvan commentedSorry I didn't catch this earlier, but this probably needs a test adding an image field to at least one other entity type now to prove it actually works and to ensure it doesn't regress in the future. As of now every instance of it is only calling node entities.
Comment #30
nicxvan commentedAfter discussing this on Slack.
@joachim pointed out the following: traits don't have dedicated test coverage.
Also this is a refactor not a bug fix so tests may not be required.
Waiting for tests to pass then I'll update status again.
Comment #31
nicxvan commentedComment #33
joachim commentedMade a 10.3.x branch, so we have:
- https://git.drupalcode.org/project/drupal/-/merge_requests/7739 - 10.3.x
- https://git.drupalcode.org/project/drupal/-/merge_requests/7207 - 11.x with deprecation removed
Comment #34
xjmNeat! Thanks for your thoughtfulness on BC here. Saving issue credits. Haven't started the code review yet.
Which/where was the pipeline job showing the demonstrative test fails as mentioned in #20?
Comment #35
xjmWeirdly wrong tag.
Comment #36
xjmOK, the approach is perfect. Looking into why the 11.x version doesn't have pipelines/does not appear to be mergeable when this is correctly filed against 11.x.
Comment #38
xjmReviewed locally with
git diff --color-wordsand committed to 11.x.Meanwhile, I accepted my comment fix for an on-commit change to the backport version.
Comment #40
xjmCommitted the backport version to 10.3.x and published the CR. Thanks everyone!