Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
default_content
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
default_content
Merge requests
!33
Issue
#3359137
: Update inline block usage on import
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Open
Issue
#3359137
: Update inline block usage on import
issue/default_content-3359137:3359137-update-inline-block-usage-on-import
into
2.0.x
Overview
0
Commits
1
Pipelines
0
Changes
2
Open
Issue #3359137: Update inline block usage on import
Florent Torregrosa
requested to merge
issue/default_content-3359137:3359137-update-inline-block-usage-on-import
into
2.0.x
May 9, 2023
Overview
0
Commits
1
Pipelines
0
Changes
2
0
0
Merge request reports
Compare
2.0.x
2.0.x (HEAD)
and
latest version
latest version
26e98262
1 commit,
May 9, 2023
2 files
+
164
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
src/EventSubscriber/InlineBlockUsageFixer.php
0 → 100644
+
156
−
0
View file @ 005ab8c1
Edit in single-file editor
Open in Web IDE
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\default_content\EventSubscriber
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\default_content
\Event\DefaultContentEvents
;
use
Drupal\default_content
\Event\ImportEvent
;
use
Drupal\layout_builder
\InlineBlockUsageInterface
;
use
Drupal\layout_builder
\LayoutEntityHelperTrait
;
use
Drupal\layout_builder
\Plugin\Block\InlineBlock
;
use
Drupal\layout_builder
\SectionStorage\SectionStorageManagerInterface
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
/**
* Fix inline block usage after install.
*
* @see \Drupal\layout_builder\InlineBlockEntityOperations
*/
class
InlineBlockUsageFixer
implements
EventSubscriberInterface
{
use
LayoutEntityHelperTrait
;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected
EntityTypeManagerInterface
$entityTypeManager
;
/**
* Inline block usage tracking service.
*
* @var \Drupal\layout_builder\InlineBlockUsageInterface|null
*/
protected
$usage
;
/**
* The section storage manager.
*
* @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface|null
*/
protected
$sectionStorageManager
;
/**
* The block content storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected
EntityStorageInterface
$blockContentStorage
;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\layout_builder\InlineBlockUsageInterface|null $usage
* Inline block usage tracking service.
* @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface|null $sectionStorageManager
* The section storage manager.
*/
public
function
__construct
(
EntityTypeManagerInterface
$entityTypeManager
,
?InlineBlockUsageInterface
$usage
,
?SectionStorageManagerInterface
$sectionStorageManager
)
{
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
usage
=
$usage
;
$this
->
sectionStorageManager
=
$sectionStorageManager
;
}
/**
* {@inheritdoc}
*/
public
static
function
getSubscribedEvents
()
{
return
[
DefaultContentEvents
::
IMPORT
=>
'initUsage'
,
];
}
/**
* Init inline block layout builder usage for imported entities.
*
* @param \Drupal\default_content\Event\ImportEvent $importEvent
* The default content import event.
*/
public
function
initUsage
(
ImportEvent
$importEvent
):
void
{
// Ensure Layout Builder module is present.
if
(
!
$this
->
sectionStorageManager
instanceof
SectionStorageManagerInterface
||
!
$this
->
usage
instanceof
InlineBlockUsageInterface
)
{
return
;
}
// Ensure Block content module is present.
try
{
$this
->
blockContentStorage
=
$this
->
entityTypeManager
->
getStorage
(
'block_content'
);
}
catch
(
\Exception
$exception
)
{
return
;
}
foreach
(
$importEvent
->
getImportedEntities
()
as
$entity
)
{
$sections
=
$this
->
getEntitySections
(
$entity
);
if
(
$sections
)
{
foreach
(
$this
->
getInlineBlockComponents
(
$sections
)
as
$component
)
{
$plugin
=
$component
->
getPlugin
();
if
(
$plugin
instanceof
InlineBlock
)
{
$block_id
=
$this
->
getPluginBlockId
(
$plugin
);
if
(
$block_id
)
{
$this
->
usage
->
addUsage
(
$block_id
,
$entity
);
}
}
}
}
}
}
/**
* Gets a block ID for an inline block plugin.
*
* @param \Drupal\layout_builder\Plugin\Block\InlineBlock $block_plugin
* The inline block plugin.
*
* @return int|null
* The block content ID or null none available.
*/
protected
function
getPluginBlockId
(
InlineBlock
$block_plugin
)
{
$configuration
=
$block_plugin
->
getConfiguration
();
if
(
!
empty
(
$configuration
[
'block_revision_id'
]))
{
$revision_ids
=
$this
->
getBlockIdsForRevisionIds
([
$configuration
[
'block_revision_id'
]]);
return
\array_pop
(
$revision_ids
);
}
return
NULL
;
}
/**
* Gets blocks IDs for an array of revision IDs.
*
* @param int[] $revision_ids
* The revision IDs.
*
* @return int[]
* The block IDs.
*/
protected
function
getBlockIdsForRevisionIds
(
array
$revision_ids
):
array
{
if
(
$revision_ids
)
{
$query
=
$this
->
blockContentStorage
->
getQuery
()
->
accessCheck
(
FALSE
);
$query
->
condition
(
'revision_id'
,
$revision_ids
,
'IN'
);
$block_ids
=
$query
->
execute
();
return
array_map
(
'intval'
,
$block_ids
);
}
return
[];
}
}
Loading