Skip to content
node.install 5.76 KiB
Newer Older
/**
 * @file
 * Install, update and uninstall functions for the node module.
 */

use Drupal\Component\Utility\SafeMarkup;
/**
 * Implements hook_requirements().
 */
function node_requirements($phase) {
  $requirements = array();
  if ($phase === 'runtime') {
    // Only show rebuild button if there are either 0, or 2 or more, rows
    // in the {node_access} table, or if there are modules that
    // implement hook_node_grants().
    $grant_count = \Drupal::entityManager()->getAccessControlHandler('node')->countGrants();
    if ($grant_count != 1 || count(\Drupal::moduleHandler()->getImplementations('node_grants')) > 0) {
      $value = format_plural($grant_count, 'One permission in use', '@count permissions in use', array('@count' => $grant_count));
    }
    else {
      $value = t('Disabled');
    }
    $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.');

    $requirements['node_access'] = array(
      'title' => t('Node Access Permissions'),
      'value' => $value,
      // The result of t() is safe and so is the result of l(). Preserving
      // safe object.
      'description' => SafeMarkup::set($description . ' ' . l(t('Rebuild permissions'), 'admin/reports/status/rebuild')),
 */
function node_schema() {
  $schema['node_access'] = array(
    'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
        'description' => 'The {node}.nid this record affects.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      'langcode' => array(
        'description' => 'The {language}.langcode of this node.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'fallback' => array(
        'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
        'type' => 'int',
      'gid' => array(
        'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
      'realm' => array(
        'description' => 'The realm in which the user must possess the grant ID. Each node access node can define one or more realms.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      'grant_view' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
      'grant_update' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
      'grant_delete' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
    'primary key' => array('nid', 'gid', 'realm', 'langcode'),
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
/**
 * Implements hook_install().
 */
function node_install() {
  // Enable default permissions for system roles.
  // IMPORTANT: Modules SHOULD NOT automatically grant any user role access
  // permissions in hook_install().
  // However, the 'access content' permission is a very special case, since
  // there is hardly a point in installing the Node module without granting
  // these permissions. Doing so also allows tests to continue to operate as
  // expected without first having to manually grant these default permissions.
  if (\Drupal::moduleHandler()->moduleExists('user')) {
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
    user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('access content'));
  }

  // Populate the node access table.
  db_insert('node_access')
    ->fields(array(
      'nid' => 0,
      'gid' => 0,
      'realm' => 'all',
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    ))
    ->execute();
}

/**
 * Implements hook_uninstall().
 *
 * @see node_ranking()
 * @see _node_rankings()
 */
function node_uninstall() {
  // Delete node type variables.
  $types = \Drupal::configFactory()->listAll('node.type.');
    $type = \Drupal::config($config_name)->get('type');
    if (\Drupal::moduleHandler()->moduleExists('language')) {
      $key = language_get_default_configuration_settings_key('node', $type);
      \Drupal::config('language.settings')->clear($key)->save();
  }

  // Delete remaining general module variables.
  \Drupal::state()->delete('node.node_access_needs_rebuild');