Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
WCMS
uw_ct_profile
Commits
f294e510
Commit
f294e510
authored
Jun 16, 2021
by
Eric Bremner
Browse files
ISTWCMS-4704: adding update hook to remove fields from the layout
parent
7e2db7e2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
140 additions
and
0 deletions
+140
-0
uw_ct_profile.install
uw_ct_profile.install
+140
-0
No files found.
uw_ct_profile.install
0 → 100644
View file @
f294e510
<?php
/**
* @file
* Install, update and uninstall for profiles.
*/
use
Drupal\node\Entity\Node
;
/**
* Removes all the node fields from the layout.
*/
function
uw_ct_profile_update_8101
(
&
$sandbox
)
{
// The list of components/blocks to be removed.
$components_to_remove
=
[
'extra_field_block:node:uw_ct_profile:content_moderation_control'
,
'extra_field_block:node:uw_ct_profile:links'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_contact'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_sort_name'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_image'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_affiliation'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_title'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_info_link'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_link_persona'
,
'field_block:node:uw_ct_profile:field_uw_ct_profile_type'
,
'field_block:node:uw_ct_profile:field_uw_meta_tags'
,
'field_block:node:uw_ct_profile:field_uw_meta_image'
,
'field_block:node:uw_ct_profile:field_uw_meta_description'
,
'field_block:node:uw_ct_profile:field_uw_profile_summary'
,
];
// Get all the nids for profiles.
$nids
=
\
Drupal
::
entityQuery
(
'node'
)
->
condition
(
'type'
,
'uw_ct_profile'
)
->
execute
();
// Load all the contact nodes.
$nodes
=
Node
::
loadMultiple
(
$nids
);
// Step through each of the nodes and remove the sections
// that were locked in fields before. This will only remove
// the layouts for the current revision.
foreach
(
$nodes
as
$node
)
{
// Load the sections.
$sections
=
$node
->
get
(
'layout_builder__layout'
)
->
getSections
();
// Step through each of the sections and load the components
// and see if it needs to be removed.
foreach
(
$sections
as
$section_id
=>
$section
)
{
// Get the components for the section.
$components
=
$section
->
getComponents
();
// Step through each of the components and see if it needs
// to be removed.
foreach
(
$components
as
$id
=>
$component
)
{
// Get the configuration for the component which will have
// the uuid.
$config
=
$component
->
get
(
'configuration'
);
// If the uuid is in the list of components to be removed,
// then remove that component.
if
(
in_array
(
$config
[
'id'
],
$components_to_remove
))
{
$sections
[
$section_id
]
->
removeComponent
(
$id
);
}
}
}
// Set the new value of the sections, with the removed
// components and save the node.
$node
->
layout_builder__layout
->
setValue
(
$sections
);
$node
->
save
();
// Get the node id from the node object.
$nid
=
$node
->
id
();
// Get all the revisions for the nid.
$query
=
\
Drupal
::
database
()
->
select
(
'node_revision'
,
'nr'
);
$query
->
addField
(
'nr'
,
'vid'
);
$query
->
condition
(
'nr.nid'
,
$nid
);
$revisions
=
$query
->
execute
()
->
fetchAll
();
// Step through each of the revisions and remove the sections.
foreach
(
$revisions
as
$revision
)
{
// Get the vid.
$vid
=
$revision
->
vid
;
// Get all the sections for the revision.
$query
=
\
Drupal
::
database
()
->
select
(
'node_revision__layout_builder__layout'
,
'nrlbl'
);
$query
->
addField
(
'nrlbl'
,
'layout_builder__layout_section'
);
$query
->
addField
(
'nrlbl'
,
'delta'
);
$query
->
condition
(
'nrlbl.entity_id'
,
$nid
);
$query
->
condition
(
'nrlbl.revision_id'
,
$vid
);
$sections
=
$query
->
execute
()
->
fetchAll
();
// If the sections are not empty, remove fields from layout.
if
(
!
empty
(
$sections
))
{
// Step through each section and remove fields.
foreach
(
$sections
as
$section
)
{
// Get the section object.
$section_info
=
unserialize
(
$section
->
layout_builder__layout_section
);
// Get the components for the section.
$components
=
$section_info
->
getComponents
();
// Step through each of the components and see if it needs
// to be removed.
foreach
(
$components
as
$id
=>
$component
)
{
// Get the configuration for the component which will have
// the uuid.
$config
=
$component
->
get
(
'configuration'
);
// If the uuid is in the list of components to be removed,
// then remove that component.
if
(
in_array
(
$config
[
'id'
],
$components_to_remove
))
{
$section_info
->
removeComponent
(
$id
);
}
}
// Update query to remove components.
$query
=
\
Drupal
::
database
()
->
update
(
'node_revision__layout_builder__layout'
)
->
fields
(
[
'layout_builder__layout_section'
=>
serialize
(
$section_info
),
]
)
->
condition
(
'entity_id'
,
$nid
)
->
condition
(
'revision_id'
,
$vid
)
->
condition
(
'delta'
,
$section
->
delta
)
->
execute
();
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment