5 * Install, update and uninstall functions for the node module.
8 use Drupal\Core\Database\Database;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\user\RoleInterface;
13 * Implements hook_requirements().
15 function node_requirements($phase) {
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]);
26 $value = t('Disabled');
29 $requirements['node_access'] = [
30 'title' => t('Node Access Permissions'),
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'),
41 * Implements hook_schema().
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.',
48 'description' => 'The {node}.nid this record affects.',
55 'description' => 'The {language}.langcode of this node.',
56 'type' => 'varchar_ascii',
62 'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
70 'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
77 'description' => 'The realm in which the user must possess the grant ID. Modules can define one or more realms by implementing hook_node_grants().',
78 'type' => 'varchar_ascii',
84 'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
92 'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
100 'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
108 'primary key' => ['nid', 'gid', 'realm', 'langcode'],
112 'columns' => ['nid' => 'nid'],
121 * Implements hook_install().
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']);
136 // Populate the node access table.
137 db_insert('node_access')
150 * Implements hook_uninstall().
152 function node_uninstall() {
153 // Delete remaining general module variables.
154 \Drupal::state()->delete('node.node_access_needs_rebuild');
158 * Add 'revision_translation_affected' field to 'node' entities.
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.'))
172 ->setRevisionable(TRUE)
173 ->setTranslatable(TRUE);
175 \Drupal::entityDefinitionUpdateManager()
176 ->installFieldStorageDefinition('revision_translation_affected', 'node', 'node', $storage_definition);
180 * Remove obsolete indexes from the node schema.
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'));
196 * Promote 'status' and 'uid' fields to entity keys.
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);
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'));
225 * Change {node_access}.fallback from an int to a tinyint as it is a boolean.
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.',
239 * Set the 'published' entity key.
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);
251 * Fix realm column description on the node_access table.
253 function node_update_8400() {
254 $schema = drupal_get_module_schema('node', 'node_access');
255 $schema['fields']['realm']['description'] = 'The realm in which the user must possess the grant ID. Modules can define one or more realms by implementing hook_node_grants().';
256 Database::getConnection()->schema()->changeField('node_access', 'realm', 'realm', $schema['fields']['realm']);
260 * Run a node access rebuild, if required.
262 function node_update_8401() {
263 // Get the list of node access modules.
264 $modules = \Drupal::moduleHandler()->getImplementations('node_grants');
265 // If multilingual usage, then rebuild node access.
266 if (count($modules) > 0 && \Drupal::languageManager()->isMultilingual()) {
267 node_access_needs_rebuild(TRUE);