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:

  1. Add an optional $entity_type to the end of the argument list. Ugly and not good for DX, but easy to do.
  2. 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.
  3. 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.
  4. 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.

Issue fork drupal-3057070

Command icon 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

Pancho created an issue. See original summary.

pancho’s picture

Status: Active » Needs review
Issue tags: +Traits
StatusFileSize
new4.03 KB

So 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.

pancho’s picture

With this patch, the following three Core tests:

  • Drupal\Tests\quickedit\FunctionalJavascript\QuickEditLoadingTest
  • Drupal\Tests\taxonomy\Functional\TaxonomyImageTest
  • Drupal\Tests\user\Functional\UserRegistrationTest

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.

joachim’s picture

The 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.

+++ b/core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php
@@ -30,20 +30,46 @@
+  protected function createImageField2($field_name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') {
     FieldStorageConfig::create([

I'd say having a '2' at the end of a method name is pretty ugly, and we should break BC here.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new167 bytes

The 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Trying a new approach.

joachim’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.46 KB

The 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.

joachim’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Appears to have test failures. Also MR was draft was that on purpose?

Hiding patches as fix is in MR.

joachim’s picture

Yeah, I wanted to see what people thought of the approach I've taken for BC.

Any feedback?

joachim’s picture

Status: Needs work » Needs review

No feedback here or on slack, so updated all the existing calls.

nicxvan’s picture

That'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.

nicxvan’s picture

Status: Needs review » Needs work

After rerunning tests they are failing. There are a bunch of Image related failures so they are probably relevant.

joachim’s picture

Status: Needs work » Needs review

Argh 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)

nicxvan’s picture

Status: Needs review » Needs work

Yeah 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:

core/modules/image/tests/src/Functional/ImageOnTranslatedEntityTest.php
 There was 1 error:
    
    1)
    Drupal\Tests\image\Functional\ImageOnTranslatedEntityTest::testSyncedImages
    Symfony\Component\Validator\Exception\UnexpectedValueException: Expected
    argument of type "string", "array" given

Remaining self deprecation notices (1)
    
      1x: Calling
    Drupal\Tests\image\Kernel\ImageFieldCreationTrait::createImageField()
    without the $entity_type argument is deprecated in drupal:10.3.0 and this
    argument will be required in drupal:11.0.0. See
    https://www.drupal.org/node/3441322
        1x in ImageOnTranslatedEntityTest::testSyncedImages from
    Drupal\Tests\image\Functional

There are a bunch of other related to images, I suspect there is an indirect call missing in the testing somewhere.

joachim’s picture

Status: Needs work » Needs review

> 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 :/

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now!

I've also confirmed all instances have been updated now.

nicxvan’s picture

Sorry forgot to mention I also looked at the CR, looks good too.

nicxvan’s picture

Status: Reviewed & tested by the community » Needs work

Sorry 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.

nicxvan’s picture

Status: Needs work » Needs review

After 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.

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community

joachim’s picture

xjm’s picture

Issue tags: +Needs tests

Neat! 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?

xjm’s picture

Weirdly wrong tag.

xjm’s picture

OK, 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.

  • xjm committed cfe99268 on 11.x
    Issue #3057070 by joachim, Pancho, nicxvan: Refactor...
xjm’s picture

Version: 11.x-dev » 10.3.x-dev

Reviewed locally with git diff --color-words and committed to 11.x.

Meanwhile, I accepted my comment fix for an on-commit change to the backport version.

  • xjm committed 1ca4906e on 10.3.x
    Issue #3057070 by joachim, Pancho, xjm, nicxvan: Refactor...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Committed the backport version to 10.3.x and published the CR. Thanks everyone!

  • xjm committed cfe99268 on 11.0.x
    Issue #3057070 by joachim, Pancho, nicxvan: Refactor...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.