Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
section_library
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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
section_library
Commits
10d64410
Commit
10d64410
authored
3 years ago
by
Mahmoud Zayed
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3226370
by mahmoud-zayed: Block with paragraphs does not clone correctly
parent
66075b81
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/DeepCloningTrait.php
+63
-9
63 additions, 9 deletions
src/DeepCloningTrait.php
src/SectionLibraryTemplateListBuilder.php
+1
-1
1 addition, 1 deletion
src/SectionLibraryTemplateListBuilder.php
with
64 additions
and
10 deletions
src/DeepCloningTrait.php
+
63
−
9
View file @
10d64410
...
...
@@ -15,12 +15,11 @@ trait DeepCloningTrait {
* The allowed types for deep cloning.
*
* @return array
* Array of en
i
tiy types ids.
* Array of enti
t
y types ids.
*/
protected
function
getAllowedTypes
()
{
return
[
'block_content'
,
'paragraph'
,
];
}
...
...
@@ -125,7 +124,7 @@ trait DeepCloningTrait {
}
/**
* Clone entity refernced entites.
* Clone entity refer
e
nced entit
i
es.
*
* @param object $entity
* The entity we like duplicate its ReferencedEntities.
...
...
@@ -134,14 +133,22 @@ trait DeepCloningTrait {
$entity_fields
=
$entity
->
getFields
();
foreach
(
$entity_fields
as
$field_key
=>
$entity_field
)
{
$value
=
$entity_field
->
getValue
();
// Handle Paragraph as special case as it does not support
// referencedEntities(), check issue #3089724, also it uses revsion id.
$this
->
cloneReferencedParagraphsEntities
(
$field_key
,
$entity_field
,
$entity
);
// The general rule for any entity.
if
(
$entity_field
->
getName
()
!=
'type'
&&
isset
(
$value
[
0
][
'target_id'
]))
{
$target_entity
=
$entity_field
->
getDataDefinition
()
->
getTargetEntityTypeId
();
if
(
in_array
(
$target_entity
,
$this
->
getAllowedTypes
()))
{
// Create a duplicate entity reference and replace
// the current target ids with the new entites.
$new_referenced_target_ids
=
[];
foreach
(
$entity_field
->
referencedEntities
()
as
$entity_reference
)
{
$referenced_entities
=
$entity_field
->
referencedEntities
();
// Create a duplicate entity reference and replace
// the current target ids with the new entites.
$new_referenced_target_ids
=
[];
if
(
in_array
(
$target_entity
,
$this
->
getAllowedTypes
())
&&
(
!
$entity_field
->
getSetting
(
'target_type'
)
||
$entity_field
->
getSetting
(
'target_type'
)
!=
'paragraph'
))
{
foreach
(
$referenced_entities
as
$entity_reference
)
{
// Skip any items not included in getAllowedTypes method.
// such as User, Media, Taxonomy term, Node...etc.
if
(
!
in_array
(
$entity_reference
->
getEntityTypeId
(),
$this
->
getAllowedTypes
()))
{
...
...
@@ -155,9 +162,56 @@ trait DeepCloningTrait {
// Recursive call.
$this
->
cloneReferencedEntities
(
$new_entity_reference
);
}
}
if
(
!
empty
(
$new_referenced_target_ids
))
{
$entity
->
set
(
$field_key
,
$new_referenced_target_ids
);
}
}
}
}
/**
* Clone paragraphs referenced entities.
*
* @param string $field_key
* The field machine name.
* @param object $entity_field
* The field object.
* @param object $entity
* The original entity object.
*/
protected
function
cloneReferencedParagraphsEntities
(
$field_key
,
$entity_field
,
&
$entity
)
{
$value
=
$entity_field
->
getValue
();
$new_value
=
[];
// Paragraphs Classic, EXPERIMENTAL and IEF - simple form mode structure.
if
(
isset
(
$value
[
0
][
'entity'
]))
{
foreach
(
$value
as
$key
=>
$item
)
{
if
(
$item
[
'entity'
])
{
$new_paragraph_entity
=
$item
[
'entity'
]
->
createDuplicate
();
$new_value
[
$key
][
'entity'
]
=
$new_paragraph_entity
;
}
}
}
// IEF - complex form mode structure.
elseif
(
$entity_field
->
getName
()
!=
'type'
&&
isset
(
$value
[
0
][
'target_id'
])
&&
$entity_field
->
getSetting
(
'target_type'
)
==
'paragraph'
)
{
foreach
(
$value
as
$key
=>
$item
)
{
$paragraph
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'paragraph'
)
->
load
(
$value
[
$key
][
'target_id'
]);
$new_paragraph
=
$paragraph
->
createDuplicate
();
// It's important to save the entity in the IEF - complex case.
$new_paragraph
->
save
();
$new_value
[
$key
][
'target_id'
]
=
$new_paragraph
->
id
();
$new_value
[
$key
][
'target_revision_id'
]
=
$new_paragraph
->
getRevisionId
();
}
}
if
(
!
empty
(
$new_value
))
{
$entity
->
set
(
$field_key
,
$new_value
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/SectionLibraryTemplateListBuilder.php
+
1
−
1
View file @
10d64410
...
...
@@ -26,7 +26,7 @@ class SectionLibraryTemplateListBuilder extends EntityListBuilder {
* {@inheritdoc}
*/
public
function
buildRow
(
EntityInterface
$entity
)
{
/* @var \Drupal\section_library\Entity\SectionLibraryTemplate $entity */
/*
*
@var \Drupal\section_library\Entity\SectionLibraryTemplate $entity */
$row
[
'id'
]
=
$entity
->
id
();
$row
[
'name'
]
=
Link
::
createFromRoute
(
$entity
->
label
(),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment