node.install 9.5 KB

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