You are here

suggestion.install in Autocomplete Search Suggestions 8.2

Same filename and directory in other branches
  1. 8 suggestion.install
  2. 7 suggestion.install
  3. 3.0.x suggestion.install

Contains suggestion.install.

File

suggestion.install
View source
<?php

/**
 * @file
 * Contains suggestion.install.
 */
use Drupal\Core\Database\Database;

/**
 * Implements hook_schema().
 */
function suggestion_schema() {
  $schema['suggestion'] = [
    'description' => 'Suggestion data.',
    'fields' => [
      'ngram' => [
        'description' => "The suggestion string.",
        'type' => 'varchar',
        'length' => 65,
        'binary' => TRUE,
        'not null' => TRUE,
      ],
      'src' => [
        'description' => "Suggestion source bitmap: 0 = disabled, 1 = content, 2 = surfer, 4 = priority",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ],
      'atoms' => [
        'description' => "The number of words in this suggestion.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ],
      'qty' => [
        'description' => "The number of instances.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ],
      'density' => [
        'description' => "The weighted average.",
        'type' => 'float',
        'unsigned' => TRUE,
        'precision' => 3,
        'not null' => TRUE,
        'default' => 1.0,
      ],
      'langcode' => [
        'description' => "The language of suggestion string.",
        'type' => 'varchar',
        'length' => 32,
        'binary' => TRUE,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'indexes' => [
      'atoms' => [
        'atoms',
      ],
      'density' => [
        'density',
      ],
      'langcode' => [
        'langcode',
      ],
      'qty' => [
        'qty',
      ],
      'src' => [
        'src',
      ],
      'src_lang' => [
        'src',
        'langcode',
      ],
      'density_ngram_atom' => [
        'density',
        'ngram',
        'atoms',
      ],
      'atom_src_lang_ngram' => [
        'atoms',
        'src',
        'langcode',
        'ngram',
      ],
    ],
    'primary key' => [
      'langcode',
      'ngram',
    ],
  ];
  return $schema;
}

/**
 * Add language column.
 */
function suggestion_update_8201() {
  module_load_include('inc', 'suggestion', 'suggestion.install');
  $schema = Database::getConnection()
    ->schema();
  $spec = suggestion_install_update_8201_spec();
  if (!$schema
    ->fieldExists('suggestion', 'langcode')) {
    $schema
      ->addField('suggestion', 'langcode', $spec['fields']['langcode']);
  }

  // Add new indexes.
  if (!$schema
    ->indexExists('suggestion', 'langcode')) {
    $schema
      ->addIndex('suggestion', 'langcode', [
      'langcode',
    ], $spec);
  }
  if (!$schema
    ->indexExists('suggestion', 'src')) {
    $schema
      ->addIndex('suggestion', 'src', [
      'src',
    ], $spec);
  }
  if (!$schema
    ->indexExists('suggestion', 'src_lang')) {
    $schema
      ->addIndex('suggestion', 'src_lang', $spec['indexes']['src_lang'], $spec);
  }
  if (!$schema
    ->indexExists('suggestion', 'density_ngram_atom')) {
    $schema
      ->addIndex('suggestion', 'density_ngram_atom', $spec['indexes']['density_ngram_atom'], $spec);
  }
  if (!$schema
    ->indexExists('suggestion', 'atom_src_lang_ngram')) {
    $schema
      ->addIndex('suggestion', 'atom_src_lang_ngram', $spec['indexes']['atom_src_lang_ngram'], $spec);
  }

  // Replace primary index.
  $schema
    ->dropPrimaryKey('suggestion');
  $schema
    ->addPrimaryKey('suggestion', [
    'langcode',
    'ngram',
  ]);

  // Delete superfluous indexes.
  if ($schema
    ->indexExists('suggestion', 'ngram_atom')) {
    $schema
      ->dropIndex('suggestion', 'ngram_atom');
  }
  if ($schema
    ->indexExists('suggestion', 'ngram_atom_qty')) {
    $schema
      ->dropIndex('suggestion', 'ngram_atom_qty');
  }
  if ($schema
    ->indexExists('suggestion', 'ngram_qty')) {
    $schema
      ->dropIndex('suggestion', 'ngram_qty');
  }
  drupal_flush_all_caches();
}

/**
 * Add the padding configuration variable to set the minimum atoms in an ngram.
 */
function suggestion_update_8202() {
  $cfg = \Drupal::configFactory()
    ->getEditable('suggestion.config');
  $max = $cfg
    ->get('atoms');
  $cfg
    ->clear('atoms');
  $cfg
    ->save(TRUE);
  $cfg
    ->set('atoms_max', $max);
  $cfg
    ->set('atoms_min', 1);
  $cfg
    ->save(TRUE);
  drupal_flush_all_caches();
}

/**
 * Update migrate configuration settings to new architecture.
 */
function suggestion_update_8203() {
  $cfg = \Drupal::configFactory()
    ->getEditable('suggestion.config');
  $field_name = $cfg
    ->get('suggestion_field_name');
  $form_key = $cfg
    ->get('suggestion_form_id');
  $cfg
    ->clear('suggestion_field_name');
  $cfg
    ->clear('suggestion_form_id');
  $cfg
    ->save(TRUE);
  $cfg
    ->set('entry_style', 'simple');
  $cfg
    ->set('field_name', $field_name);
  $cfg
    ->set('form_key', $form_key);
  $cfg
    ->save(TRUE);
  drupal_flush_all_caches();
}

Functions

Namesort descending Description
suggestion_schema Implements hook_schema().
suggestion_update_8201 Add language column.
suggestion_update_8202 Add the padding configuration variable to set the minimum atoms in an ngram.
suggestion_update_8203 Update migrate configuration settings to new architecture.