- Issue created by @srjosh
- Merge request !23Issue #3476300 by srjosh: Passing null to $typedConfigManager in... β (Open) created by srjosh
- First commit to issue fork.
- Merge request !24Issue #3476300 by srjosh, mably: Passing null to $typedConfigManager in... β (Open) created by mably
- π§π¬Bulgaria pfrenssen Sofia
This is looking good. My personal preference is with the original MR https://git.drupalcode.org/project/menu_breadcrumb/-/merge_requests/23 - this will make it easier to make the injected dependencies readonly in the future.
- π«π·France mably
Hi @pfrenssen could you explain what you mean by "make the injected dependencies readonly in the future", please?
- π§π¬Bulgaria pfrenssen Sofia
Once we drop support for Drupal 8 and 9 we can use readonly properties which were introduced in PHP8.1, with this all injected dependencies can be made readonly and you can safeguard against unwanted writes to the properties. I.e. it prevents child classes from messing with the injected dependencies, and allows static analyzers to detect bugs where accidental writes are done to these properties.
Now the constructor looks like this:
protected $entityTypeManager; public function __construct( ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, protected TypedConfigManagerInterface $typedConfigManager ) { parent::__construct($config_factory, $typedConfigManager); $this->entityTypeManager = $entity_type_manager; }
In the future it can look like this (assuming the parent class signature doesn't change).
public function __construct( ConfigFactoryInterface $config_factory, protected readonly EntityTypeManagerInterface $entityTypeManager, protected TypedConfigManagerInterface $typedConfigManager, ) { parent::__construct($config_factory, $typedConfigManager); }
- π«π·France mably
If I understand correctly what is written in this article, read-only properties can be initialized from anywhere inside the class, not only the constructor.
A property declared readonly can be initialized from anywhere inside the class. It makes more sense to initialize read-only properties from the constructor, but it is not required to do so.
So it seems it could be done in the
create
method.Actually the following code seems to work:
/** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected readonly EntityTypeManagerInterface $entityTypeManager; /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { $instance = parent::create($container); $instance->entityTypeManager = $container->get('entity_type.manager'); return $instance; }
- Status changed to Closed: outdated
3 months ago 10:18am 9 January 2025 - π¨πSwitzerland yobottehg Basel
I think this can be closed as this is in dev with https://git.drupalcode.org/project/menu_breadcrumb/-/commit/42bd1aa7bbf2...
- π³πΏNew Zealand xurizaemon Εtepoti, Aotearoa π
I'll mark it fixed then, I _think_ already copied credit over to π Drupal\menu_breadcrumb\Form\SettingsForm is incompatible with ConfigFormBase Active . Thanks!
Automatically closed - issue fixed for 2 weeks with no activity.