Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In #2912348: Handle entity_references related to Drupal 6 and 7 node translations with different IDs, we needed the config factory service in MigrationConfigurationTrait
and we used \Drupal::configFactory()
(See comments #77 and #78). There is also the state and migration plugin manager services that are accessed in the same maner.
Proposed resolution
Add getter methods to the trait with fall back on using \Drupal::()
and inject the services in the classes using the trait.
Remaining tasks
Check if it is possibleWrite the patch- Review
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#24 | diff-13.24.txt | 11.53 KB | quietone |
#24 | 2961226-24.patch | 18.63 KB | quietone |
#13 | interdiff-2961226-7-13.txt | 2.7 KB | maxocub |
#13 | 2961226-13.patch | 19.5 KB | maxocub |
Comments
Comment #2
catchhttps://api.drupal.org/api/drupal/core!lib!Drupal!Core!StringTranslation... is an example using setter injection.
$stringTranslation property.
setStringTranslation() method.
getStringTranslation() with a fallback to \Drupal.
Comment #3
maxocub CreditAttribution: maxocub as a volunteer commentedHere's a first patch.
Comment #4
quietone CreditAttribution: quietone at Acro Commerce commentedAssigning to self for review
Comment #5
quietone CreditAttribution: quietone at Acro Commerce commentedNo longer applies, needs a reroll
Comment #6
quietone CreditAttribution: quietone as a volunteer commentedComment #7
maxocub CreditAttribution: maxocub as a volunteer commentedHere's the re-roll.
Comment #8
msankhala CreditAttribution: msankhala as a volunteer and at Srijan | A Material+ Company commentedAccording to the scope of this issue this patch should only modify
MigrationConfigurationTrait
and use dependency injection in that trait but this patch is modifying way more than this. Patch is using dependency injection inmigrate_drupal
andmigrate_drupal_ui
module which is a bigger change.I'll suggest make dependency injection in
migrate_drupal
module in this issue and create another follow-up issue to use dependency injection inmigrate_drupal_ui
module. Rename this issue to Use dependency injection in migrate_drupal moduleComment #9
maxocub CreditAttribution: maxocub as a volunteer commentedBy itself, the trait cannot use dependency injection, it can only provide setter and getter methods, with fallback on using
\Drupal::
. For the trait to really use dependency injection, it's the job for the classes that are using the trait to inject the needed services. Otherwise, the trait will fallback on using\Drupal::
and this patch will have no impact.The changes in migrate_drupal_ui are to inject the services in the classes that are using the trait. There might seems so many of them but it's only changes to the UI upgrade form (which is split in multiple classes). We are not introducing dependency injection on the whole Migrate Drupal module, or Migrate Drupal UI module, only on the classes that are using the trait.
IS updated.
Comment #10
quietone CreditAttribution: quietone as a volunteer commented@maxocub, I finally read the comments #77 and #78 that suggested making this followup and like you I wasn't sure how it was to work here. However, after talking to you and heddn and reading the patch it makes sense. Thanks!
Aside from all the goodness of dependency injection I find that the Migrate Upgrade UI form classes are tidier and easier to read and that is most welcome. So, RTBC it is.
Comment #11
maxocub CreditAttribution: maxocub as a volunteer commentedThanks @quietone, I also find the new split up upgrade UI form much easier to read.
Added links in the IS to the parent issue's comments where this follow up originated for more clarity.
Comment #12
catchThe setters don't seem necessary here - all the classes being modified use constructor injection, we're not actually using setter injection anywhere. Apart from that looks good.
Comment #13
maxocub CreditAttribution: maxocub commentedRemoved the setters and updated the IS.
Comment #14
catchThanks, looks good.
Comment #15
msankhala CreditAttribution: msankhala as a volunteer and at Srijan | A Material+ Company commentedThanks @maxocub for clarifying in #9.
Comment #17
larowlanThis fails on php 5.5?
Comment #18
longwaveThe problem is that both FormBase and MigrationConfigurationTrait declare $configFactory, and MigrateUpgradeFormBase uses both classes, but overwriting properties with traits is not allowed.
Comment #19
maxocub CreditAttribution: maxocub commentedExactly, but the trait needs the $configFactory and we can't assume that the class using the trait will provide it. I'm trying to think of an alternative, but I can't. Any idea?
Comment #20
longwavePostpone until the 8.8.x branch is open? PHP 5 is going away and #2842431: [policy] Remove PHP 5.5, 5.6 support in Drupal 8.7 states "All currently supported PHP versions will be tested during the 8.7.x development phase prior to 8.7.0, since PHP 5.5 must be supported for Drupal 8.6 backports." - although this will never be backported it will also never pass the 8.7.x tests.
Comment #21
maxocub CreditAttribution: maxocub commentedHaha, great thinking! Since this is not top priority, I guess postponing is a simple and elegant solution. Postponed until 8.8.x.
Comment #23
quietone CreditAttribution: quietone as a volunteer commentedNo longer postponed
Comment #24
quietone CreditAttribution: quietone as a volunteer commentedHere's a reroll.
Comment #25
heddnLooking good. Nice solid PHP practices at work here
Comment #26
larowlanComment #27
larowlanCommitted b6b1358 and pushed to 8.8.x. Thanks!