Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DisplayBuilderViewsHook | |
0.00% |
0 / 24 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| entityUpdate | |
0.00% |
0 / 23 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder_views\Hook; |
| 6 | |
| 7 | use Drupal\Core\Entity\EntityInterface; |
| 8 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 9 | use Drupal\Core\Hook\Attribute\Hook; |
| 10 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 11 | use Drupal\display_builder\DisplayBuildablePluginManager; |
| 12 | use Drupal\views\Entity\View; |
| 13 | |
| 14 | /** |
| 15 | * Hook implementations for the display_builder_views module. |
| 16 | */ |
| 17 | class DisplayBuilderViewsHook { |
| 18 | |
| 19 | public function __construct( |
| 20 | protected EntityTypeManagerInterface $entityTypeManager, |
| 21 | protected DisplayBuildablePluginManager $displayBuildableManager, |
| 22 | ) {} |
| 23 | |
| 24 | /** |
| 25 | * Implements hook_entity_update(). |
| 26 | * |
| 27 | * @param \Drupal\Core\Entity\EntityInterface $entity |
| 28 | * The entity to be updated. |
| 29 | */ |
| 30 | #[Hook('entity_update')] |
| 31 | public function entityUpdate(EntityInterface $entity): void { |
| 32 | if (!$entity instanceof View) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // Runs during config import. |
| 37 | if (!$entity->isSyncing()) { |
| 38 | return; |
| 39 | } |
| 40 | $view = $entity->getExecutable(); |
| 41 | |
| 42 | foreach ($entity->get('display') as $display_id => $display) { |
| 43 | $view->setDisplay($display_id); |
| 44 | $extenders = $view->getDisplay()->getExtenders(); |
| 45 | |
| 46 | if (!isset($extenders['display_builder'])) { |
| 47 | continue; |
| 48 | } |
| 49 | $extender = $extenders['display_builder']; |
| 50 | /** @var \Drupal\display_builder\DisplayBuildableInterface $buildable */ |
| 51 | $buildable = $this->displayBuildableManager->createInstance('view_display', [ |
| 52 | 'extender' => $extender, |
| 53 | ]); |
| 54 | |
| 55 | $sources = $buildable->getSources(); |
| 56 | $instance_id = $buildable->getInstanceId(); |
| 57 | |
| 58 | /** @var \Drupal\display_builder\InstanceInterface|null $instance */ |
| 59 | $instance = $this->entityTypeManager |
| 60 | ->getStorage('display_builder_instance') |
| 61 | ->load($instance_id); |
| 62 | |
| 63 | // Reset the state once you import a configuration. |
| 64 | if ($instance) { |
| 65 | $log = new TranslatableMarkup('Synchronize display from imported configuration'); |
| 66 | $instance->setNewPresent($sources, $log); |
| 67 | $instance->save(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | } |