Skip to content

Commit b627623

Browse files
committed
change entity type parent to determine if have parent
1 parent 0524ae0 commit b627623

10 files changed

Lines changed: 100 additions & 50 deletions

File tree

core/modules/block_content/block_content.module

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function block_content_query_entity_reference_alter(AlterableInterface $query) {
132132
$data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
133133
if (array_key_exists($data_table, $query->getTables()) && !_block_content_has_parent_entity_condition($query->conditions())) {
134134
// If no parent entity condition create a condition.
135-
$query->isNull("$data_table.parent_entity_id");
135+
$query->isNull("$data_table.parent_entity_type");
136136
}
137137
}
138138
}

core/modules/block_content/block_content.post_update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function block_content_post_update_add_views_parent_filter() {
3131
->set("$base.field", "has_parent")
3232
->set("$base.value", '0')
3333
->set("$base.entity_type", "block_content")
34-
->set("$base.entity_field", "parent_entity_id");
34+
->set("$base.entity_field", "parent_entity_type");
3535
}
3636
}
3737
$view->save();

core/modules/block_content/config/optional/views.view.block_content.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ display:
467467
default_group_multiple: { }
468468
group_items: { }
469469
entity_type: block_content
470-
entity_field: parent_entity_id
470+
entity_field: parent_entity_type
471471
plugin_id: boolean_string
472472
sorts: { }
473473
title: 'Custom block library'

