You are here

search_by_page_paths.install in Search by Page 8

Install hooks for search_by_page_paths module

File

search_by_page_paths/search_by_page_paths.install
View source
<?php

/**
 * @file
 * Install hooks for search_by_page_paths module
 */
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_schema().
 */
function search_by_page_paths_schema() {
  $schema['sbpp_path'] = array(
    'description' => 'Contains paths to be indexed by search_by_page_paths module',
    'fields' => array(
      'pid' => array(
        'description' => 'Primary key',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'environment' => array(
        'description' => 'Environment ID',
        'type' => 'int',
        'size' => 'big',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      // Do not use 'path' as a db field - reserved word!
      'page_path' => array(
        'description' => 'Path to index',
        'type' => 'varchar',
        'length' => 255,
      ),
      'title' => array(
        'description' => 'Title of this page',
        'type' => 'varchar',
        'length' => 255,
      ),
      // Do not use 'type' as a db field - reserved word!
      'page_type' => array(
        'description' => 'Type to display for this page',
        'type' => 'varchar',
        'length' => 255,
      ),
      'snippet' => array(
        'description' => 'Yes, no, or some custom snippet text',
        'type' => 'text',
        'size' => 'medium',
      ),
      'role' => array(
        'description' => 'Role ID used to index this path',
        'type' => 'int',
        'size' => 'big',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'languages' => array(
        'description' => 'Serialized array of languages that can be used for this path',
        'type' => 'text',
      ),
    ),
    'indexes' => array(
      'pth' => array(
        'page_path',
      ),
      'ttl' => array(
        'title',
      ),
      'envi' => array(
        'environment',
      ),
    ),
    'primary key' => array(
      'pid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_update_N().
 *
 * Adds languages, environment, and uname fields to sbpp_path table.
 */
function search_by_page_paths_update_6000() {
  \Drupal::database()
    ->schema()
    ->addField('sbpp_path', 'languages', array(
    'description' => 'Serialized array of languages that can be used for this path',
    'type' => 'text',
  ));
  \Drupal::database()
    ->schema()
    ->addField('sbpp_path', 'environment', array(
    'description' => 'Environment ID',
    'type' => 'int',
    'size' => 'big',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  \Drupal::database()
    ->schema()
    ->addIndex('sbpp_path', 'envi', array(
    'environment',
  ));
  \Drupal::database()
    ->schema()
    ->addField('sbpp_path', 'uname', array(
    'description' => 'User name used to index this path',
    'type' => 'varchar',
    'length' => '60',
  ));

  // Set default for languages and environment
  $lang = language_default('language');
  $val = serialize(array(
    $lang => $lang,
  ));
  \Drupal::database()
    ->update('sbpp_path')
    ->fields(array(
    'languages' => $val,
    'environment' => 1,
  ))
    ->execute();
}

/**
 * Implements hook_update_N().
 *
 * Removes uname field, adds role field.
 */
function search_by_page_paths_update_6001() {
  \Drupal::database()
    ->schema()
    ->dropField('sbpp_path', 'uname');
  \Drupal::database()
    ->schema()
    ->addField('sbpp_path', 'role', array(
    'description' => 'Role ID used to index this path',
    'type' => 'int',
    'size' => 'big',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => AccountInterface::ANONYMOUS_ROLE,
  ));
}