node.install 10 KB

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