forum.install

Same filename and directory in other branches
  1. 7.x modules/forum/forum.install
  2. 9 core/modules/forum/forum.install
  3. 8.9.x core/modules/forum/forum.install
  4. 11.x core/modules/forum/forum.install

Install, update, and uninstall functions for the Forum module.

File

core/modules/forum/forum.install

View source
<?php


/**
 * @file
 * Install, update, and uninstall functions for the Forum module.
 */
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_install().
 */
function forum_install($is_syncing) {
    // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
    module_set_weight('forum', 1);
    // Do not allow to delete the forum's node type machine name.
    $locked = \Drupal::state()->get('node.type.locked');
    $locked['forum'] = 'forum';
    \Drupal::state()->set('node.type.locked', $locked);
    if (!$is_syncing) {
        // Create a default forum so forum posts can be created.
        $term = Term::create([
            'name' => t('General discussion'),
            'description' => '',
            'parent' => [
                0,
            ],
            'vid' => 'forums',
            'forum_container' => 0,
        ]);
        $term->save();
    }
}

/**
 * Implements hook_uninstall().
 */
function forum_uninstall() {
    if ($field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums')) {
        $field_storage->delete();
    }
    if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
        $field_storage->delete();
    }
    if ($field_storage = FieldStorageConfig::loadByName('taxonomy_term', 'forum_container')) {
        $field_storage->delete();
    }
    // Purge field data now to allow taxonomy and options module to be uninstalled
    // if this is the only field remaining.
    field_purge_batch(10);
    // Allow to delete a forum's node type.
    $locked = \Drupal::state()->get('node.type.locked');
    unset($locked['forum']);
    \Drupal::state()->set('node.type.locked', $locked);
}

/**
 * Implements hook_schema().
 */
function forum_schema() {
    $schema['forum'] = [
        'description' => 'Stores the relationship of nodes to forum terms.',
        'fields' => [
            'nid' => [
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => 'The {node}.nid of the node.',
            ],
            'vid' => [
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => 'Primary Key: The {node}.vid of the node.',
            ],
            'tid' => [
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
            ],
        ],
        'indexes' => [
            'forum_topic' => [
                'nid',
                'tid',
            ],
            'tid' => [
                'tid',
            ],
        ],
        'primary key' => [
            'vid',
        ],
        'foreign keys' => [
            'forum_node' => [
                'table' => 'node',
                'columns' => [
                    'nid' => 'nid',
                    'vid' => 'vid',
                ],
            ],
        ],
    ];
    $schema['forum_index'] = [
        'description' => 'Maintains denormalized information about node/term relationships.',
        'fields' => [
            'nid' => [
                'description' => 'The {node}.nid this record tracks.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ],
            'title' => [
                'description' => 'The node title.',
                'type' => 'varchar',
                'length' => 255,
                'not null' => TRUE,
                'default' => '',
            ],
            'tid' => [
                'description' => 'The term ID.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ],
            'sticky' => [
                'description' => 'Boolean indicating whether the node is sticky.',
                'type' => 'int',
                'not null' => FALSE,
                'default' => 0,
                'size' => 'tiny',
            ],
            'created' => [
                'description' => 'The Unix timestamp when the node was created.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'size' => 'big',
            ],
            'last_comment_timestamp' => [
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
                'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
                'size' => 'big',
            ],
            'comment_count' => [
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => 'The total number of comments on this node.',
            ],
        ],
        'indexes' => [
            'forum_topics' => [
                'nid',
                'tid',
                'sticky',
                'last_comment_timestamp',
            ],
            'created' => [
                'created',
            ],
            'last_comment_timestamp' => [
                'last_comment_timestamp',
            ],
        ],
        'primary key' => [
            'nid',
            'tid',
        ],
        'foreign keys' => [
            'tracked_node' => [
                'table' => 'node',
                'columns' => [
                    'nid' => 'nid',
                ],
            ],
            'term' => [
                'table' => 'taxonomy_term_data',
                'columns' => [
                    'tid' => 'tid',
                ],
            ],
        ],
    ];
    return $schema;
}

/**
 * Remove the year 2038 date limitation.
 */
function forum_update_10100(&$sandbox = NULL) {
    $connection = \Drupal::database();
    if ($connection->schema()
        ->tableExists('forum_index') && $connection->databaseType() != 'sqlite') {
        $new = [
            'description' => 'The Unix timestamp when the node was created.',
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0,
            'size' => 'big',
        ];
        $connection->schema()
            ->changeField('forum_index', 'created', 'created', $new);
        $new = [
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
            'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
            'size' => 'big',
        ];
        $connection->schema()
            ->changeField('forum_index', 'last_comment_timestamp', 'last_comment_timestamp', $new);
    }
}

/**
 * Repopulate the forum index table.
 */
function forum_update_10101(&$sandbox = NULL) : PluralTranslatableMarkup {
    $query = \Drupal::database()->select('forum_index', 'fi')
        ->fields('fi', [
        'nid',
        'tid',
    ])
        ->groupBy('nid')
        ->groupBy('tid');
    $query->addExpression('count(*)', 'count');
    $query->having('count(*) > 1');
    $results = $query->execute();
    $nids_to_rebuild = [];
    foreach ($results as $row) {
        \Drupal::database()->delete('forum_index')
            ->condition('tid', $row->tid)
            ->condition('nid', $row->nid)
            ->execute();
        $nids_to_rebuild[] = $row->nid;
    }
    \Drupal::state()->set('forum_update_10101_nids', $nids_to_rebuild);
    return new PluralTranslatableMarkup(count($nids_to_rebuild), 'Removed 1 duplicate entry from forum_index', 'Removed @count duplicate entries from forum_index');
}

/**
 * Add a primary key to forum_index.
 */
function forum_update_10102(&$sandbox = NULL) {
    $connection = \Drupal::database();
    if ($connection->schema()
        ->tableExists('forum_index')) {
        // Data in this table could have duplicates. The data can be re-constructed
        // from other data in the site. To avoid duplicate key errors we delete any
        // rows that are duplicates and then recreate them in a post-update hook.
        // @see \forum_post_update_recreate_forum_index_rows().
        $connection->schema()
            ->addPrimaryKey('forum_index', [
            'nid',
            'tid',
        ]);
        return \t('Added primary key to the forum_index table.');
    }
    return \t('Index already exists');
}

Functions

Title Deprecated Summary
forum_install Implements hook_install().
forum_schema Implements hook_schema().
forum_uninstall Implements hook_uninstall().
forum_update_10100 Remove the year 2038 date limitation.
forum_update_10101 Repopulate the forum index table.
forum_update_10102 Add a primary key to forum_index.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.