You are here

menu_entity_index.install in Menu Entity Index 8

Hooks related to (un-)installation of Menu Entity Index module.

File

menu_entity_index.install
View source
<?php

/**
 * @file
 * Hooks related to (un-)installation of Menu Entity Index module.
 */
use Drupal\Core\Database\Database;

/**
 * Implements hook_schema().
 */
function menu_entity_index_schema() {
  $schema['menu_entity_index'] = [
    'description' => 'Maintains an index of menu links and their referenced entities.',
    'fields' => [
      'menu_name' => [
        'description' => 'Menu name of host entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'level' => [
        'description' => 'Menu level of the host entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'The entity type of the host entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'entity_subtype' => [
        'description' => 'The entity subtype of the host entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'entity_id' => [
        'description' => 'The entity id of the host entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_uuid' => [
        'description' => 'The uuid of the host entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'parent_type' => [
        'description' => 'The entity type of the parent entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'parent_id' => [
        'description' => 'The entity id of the parent entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ],
      'parent_uuid' => [
        'description' => 'The uuid of the parent entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'langcode' => [
        'description' => 'The language code of the host entity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ],
      'target_type' => [
        'description' => 'The entity type of the target entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'target_subtype' => [
        'description' => 'The entity subtype of the target entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'target_id' => [
        'description' => 'The entity id of the target entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'target_uuid' => [
        'description' => 'The uuid of the target entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'target_langcode' => [
        'description' => 'The language code of the target entity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'menu_name',
      'entity_uuid',
      'parent_uuid',
      'target_uuid',
      'langcode',
      'target_langcode',
    ],
    'indexes' => [
      'host_entity' => [
        'entity_type',
        'entity_subtype',
        'entity_id',
        'langcode',
      ],
      'target_entity' => [
        'target_type',
        'target_subtype',
        'target_id',
        'target_langcode',
      ],
      'target_id' => [
        'target_id',
      ],
      'target_type' => [
        'target_type',
      ],
      'target_langcode' => [
        'target_langcode',
      ],
    ],
  ];
  return $schema;
}

/**
 * Adds langcode fields to primary index of menu_entity_index database table.
 */
function menu_entity_index_update_8101() {
  $table = 'menu_entity_index';
  $index = 'PRIMARY';
  $schema = Database::getConnection()
    ->schema();
  if ($schema) {
    if ($schema
      ->tableExists($table)) {
      if ($schema
        ->indexExists($table, $index)) {
        $schema
          ->dropIndex($table, $index);
      }
      $schema
        ->addPrimaryKey($table, [
        'menu_name',
        'entity_uuid',
        'parent_uuid',
        'target_uuid',
        'langcode',
        'target_langcode',
      ]);
    }
  }
}

/**
 * Updates parent_id field definition of menu_entity_index database table.
 */
function menu_entity_index_update_8102() {
  $table = 'menu_entity_index';
  $field = 'parent_id';
  $spec = [
    'description' => 'The entity id of the parent entity.',
    'type' => 'varchar',
    'length' => 128,
    'not null' => FALSE,
  ];
  $schema = Database::getConnection()
    ->schema();
  if ($schema) {
    if ($schema
      ->tableExists($table)) {
      if ($schema
        ->fieldExists($table, $field)) {
        $schema
          ->changeField($table, $field, $field, $spec);
      }
    }
  }
}

/**
 * Adds extra indexes to menu_entity_index table.
 */
function menu_entity_index_update_8103() {
  $table = 'menu_entity_index';
  $indexes = [
    'target_id' => [
      'target_id',
    ],
    'target_type' => [
      'target_type',
    ],
    'target_langcode' => [
      'target_langcode',
    ],
  ];
  $schema = Database::getConnection()
    ->schema();
  if (!$schema) {
    return;
  }
  if (!$schema
    ->tableExists($table)) {
    return;
  }
  $table_schema = [
    'fields' => [
      'target_type' => [
        'description' => 'The entity type of the target entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'target_id' => [
        'description' => 'The entity id of the target entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'target_langcode' => [
        'description' => 'The language code of the target entity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'target_id' => [
        'target_id',
      ],
      'target_type' => [
        'target_type',
      ],
      'target_langcode' => [
        'target_langcode',
      ],
    ],
  ];
  foreach ($indexes as $index_name => $fields) {
    if (!$schema
      ->indexExists($table, $index_name)) {
      $schema
        ->addIndex($table, $index_name, $fields, $table_schema);
    }
  }
}

/**
 * Skipped in favor of post update hook "reindex_8x1_1".
 */
function menu_entity_index_update_8104() {

  // @note: Post update hook "reindex_8x1_1" will reinitialize the index table
  // once more, so for anyone that hasn't applied this update, yet, it will do
  // nothing in this release.
}

Functions

Namesort descending Description
menu_entity_index_schema Implements hook_schema().
menu_entity_index_update_8101 Adds langcode fields to primary index of menu_entity_index database table.
menu_entity_index_update_8102 Updates parent_id field definition of menu_entity_index database table.
menu_entity_index_update_8103 Adds extra indexes to menu_entity_index table.
menu_entity_index_update_8104 Skipped in favor of post update hook "reindex_8x1_1".