uw_ct_event.install 5.91 KB
Newer Older
1
2
3
4
5
6
7
<?php

/**
 * @file
 * Install, update and uninstall for event content type.
 */

8
use Drupal\field\Entity\FieldConfig;
Eric Bremner's avatar
Eric Bremner committed
9
10
use Drupal\node\Entity\Node;

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * Implements hook_install().
 */
function uw_ct_event_install() {

  // Add terms to the vocabulary 'uw_tax_event_type'.
  $types = [
    'Conference',
    'Information session',
    'Lecture',
    'Open house',
    'Performance',
    'Reception',
    'Reunion',
    'Seminar',
    'Thesis defence',
    'Workshop',
  ];

30
  $weight = 0;
31
  foreach ($types as $type) {
32
    _uw_cfg_common_create_term($type, 'uw_tax_event_type', $weight, []);
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
/**
 * Removes the first and last section from events.
 */
function uw_ct_event_update_8101(&$sandbox) {

  // Get all the nids for blogs.
  $nids = \Drupal::entityQuery('node')->condition('type', 'uw_ct_event')->execute();

  // Load all the blog 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();

    // If the node has at least 3 sections, remove the
    // first and last.
    if (count($sections) > 2) {

      // Remove first and last section.
      unset($sections[0]);
      unset($sections[count($sections)]);

      // Set the updated sections.
      $node->layout_builder__layout->setValue($sections);

      // Save the node.
      $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->condition('nrlbl.entity_id', $nid);
      $query->condition('nrlbl.revision_id', $vid);
      $sections = $query->execute()->fetchAll();

      // If the sections are not empty, remove first and last.
      if (!empty($sections)) {

        // Get the last section.
        $last = count($sections) - 1;

        // Delete the first section.
        $query = \Drupal::database()->delete('node_revision__layout_builder__layout');
        $query->condition('entity_id', $nid);
        $query->condition('revision_id', $vid);
        $query->condition('delta', 0);
        $query->execute();

        // Delete the last section.
        $query = \Drupal::database()->delete('node_revision__layout_builder__layout');
        $query->condition('entity_id', $nid);
        $query->condition('revision_id', $vid);
        $query->condition('delta', $last);
        $query->execute();
      }
    }
  }
}
115
116
117
118
119
120

/**
 * Creates hero image copy from listing page image.
 */
function uw_ct_event_update_8102(&$sandbox) {

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  // Adding field to the bundle. FieldStorageConfig has already been created.
  FieldConfig::create([
    'field_name' => 'field_uw_hero_image',
    'entity_type' => 'node',
    'bundle' => 'uw_ct_event',
    'label' => 'Hero image',
    'settings' => [
      'handler' => 'default:media',
      'handler_settings' => [
        'target_bundles' => [
          'uw_mt_image' => 'uw_mt_image',
        ],
      ],
    ],
  ])->save();

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  // Get all the nids for events.
  $nids = \Drupal::entityQuery('node')->condition('type', 'uw_ct_event')->execute();

  // Load all the event nodes.
  $nodes = Node::loadMultiple($nids);

  foreach ($nodes as $node) {

    // Get listing page entity.
    $listing_image = $node->field_uw_event_listing_page_img->entity;

    if ($listing_image) {

      // Set hero image to use same photo as listing page.
      $node->field_uw_hero_image->target_id = $listing_image->id();
      $node->save();
    }
  }
}
156
157
158
159
160
161
162
163

/**
 * Update location country and area to null if only components.
 */
function uw_ct_event_update_8103() {

  // Get the nids and nodes for all events.
  $nids = \Drupal::entityQuery('node')->condition('type', 'uw_ct_event')->execute();
Eric Bremner's avatar
Eric Bremner committed
164
  $nodes = Node::loadMultiple($nids);
165
166
167
168

  // The address components that need to have
  // values for us not to reset country/area.
  $address_components = [
169
    'organization',
170
171
172
173
174
175
176
    'locality',
    'postal_code',
    'address_line1',
    'address_line2',
  ];

  // Step through each of the nodes and check
Eric Bremner's avatar
Eric Bremner committed
177
  // if we need to update the country/area.
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  foreach ($nodes as $node) {

    // Get the address field from the node.
    $address = $node->field_uw_event_location_address->getValue()[0];

    // If there is an address, check if we need
    // to update the country/area.
    if ($address) {

      // Flag to update the address.  Initial set to
      // TRUE, if we find that the address has a component
      // then we will not update the country/area.
      $address_update = TRUE;

      // Step through each of the components as see if
      // it has a value, and if it does set the flag
      // so that we do not update the country/area.
      foreach ($address_components as $address_component) {
        if ($address[$address_component]) {
          $address_update = FALSE;
        }
      }

      // If we are to update country/area, then update it,
      // and save the node.
      if ($address_update) {
        $address['country_code'] = NULL;
        $address['administrative_area'] = NULL;
        $node->field_uw_event_location_address->setValue($address);
        $node->save();
      }
    }
  }
}
212
213
214
215
216
217
218
219
220
221
222

/**
 * Implements hook_update_dependencies().
 */
function uw_ct_event_update_dependencies() {
  $dependencies['uw_ct_event'][8102] = [
    'uw_ct_blog' => 8102,
  ];

  return $dependencies;
}