Problem/Motivation

Add object that uses the FactoryInterface should have dependencies inject via ::create and not ::__construct(). This allows parent classes to change their constructor without breaking any code.
See https://events.drupal.org/seattle2019/sessions/drupal-9-coming-getting-y...
See https://www.drupal.org/core/d8-bc-policy

Example: \Drupal\config_sync\Controller\ConfigSyncController::create
See https://www.youtube.com/watch?time_continue=1554&v=hN9KjaBvAUk
See https://www.previousnext.com.au/blog/safely-extending-drupal-8-plugin-cl...

Proposed resolution

Fix subclassing and stop overriding constructors.

Comments

thalles created an issue. See original summary.

thalles’s picture

gausarts’s picture

Status: Needs review » Needs work

Awesome. Thanks!
You might want to move the basic setters (::setFormatter(), ::setManager(), ::setImageFactory()) into https://git.drupalcode.org/project/slick/blob/8.x-2.x/src/Plugin/Field/F...

This way the rest should be able to call them without redefining.

Then please replace the references to the following at other patches:

+    $instance->formatter = $container->get('slick.formatter');
+    $instance->manager = $container->get('slick.manager');

with below setters:

+    $instance->setFormatter($container->get('slick.formatter'));
+    $instance->setManager($container->get('slick.manager'));

The $instance->setFormatter(), $instance->setImageFactory() and few more similar setters can be removed later once Blazy has them.

The same DRY should apply if you use setters for $instance->imageFactory, $instance->blazyEntity, $instance->blazyOembed, I think. And later they can be removed, too. These are still/ only needed during transition, though.

Alternatively create a new method to not repeat, but optimization can be ignored for now.
Feel free to commit it after this change to the rest. Thanks again!

gausarts’s picture

Oh, please also consider protected not public visibility for these setters.

gausarts’s picture

One more, please. You might want to return the instance for all setters like below:

protected function setManager(SlickManagerInterface $manager) {
  $this->manager = $manager;
  return $this;
}

For chainability like at most OO languages.

gausarts’s picture

I just tested your patches.

It turned out setters give more trouble given different situations, either before or after blazy provides them. Setters are only good in trickle down approach, not the reverse like during this transition.

Conclusion:
1. Abandon my idea about setters at #5. I will mark them deleted.
2. Stick to your approach. Yours is safer for now.

Later when all is settled, we can start implementing setters at blazy for most similar services, then at sub-classes for anything else.

Thanks!

gausarts’s picture

Assigned: thalles » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.61 KB

Let's see.

  • gausarts committed a522b3a on 8.x-2.x authored by thalles
    Issue #3113224 by thalles, gausarts: Fix subclassing and stop overriding...
gausarts’s picture

Status: Needs review » Fixed

Let's get the ball rolling, we have another release to catch up. Any (side) issue and or improvements may follow as always.

Committed. Thank you for contribution!

thalles’s picture

Thanks @gausarts!

Status: Fixed » Closed (fixed)

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