Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| FieldConfigHooks | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fieldConfigCreate | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 | |||
| fieldConfigDelete | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Hook; |
| 6 | |
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; |
| 8 | use Drupal\Core\Hook\Attribute\Hook; |
| 9 | use Drupal\field\FieldConfigInterface; |
| 10 | |
| 11 | /** |
| 12 | * Hook implementations that track the preferred user name field in config. |
| 13 | * |
| 14 | * @internal |
| 15 | */ |
| 16 | final class FieldConfigHooks { |
| 17 | |
| 18 | public function __construct( |
| 19 | private readonly ConfigFactoryInterface $configFactory, |
| 20 | ) {} |
| 21 | |
| 22 | /** |
| 23 | * Implements hook_ENTITY_TYPE_create() for field_config entities. |
| 24 | */ |
| 25 | // phpcs:ignore Drupal.Commenting.PostStatementComment.Found -- #[Hook] is a PHP attribute, not a trailing comment. |
| 26 | #[Hook('field_config_create')] // @phpstan-ignore attribute.notFound |
| 27 | public function fieldConfigCreate(FieldConfigInterface $entity): void { |
| 28 | $should_skip = ( |
| 29 | $entity->isSyncing() |
| 30 | || $entity->getTargetEntityTypeId() !== 'user' |
| 31 | || $entity->getTargetBundle() !== 'user' |
| 32 | || $entity->getType() !== 'name' |
| 33 | ); |
| 34 | if ($should_skip) { |
| 35 | return; |
| 36 | } |
| 37 | if ($this->configFactory->get('name.settings')->get('user_preferred') !== '') { |
| 38 | return; |
| 39 | } |
| 40 | $this->configFactory |
| 41 | ->getEditable('name.settings') |
| 42 | ->set('user_preferred', $entity->getName()) |
| 43 | ->save(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Implements hook_ENTITY_TYPE_delete() for field_config entities. |
| 48 | */ |
| 49 | // phpcs:ignore Drupal.Commenting.PostStatementComment.Found -- #[Hook] is a PHP attribute, not a trailing comment. |
| 50 | #[Hook('field_config_delete')] // @phpstan-ignore attribute.notFound |
| 51 | public function fieldConfigDelete(FieldConfigInterface $entity): void { |
| 52 | $should_skip = ( |
| 53 | $entity->isSyncing() |
| 54 | || $entity->getTargetEntityTypeId() !== 'user' |
| 55 | || $entity->getTargetBundle() !== 'user' |
| 56 | ); |
| 57 | if ($should_skip) { |
| 58 | return; |
| 59 | } |
| 60 | if ($this->configFactory->get('name.settings')->get('user_preferred') !== $entity->getName()) { |
| 61 | return; |
| 62 | } |
| 63 | $this->configFactory |
| 64 | ->getEditable('name.settings') |
| 65 | ->set('user_preferred', '') |
| 66 | ->save(); |
| 67 | } |
| 68 | |
| 69 | } |