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

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

Eric Bremner's avatar
Eric Bremner committed
8
use Drupal\node\Entity\Node;
Eric Bremner's avatar
Eric Bremner committed
9
use Drupal\taxonomy\Entity\Term;
Eric Bremner's avatar
Eric Bremner committed
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
/**
 * 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',
  ];

  foreach ($types as $type) {
    _uw_ct_event_term_create($type, 'uw_tax_event_type', []);
  }
}

/**
 * Helper function to create a taxonomy term programmatically.
 *
 * @code
 * // Create top level term
 * $term_id = _nodemaker_term_create('My Term', 'my_vocab', []);
 *
 * // Create term with parent term with an id of 999
 * $term_id = _nodemaker_term_create('My Term', 'my_vocab', [999]);
 * @endcode
 *
 * @param string $term
 *   - Term Name.
 * @param string $vocabulary
 *   - System id of the vocabulary term will be added to.
 * @param array $parent
 *   - Array of term ids to be assigned as parent.
 *
 * @return int|null
 *   - Returns the term id of the created term on success, null on failure.
 */
function _uw_ct_event_term_create($term, $vocabulary, array $parent = []) {

  // Create the taxonomy term.
  $new_term = Term::create([
    'name' => $term,
    'vid' => $vocabulary,
    'parent' => $parent,
  ]);

  // Save the taxonomy term.
  $new_term->save();

  // Return the taxonomy term id.
  return $new_term->id();
}
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
141
142
143
144
145
146
147
148
149
150

/**
 * 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();
      }
    }
  }
}