core/modules/block_content/src/BlockContentListBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function buildRow(EntityInterface $entity) {
3434
protected function getEntityIds() {
3535
$query = $this->getStorage()->getQuery()
3636
->sort($this->entityType->getKey('id'))
37-
->notExists('parent_entity_id');
37+
->notExists('parent_entity_type');
3838

3939
// Only add the pager if a limit is specified.
4040
if ($this->limit) {

core/modules/block_content/src/BlockContentViewsData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function getViewsData() {
2929
'id' => 'boolean_string',
3030
'accept_null' => TRUE,
3131
],
32-
'entity field' => 'parent_entity_id',
33-
'real field' => 'parent_entity_id',
32+
'entity field' => 'parent_entity_type',
33+
'real field' => 'parent_entity_type',
3434
];
3535

3636
$data['block_content_field_data']['type']['field']['id'] = 'field';

core/modules/block_content/src/Entity/BlockContent.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function getTheme() {
119119
*/
120120
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
121121
parent::postSave($storage, $update);
122-
if (empty($this->get('parent_entity_id')->value) || (isset($this->original) && empty($this->original->get('parent_entity_id')->value))) {
122+
if (empty($this->get('parent_entity_type')->value) || (isset($this->original) && empty($this->original->get('parent_entity_type')->value))) {
123123
static::invalidateBlockPluginCache();
124124
}
125125
}
@@ -210,6 +210,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
210210
->setTranslatable(TRUE)
211211
->setRevisionable(TRUE);
212212

213+
// @todo Is there a core issue to add
214+
// https://www.drupal.org/project/dynamic_entity_reference
213215
$fields['parent_entity_type'] = BaseFieldDefinition::create('string')
214216
->setLabel(t('Parent entity type'))
215217
->setDescription(t('The parent entity type.'))
@@ -348,6 +350,8 @@ public function removeParentEntity() {
348350
* {@inheritdoc}
349351
*/
350352
public function hasParentEntity() {
351-
return !empty($this->get('parent_entity_type')->value) && !empty($this->get('parent_entity_id')->value);
353+
// If either parent field value is set then the block is considered to have
354+
// a parent.
355+
return !empty($this->get('parent_entity_type')->value) || !empty($this->get('parent_entity_id')->value);
352356
}
353357
}

core/modules/block_content/src/Plugin/Derivative/BlockContent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
4343
* {@inheritdoc}
4444
*/
4545
public function getDerivativeDefinitions($base_plugin_definition) {
46-
$block_ids = $this->blockContentStorage->getQuery()->notExists('parent_entity_id')->execute();
46+
$block_ids = $this->blockContentStorage->getQuery()->notExists('parent_entity_type')->execute();
4747
$block_contents = $this->blockContentStorage->loadMultiple($block_ids);
4848
// Reset the discovered definitions.
4949
$this->derivatives = [];

core/modules/block_content/src/Plugin/views/wizard/BlockContent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getFilters() {
2828
'operator' => '=',
2929
'value' => '0',
3030
'entity_type' => $this->entityTypeId,
31-
'entity_field' => 'parent_entity_id',
31+
'entity_field' => 'parent_entity_type',
3232
];
3333
return $filters;
3434
}

core/modules/block_content/tests/src/Functional/Views/BlockContentWizardTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testViewAddBlockContent() {
4444
$display_options = $view->getDisplay('default')['display_options'];
4545

4646
$this->assertEquals('block_content', $display_options['filters']['has_parent']['entity_type']);
47-
$this->assertEquals('parent_entity_id', $display_options['filters']['has_parent']['entity_field']);
47+
$this->assertEquals('parent_entity_type', $display_options['filters']['has_parent']['entity_field']);
4848
$this->assertEquals('boolean_string', $display_options['filters']['has_parent']['plugin_id']);
4949
$this->assertEquals('0', $display_options['filters']['has_parent']['value']);
5050
}

core/modules/layout_builder/src/EntityOperations.php

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Database\Database;
77
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
88
use Drupal\Core\Entity\EntityInterface;
9+
use Drupal\Core\Entity\EntityTypeInterface;
910
use Drupal\Core\Entity\EntityTypeManagerInterface;
1011
use Drupal\Core\Entity\FieldableEntityInterface;
1112
use Drupal\Core\Entity\RevisionableInterface;
@@ -102,13 +103,17 @@ protected function removeUnusedForEntityOnSave(EntityInterface $entity) {
102103
*
103104
* @param \Drupal\Core\Entity\EntityInterface $entity
104105
* The parent entity.
106+
*
107+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
108+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
109+
* @throws \Drupal\Core\Entity\EntityStorageException
105110
*/
106111
public function handleEntityDelete(EntityInterface $entity) {
107112
if ($this->isStorageAvailable() && $this->isLayoutCompatibleEntity($entity)) {
108-
$entity_type = $this->entityTypeManager->getDefinition($entity->getEntityTypeId());
109-
if (!$entity_type->getDataTable()) {
110-
// If the entity type does not have a data table we cannot find unused
111-
// blocks on cron.
113+
if (!$this->isUsingDataTables($entity->getEntityTypeId())) {
114+
// If either entity type does not have a data table we cannot find
115+
// unused blocks in '::removeUnused()'.
116+
// @see ::getUnusedBlockIdsForEntityWithDataTable
112117
$block_storage = $this->entityTypeManager->getStorage('block_content');
113118
$query = $block_storage->getQuery();
114119
$query->condition('parent_entity_id', $entity->id());
@@ -278,6 +283,8 @@ protected function deleteBlocks(array $block_content_ids) {
278283
* @param int $limit
279284
* The maximum number of block content entities to remove.
280285
*
286+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
287+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
281288
* @throws \Drupal\Core\Entity\EntityStorageException
282289
*/
283290
public function removeUnused($limit = 100) {
@@ -347,47 +354,86 @@ protected function getBlockIdsForRevisionIds(array $revision_ids) {
347354
*
348355
* @return int[]
349356
* The block IDs.
357+
*
358+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
359+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
350360
*/
351361
protected function getUnused($limit) {
352-
$block_type_definition = $this->entityTypeManager->getDefinition('block_content');
353-
$data_table = $block_type_definition->getDataTable();
354-
$query = Database::getConnection()->select($data_table);
355-
$query->distinct(TRUE);
356-
$query->isNotNull('parent_entity_type');
357-
$query->fields($data_table, ['parent_entity_type']);
358-
$parent_entity_types = $query->execute()->fetchCol();
359-
$block_id_key = $block_type_definition->getKey('id');
360362
$block_ids = [];
361-
foreach ($parent_entity_types as $parent_entity_type) {
362-
$parent_type_definition = $this->entityTypeManager->getDefinition($parent_entity_type);
363-
if ($parent_data_table = $parent_type_definition->getDataTable()) {
364-
$sub_query = Database::getConnection()->select($parent_data_table, 'parent');
365-
$parent_id_key = $parent_type_definition->getKey('id');
366-
$sub_query->fields('parent', [$parent_id_key]);
367-
$sub_query->where("blocks.parent_entity_id = parent.$parent_id_key");
368-
369-
$query = Database::getConnection()->select($data_table, 'blocks');
370-
$query->fields('blocks', [$block_id_key]);
371-
$query->isNotNull('parent_entity_id');
372-
$query->condition('parent_entity_type', $parent_entity_type);
373-
$query->notExists($sub_query);
374-
$query->range(0, $limit - count($block_ids));
375-
$new_block_ids = $query->execute()->fetchCol();
376-
}
377-
else {
378-
// @todo Handle parent types with no data table.
379-
$block_query = $this->entityTypeManager->getStorage('block_content')->getQuery();
380-
$block_query->condition('parent_entity_type', $parent_entity_type);
381-
$block_query->notExists('parent_entity_id');
382-
$block_query->range(0, $limit - count($block_ids));
383-
$new_block_ids = $block_query->execute();
384-
}
385-
$block_ids = array_merge($block_ids, $new_block_ids);
386-
if (count($block_ids) > 50) {
387-
break;
363+
foreach ($this->entityTypeManager->getDefinitions() as $definition) {
364+
if ($this->entityTypeSupportsLayouts($definition)) {
365+
if ($this->isUsingDataTables($definition->id())) {
366+
$new_block_ids = $this->getUnusedBlockIdsForEntityWithDataTable($limit - count($block_ids), $definition);
367+
}
368+
else {
369+
$block_query = $this->entityTypeManager->getStorage('block_content')->getQuery();
370+
$block_query->condition('parent_entity_type', $definition->id());
371+
$block_query->notExists('parent_entity_id');
372+
$block_query->range(0, $limit - count($block_ids));
373+
$new_block_ids = $block_query->execute();
374+
}
375+
$block_ids = array_merge($block_ids, $new_block_ids);
376+
if (count($block_ids) >= $limit) {
377+
break;
378+
}
388379
}
380+
389381
}
390382
return $block_ids;
391383
}
392384

385+
/**
386+
* Gets the unused block IDs for an entity type using a datatable.
387+
*
388+
* @param int $limit
389+
* The limit of number of block IDs to retrieve.
390+
* @param \Drupal\Core\Entity\EntityTypeInterface $parent_type_definition
391+
* The parent entity type definition.
392+
*
393+
* @return int[]
394+
* The block IDs.
395+
*/
396+
protected function getUnusedBlockIdsForEntityWithDataTable($limit, EntityTypeInterface $parent_type_definition) {
397+
$block_type_definition = $this->entityTypeManager->getDefinition('block_content');
398+
$sub_query = Database::getConnection()
399+
->select($parent_type_definition->getDataTable(), 'parent');
400+
$parent_id_key = $parent_type_definition->getKey('id');
401+
$sub_query->fields('parent', [$parent_id_key]);
402+
$sub_query->where("blocks.parent_entity_id = parent.$parent_id_key");
403+
404+
$query = Database::getConnection()->select($block_type_definition->getDataTable(), 'blocks');
405+
$query->fields('blocks', [$block_type_definition->getKey('id')]);
406+
$query->isNotNull('parent_entity_id');
407+
$query->condition('parent_entity_type', $parent_type_definition->id());
408+
$query->notExists($sub_query);
409+
$query->range(0, $limit);
410+
return $query->execute()->fetchCol();
411+
}
412+
413+
/**
414+
* Determines if parent entity and 'block_content' type are using datatables.
415+
*
416+
* @param string $parent_entity_type_id
417+
* The parent entity type.
418+
*
419+
* @return bool
420+
* TRUE if the parent entity and 'block_content' are using datatables.
421+
*/
422+
protected function isUsingDataTables($parent_entity_type_id) {
423+
return $this->entityTypeManager->getDefinition($parent_entity_type_id)->getDataTable() && $this->entityTypeManager->getDefinition('block_content')->getDataTable();
424+
}
425+
426+
/**
427+
* Checks if the entity type supports layouts.
428+
*
429+
* @param \Drupal\Core\Entity\EntityTypeInterface $definition
430+
* The entity type definition.
431+
*
432+
* @return bool
433+
* TRUE if the entity type supports layouts.
434+
*/
435+
protected function entityTypeSupportsLayouts(EntityTypeInterface $definition) {
436+
return $definition->id() === 'entity_view_display' || array_search(FieldableEntityInterface::class, class_implements($definition->getClass())) !== FALSE;
437+
}
438+
393439
}

0 commit comments

Comments
 (0)