Skip to content
Snippets Groups Projects
Commit 97460764 authored by David Suissa's avatar David Suissa
Browse files

Issue #3412951 by DYdave: Automated testing on GitLab CI: Fixed PHPStan errors...

Issue #3412951 by DYdave: Automated testing on GitLab CI: Fixed PHPStan errors 'Unsafe usage of new static().' by declaring plugin constructor methods as 'final' and updated module's documentation to explain how its 'FieldFormatter' plugin classes could be extended.
parent 5a52c7a2
No related branches found
No related tags found
1 merge request!2Issue #3412951 by DYdave: Automated testing on GitLab CI: Fixed PHPStan errors...
Pipeline #72896 passed
......@@ -123,6 +123,83 @@ formatter is a recurring topic).
- Another solution would be to use the [Linked Field][19] module which pretty
much allows linking any field to custom URL targets with Token patterns.
## Extending plugin classes
To prevent issues with modules extending the plugin classes `ImageLinkFormatter`
and/or `ResponsiveImageLinkFormatter`, their constructor methods had to be made
`final`, preventing child classes from overriding their `_construct` methods and
thus disallowing their dependencies from being changed the usual way.\
However, it is still possible to add new dependencies in a child class by using
a `create()` method with the following pattern example:
````php
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var static $plugin */
$plugin = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$plugin->currentUser = $container->get('current_user');
return $plugin;
}
````
Better yet, instead of using a direct assignment to inject a dependency, it is
recommended to add a set of `set` and `get` methods for the corresponding
service in the extending plugin class, modifying our example above with the
following pattern:
````php
/**
* Returns the current user to use for this plugin.
*
* @return \Drupal\Core\Session\AccountProxyInterface
* The current user.
*/
public function getCurrentUser() {
return $this->currentUser ?: \Drupal::currentUser();
}
/**
* Sets the current user to use for this plugin.
*
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user to use for this plugin.
*
* @return $this
*/
public function setCurrentUser(AccountProxyInterface $current_user) {
$this->currentUser = $current_user;
return $this;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var static $plugin */
$plugin = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$plugin->setCurrentUser($container->get('current_user'));
return $plugin;
}
````
This allows the child class to be able to utilize the parent `create()` method
while also adding new dependencies to the child class without calling or
overwriting the constructor of the parent class.\
For more information on this particular issue, see ticket #3412951.
## Support and maintenance
Releases of the module can be requested or will generally be created based on
......
......@@ -71,7 +71,7 @@ class ResponsiveImageLinkFormatter extends ResponsiveImageFormatter implements C
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage, LinkGeneratorInterface $link_generator, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager) {
final public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage, LinkGeneratorInterface $link_generator, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $responsive_image_style_storage, $image_style_storage, $link_generator, $current_user);
$this->entityFieldManager = $entity_field_manager;
}
......
......@@ -67,7 +67,7 @@ class ImageLinkFormatter extends ImageFormatter implements ContainerFactoryPlugi
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, FileUrlGeneratorInterface $file_url_generator, EntityFieldManagerInterface $entity_field_manager) {
final public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, FileUrlGeneratorInterface $file_url_generator, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage, $file_url_generator);
$this->entityFieldManager = $entity_field_manager;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment