uw_ct_profile.install 4.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?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();
        }
      }
    }
  }
}