Refactor ImageFieldCreationTrait to support entity types other than nodes

Created on 25 May 2019, over 5 years ago
Updated 26 April 2024, 7 months ago

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.

πŸ“Œ Task
Status

Fixed

Version

10.3 ✨

Component
Image moduleΒ  β†’

Last updated 17 days ago

Created by

Pancho UTC+2 πŸ‡ͺπŸ‡Ί EU

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

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

  • Pipeline finished with Canceled
    8 months ago
    Total: 35s
    #130438
  • πŸ‡¬πŸ‡§United Kingdom joachim

    Trying a new approach.

  • Pipeline finished with Failed
    8 months ago
    Total: 182s
    #130440
  • Pipeline finished with Canceled
    7 months ago
    Total: 79s
    #147087
  • Status changed to Needs review 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim
  • Pipeline finished with Failed
    7 months ago
    Total: 196s
    #147088
  • Status changed to Needs work 7 months ago
  • 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.

  • Pipeline finished with Failed
    7 months ago
    #148284
  • Status changed to Needs review 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim
  • Pipeline finished with Failed
    7 months ago
    Total: 959s
    #148299
  • Status changed to Needs work 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

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

    Hiding patches as fix is in MR.

  • πŸ‡¬πŸ‡§United Kingdom joachim

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

    Any feedback?

  • Pipeline finished with Canceled
    7 months ago
    Total: 639s
    #152117
  • Status changed to Needs review 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

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

  • Pipeline finished with Canceled
    7 months ago
    Total: 274s
    #152123
  • Pipeline finished with Failed
    7 months ago
    #152125
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    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.

  • Pipeline finished with Failed
    7 months ago
    Total: 6847s
    #155566
  • Status changed to Needs work 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    After rerunning tests they are failing

  • Status changed to Needs review 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

    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)

  • Pipeline finished with Failed
    7 months ago
    Total: 989s
    #155846
  • Status changed to Needs work 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    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.

  • Status changed to Needs review 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

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

  • Pipeline finished with Success
    7 months ago
    Total: 1017s
    #156093
  • Status changed to RTBC 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    Looks good now!

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

  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

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

  • Status changed to Needs work 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    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.

  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    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.

  • Pipeline finished with Success
    7 months ago
    Total: 986s
    #156463
  • Status changed to RTBC 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan
  • πŸ‡¬πŸ‡§United Kingdom joachim
  • Pipeline finished with Success
    7 months ago
    Total: 654s
    #156640
  • Pipeline finished with Success
    7 months ago
    Total: 989s
    #156638
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    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?

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    Weirdly wrong tag.

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    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...
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    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...
  • Status changed to Fixed 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    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...
Production build 0.71.5 2024