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 possible
  • Write the patch
  • Review

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maxocub created an issue. See original summary.

catch’s picture

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

maxocub’s picture

Title: Let's see if we can use dependency injection in MigrationConfigurationTrait » Use dependency injection in MigrationConfigurationTrait
Issue summary: View changes
Status: Active » Needs review
FileSize
20.24 KB

Here's a first patch.

quietone’s picture

Assigned: Unassigned » quietone

Assigning to self for review

quietone’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

No longer applies, needs a reroll

quietone’s picture

Assigned: quietone » Unassigned
maxocub’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
20.56 KB

Here's the re-roll.

msankhala’s picture

Status: Needs review » Needs work

According 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 in migrate_drupal and migrate_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 in migrate_drupal_ui module. Rename this issue to Use dependency injection in migrate_drupal module

maxocub’s picture

Issue summary: View changes
Status: Needs work » Needs review

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

quietone’s picture

Status: Needs review » Reviewed & tested by the community

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

maxocub’s picture

Issue summary: View changes

Thanks @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.

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeFormBase.php
@@ -25,10 +28,19 @@
+  public function __construct(ConfigFactoryInterface $config_factory, MigrationPluginManagerInterface $migration_plugin_manager, StateInterface $state, PrivateTempStoreFactory $tempstore_private) {
+    $this->setConfigFactory($config_factory);
+    $this->setMigrationPluginManager($migration_plugin_manager);
+    $this->setState($state);

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

maxocub’s picture

Removed the setters and updated the IS.

catch’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, looks good.

msankhala’s picture

Thanks @maxocub for clarifying in #9.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

This fails on php 5.5?

longwave’s picture

The problem is that both FormBase and MigrationConfigurationTrait declare $configFactory, and MigrateUpgradeFormBase uses both classes, but overwriting properties with traits is not allowed.

maxocub’s picture

Exactly, 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?

longwave’s picture

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

maxocub’s picture

Status: Needs work » Postponed

Haha, great thinking! Since this is not top priority, I guess postponing is a simple and elegant solution. Postponed until 8.8.x.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

quietone’s picture

Status: Postponed » Needs review

No longer postponed

quietone’s picture

Here's a reroll.

heddn’s picture

Status: Needs review » Reviewed & tested by the community

Looking good. Nice solid PHP practices at work here

larowlan’s picture

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed b6b1358 and pushed to 8.8.x. Thanks!

  • larowlan committed b6b1358 on 8.8.x
    Issue #2961226 by maxocub, quietone, catch: Use dependency injection in...

Status: Fixed » Closed (fixed)

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