Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / node / node.install
1 <?php
2
3 /**
4  * @file
5  * Install, update and uninstall functions for the node module.
6  */
7
8 use Drupal\Core\Database\Database;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\user\RoleInterface;
11
12 /**
13  * Implements hook_requirements().
14  */
15 function node_requirements($phase) {
16   $requirements = [];
17   if ($phase === 'runtime') {
18     // Only show rebuild button if there are either 0, or 2 or more, rows
19     // in the {node_access} table, or if there are modules that
20     // implement hook_node_grants().
21     $grant_count = \Drupal::entityManager()->getAccessControlHandler('node')->countGrants();
22     if ($grant_count != 1 || count(\Drupal::moduleHandler()->getImplementations('node_grants')) > 0) {
23       $value = \Drupal::translation()->formatPlural($grant_count, 'One permission in use', '@count permissions in use', ['@count' => $grant_count]);
24     }
25     else {
26       $value = t('Disabled');
27     }
28
29     $requirements['node_access'] = [
30       'title' => t('Node Access Permissions'),
31       'value' => $value,
32       'description' => t('If the site is experiencing problems with permissions to content, you may have to rebuild the permissions cache. Rebuilding will remove all privileges to content and replace them with permissions based on the current modules and settings. Rebuilding may take some time if there is a lot of content or complex permission settings. After rebuilding has completed, content will automatically use the new permissions. <a href=":rebuild">Rebuild permissions</a>', [
33         ':rebuild' => \Drupal::url('node.configure_rebuild_confirm'),
34       ]),
35     ];
36   }
37   return $requirements;
38 }
39
40 /**
41  * Implements hook_schema().
42  */
43 function node_schema() {
44   $schema['node_access'] = [
45     'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
46     'fields' => [
47       'nid' => [
48         'description' => 'The {node}.nid this record affects.',
49         'type' => 'int',
50         'unsigned' => TRUE,
51         'not null' => TRUE,
52         'default' => 0,
53       ],
54       'langcode' => [
55         'description' => 'The {language}.langcode of this node.',
56         'type' => 'varchar_ascii',
57         'length' => 12,
58         'not null' => TRUE,
59         'default' => '',
60       ],
61       'fallback' => [
62         'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
63         'type' => 'int',
64         'unsigned' => TRUE,
65         'not null' => TRUE,
66         'default' => 1,
67         'size' => 'tiny',
68       ],
69       'gid' => [
70         'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
71         'type' => 'int',
72         'unsigned' => TRUE,
73         'not null' => TRUE,
74         'default' => 0,
75       ],
76       'realm' => [
77         'description' => 'The realm in which the user must possess the grant ID. Each node access node can define one or more realms.',
78         'type' => 'varchar_ascii',
79         'length' => 255,
80         'not null' => TRUE,
81         'default' => '',
82       ],
83       'grant_view' => [
84         'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
85         'type' => 'int',
86         'unsigned' => TRUE,
87         'not null' => TRUE,
88         'default' => 0,
89         'size' => 'tiny',
90       ],
91       'grant_update' => [
92         'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
93         'type' => 'int',
94         'unsigned' => TRUE,
95         'not null' => TRUE,
96         'default' => 0,
97         'size' => 'tiny',
98       ],
99       'grant_delete' => [
100         'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
101         'type' => 'int',
102         'unsigned' => TRUE,
103         'not null' => TRUE,
104         'default' => 0,
105         'size' => 'tiny',
106       ],
107     ],
108     'primary key' => ['nid', 'gid', 'realm', 'langcode'],
109     'foreign keys' => [
110       'affected_node' => [
111         'table' => 'node',
112         'columns' => ['nid' => 'nid'],
113       ],
114     ],
115   ];
116
117   return $schema;
118 }
119
120 /**
121  * Implements hook_install().
122  */
123 function node_install() {
124   // Enable default permissions for system roles.
125   // IMPORTANT: Modules SHOULD NOT automatically grant any user role access
126   // permissions in hook_install().
127   // However, the 'access content' permission is a very special case, since
128   // there is hardly a point in installing the Node module without granting
129   // these permissions. Doing so also allows tests to continue to operate as
130   // expected without first having to manually grant these default permissions.
131   if (\Drupal::moduleHandler()->moduleExists('user')) {
132     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content']);
133     user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access content']);
134   }
135
136   // Populate the node access table.
137   db_insert('node_access')
138     ->fields([
139       'nid' => 0,
140       'gid' => 0,
141       'realm' => 'all',
142       'grant_view' => 1,
143       'grant_update' => 0,
144       'grant_delete' => 0,
145     ])
146     ->execute();
147 }
148
149 /**
150  * Implements hook_uninstall().
151  */
152 function node_uninstall() {
153   // Delete remaining general module variables.
154   \Drupal::state()->delete('node.node_access_needs_rebuild');
155 }
156
157 /**
158  * Add 'revision_translation_affected' field to 'node' entities.
159  */
160 function node_update_8001() {
161   // Install the definition that this field had in
162   // \Drupal\node\Entity\Node::baseFieldDefinitions()
163   // at the time that this update function was written. If/when code is
164   // deployed that changes that definition, the corresponding module must
165   // implement an update function that invokes
166   // \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition()
167   // with the new definition.
168   $storage_definition = BaseFieldDefinition::create('boolean')
169     ->setLabel(t('Revision translation affected'))
170     ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
171     ->setReadOnly(TRUE)
172     ->setRevisionable(TRUE)
173     ->setTranslatable(TRUE);
174
175   \Drupal::entityDefinitionUpdateManager()
176     ->installFieldStorageDefinition('revision_translation_affected', 'node', 'node', $storage_definition);
177 }
178
179 /**
180  * Remove obsolete indexes from the node schema.
181  */
182 function node_update_8002() {
183   // The "node__default_langcode" and "node_field__langcode" indexes were
184   // removed from \Drupal\node\NodeStorageSchema in
185   // https://www.drupal.org/node/2261669, but this update function wasn't
186   // added until https://www.drupal.org/node/2542748. Regenerate the related
187   // schemas to ensure they match the currently expected status.
188   $manager = \Drupal::entityDefinitionUpdateManager();
189   // Regenerate entity type indexes, this should drop "node__default_langcode".
190   $manager->updateEntityType($manager->getEntityType('node'));
191   // Regenerate "langcode" indexes, this should drop "node_field__langcode".
192   $manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition('langcode', 'node'));
193 }
194
195 /**
196  * Promote 'status' and 'uid' fields to entity keys.
197  */
198 function node_update_8003() {
199   // The 'status' and 'uid' fields were added to the 'entity_keys' annotation
200   // of \Drupal\node\Entity\Node in https://www.drupal.org/node/2498919, but
201   // this update function wasn't added until
202   // https://www.drupal.org/node/2542748.
203   $manager = \Drupal::entityDefinitionUpdateManager();
204   $entity_type = $manager->getEntityType('node');
205   $entity_keys = $entity_type->getKeys();
206   $entity_keys['status'] = 'status';
207   $entity_keys['uid'] = 'uid';
208   $entity_type->set('entity_keys', $entity_keys);
209   $manager->updateEntityType($entity_type);
210
211   // @todo The above should be enough, since that is the only definition that
212   //   changed. But \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema varies
213   //   field schema by whether a field is an entity key, so invoke
214   //   EntityDefinitionUpdateManagerInterface::updateFieldStorageDefinition()
215   //   with an unmodified field storage definition to trigger the necessary
216   //   changes. SqlContentEntityStorageSchema::onEntityTypeUpdate() should be
217   //   fixed to automatically handle this.
218   //   See https://www.drupal.org/node/2554245.
219   foreach (['status', 'uid'] as $field_name) {
220     $manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition($field_name, 'node'));
221   }
222 }
223
224 /**
225  * Change {node_access}.fallback from an int to a tinyint as it is a boolean.
226  */
227 function node_update_8300() {
228   Database::getConnection()->schema()->changeField('node_access', 'fallback', 'fallback', [
229     'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
230     'type' => 'int',
231     'unsigned' => TRUE,
232     'not null' => TRUE,
233     'default' => 1,
234     'size' => 'tiny',
235   ]);
236 }
237
238 /**
239  * Set the 'published' entity key.
240  */
241 function node_update_8301() {
242   $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
243   $entity_type = $definition_update_manager->getEntityType('node');
244   $keys = $entity_type->getKeys();
245   $keys['published'] = 'status';
246   $entity_type->set('entity_keys', $keys);
247   $definition_update_manager->updateEntityType($entity_type);
248 }