comparison core/modules/layout_builder/src/InlineBlockEntityOperations.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; 6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Entity\EntityInterface; 7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface; 8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\RevisionableInterface; 9 use Drupal\Core\Entity\RevisionableInterface;
10 use Drupal\layout_builder\Plugin\Block\InlineBlock; 10 use Drupal\layout_builder\Plugin\Block\InlineBlock;
11 use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface; 12 use Symfony\Component\DependencyInjection\ContainerInterface;
12 13
13 /** 14 /**
14 * Defines a class for reacting to entity events related to Inline Blocks. 15 * Defines a class for reacting to entity events related to Inline Blocks.
15 * 16 *
16 * @internal 17 * @internal
18 * This is an internal utility class wrapping hook implementations.
17 */ 19 */
18 class InlineBlockEntityOperations implements ContainerInjectionInterface { 20 class InlineBlockEntityOperations implements ContainerInjectionInterface {
19 21
20 use LayoutEntityHelperTrait; 22 use LayoutEntityHelperTrait;
21 23
22 /** 24 /**
23 * Inline block usage tracking service. 25 * Inline block usage tracking service.
24 * 26 *
25 * @var \Drupal\layout_builder\InlineBlockUsage 27 * @var \Drupal\layout_builder\InlineBlockUsageInterface
26 */ 28 */
27 protected $usage; 29 protected $usage;
28 30
29 /** 31 /**
30 * The block content storage. 32 * The block content storage.
40 */ 42 */
41 protected $entityTypeManager; 43 protected $entityTypeManager;
42 44
43 /** 45 /**
44 * Constructs a new EntityOperations object. 46 * Constructs a new EntityOperations object.
47 *
48 * @todo This constructor has one optional parameter, $section_storage_manager
49 * and one totally unused $database parameter. Deprecate the current
50 * constructor signature in https://www.drupal.org/node/3031492 after the
51 * general policy for constructor backwards compatibility is determined in
52 * https://www.drupal.org/node/3030640.
45 * 53 *
46 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager 54 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
47 * The entity type manager service. 55 * The entity type manager service.
48 * @param \Drupal\layout_builder\InlineBlockUsage $usage 56 * @param \Drupal\layout_builder\InlineBlockUsageInterface $usage
49 * Inline block usage tracking service. 57 * Inline block usage tracking service.
50 * @param \Drupal\Core\Database\Connection $database 58 * @param \Drupal\Core\Database\Connection $database
51 * The database connection. 59 * The database connection.
52 */ 60 * @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager
53 public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsage $usage, Connection $database) { 61 * (optional) The section storage manager.
62 *
63 * @todo The current constructor signature is deprecated:
64 * - The $section_storage_manager parameter is optional, but should become
65 * required.
66 * - The $database parameter is unused and should be removed.
67 * Deprecate in https://www.drupal.org/node/3031492.
68 */
69 public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsageInterface $usage, Connection $database, SectionStorageManagerInterface $section_storage_manager = NULL) {
54 $this->entityTypeManager = $entityTypeManager; 70 $this->entityTypeManager = $entityTypeManager;
55 $this->blockContentStorage = $entityTypeManager->getStorage('block_content'); 71 $this->blockContentStorage = $entityTypeManager->getStorage('block_content');
56 $this->usage = $usage; 72 $this->usage = $usage;
73 if ($section_storage_manager === NULL) {
74 @trigger_error('The plugin.manager.layout_builder.section_storage service must be passed to \Drupal\layout_builder\InlineBlockEntityOperations::__construct(). It was added in Drupal 8.7.0 and will be required before Drupal 9.0.0.', E_USER_DEPRECATED);
75 $section_storage_manager = \Drupal::service('plugin.manager.layout_builder.section_storage');
76 }
77 $this->sectionStorageManager = $section_storage_manager;
57 } 78 }
58 79
59 /** 80 /**
60 * {@inheritdoc} 81 * {@inheritdoc}
61 */ 82 */
62 public static function create(ContainerInterface $container) { 83 public static function create(ContainerInterface $container) {
63 return new static( 84 return new static(
64 $container->get('entity_type.manager'), 85 $container->get('entity_type.manager'),
65 $container->get('inline_block.usage'), 86 $container->get('inline_block.usage'),
66 $container->get('database') 87 $container->get('database'),
88 $container->get('plugin.manager.layout_builder.section_storage')
67 ); 89 );
68 } 90 }
69 91
70 /** 92 /**
71 * Remove all unused inline blocks on save. 93 * Remove all unused inline blocks on save.
83 // could be referenced in previous revisions even if this is not a new 105 // could be referenced in previous revisions even if this is not a new
84 // revision. 106 // revision.
85 if ($entity->isNew() || !isset($entity->original) || $entity instanceof RevisionableInterface) { 107 if ($entity->isNew() || !isset($entity->original) || $entity instanceof RevisionableInterface) {
86 return; 108 return;
87 } 109 }
88 $sections = $this->getEntitySections($entity); 110 // If the original entity used the default storage then we cannot remove
89 // If this is a layout override and there are no sections then it is a new 111 // unused inline blocks because they will still be referenced in the
90 // override. 112 // defaults.
91 if ($this->isEntityUsingFieldOverride($entity) && empty($sections)) { 113 if ($this->originalEntityUsesDefaultStorage($entity)) {
92 return; 114 return;
93 } 115 }
94 116
95 // Delete and remove the usage for inline blocks that were removed. 117 // Delete and remove the usage for inline blocks that were removed.
96 if ($removed_block_ids = $this->getRemovedBlockIds($entity)) { 118 if ($removed_block_ids = $this->getRemovedBlockIds($entity)) {
130 * 152 *
131 * @param \Drupal\Core\Entity\EntityInterface $entity 153 * @param \Drupal\Core\Entity\EntityInterface $entity
132 * The parent entity. 154 * The parent entity.
133 */ 155 */
134 public function handleEntityDelete(EntityInterface $entity) { 156 public function handleEntityDelete(EntityInterface $entity) {
135 if ($this->isLayoutCompatibleEntity($entity)) { 157 // @todo In https://www.drupal.org/node/3008943 call
136 $this->usage->removeByLayoutEntity($entity); 158 // \Drupal\layout_builder\LayoutEntityHelperTrait::isLayoutCompatibleEntity().
137 } 159 $this->usage->removeByLayoutEntity($entity);
138 } 160 }
139 161
140 /** 162 /**
141 * Handles saving a parent entity. 163 * Handles saving a parent entity.
142 * 164 *
148 return; 170 return;
149 } 171 }
150 $duplicate_blocks = FALSE; 172 $duplicate_blocks = FALSE;
151 173
152 if ($sections = $this->getEntitySections($entity)) { 174 if ($sections = $this->getEntitySections($entity)) {
153 if ($this->isEntityUsingFieldOverride($entity)) { 175 if ($this->originalEntityUsesDefaultStorage($entity)) {
154 if (!$entity->isNew() && isset($entity->original)) { 176 // This is a new override from a default and the blocks need to be
155 if (empty($this->getEntitySections($entity->original))) { 177 // duplicated.
156 // If there were no sections in the original entity then this is a 178 $duplicate_blocks = TRUE;
157 // new override from a default and the blocks need to be duplicated.
158 $duplicate_blocks = TRUE;
159 }
160 }
161 } 179 }
162 $new_revision = FALSE; 180 $new_revision = FALSE;
163 if ($entity instanceof RevisionableInterface) { 181 if ($entity instanceof RevisionableInterface) {
164 // If the parent entity will have a new revision create a new revision 182 // If the parent entity will have a new revision create a new revision
165 // of the block. 183 // of the block.