entity_value_inheritance-1.3.0/entity_value_inheritance.install
entity_value_inheritance.install
<?php
/**
* @file
* Entity Value Inheritance Install File.
*/
/**
* Implements hook_schema().
*/
function entity_value_inheritance_schema() {
$schema['evi_overrides'] = [
'description' => 'Tracks the override details for entities',
'fields' => [
'key' => [
'type' => 'varchar',
'not null' => TRUE,
'length' => 255,
'description' => 'Key used for updates.',
],
'inheritance' => [
'type' => 'varchar',
'not null' => TRUE,
'length' => 255,
'description' => 'Inheritance Entity ID',
],
'entity_type' => [
'type' => 'varchar',
'not null' => TRUE,
'length' => 255,
'description' => 'Entity Type',
],
'entity_id' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Entity ID for the Destination.',
],
'override' => [
'type' => 'int',
'not null' => TRUE, // NOTE: see heads-up below about this typo.
'description' => 'Override value for field',
'default' => 0,
],
],
'indexes' => [
// Speeds up WHERE entity_type = ? AND entity_id = ?
'evi_overrides_et_eid' => ['entity_type', 'entity_id'],
// Helpful when also grouping/associating by inheritance.
'evi_overrides_et_eid_inh' => ['entity_type', 'entity_id', 'inheritance'],
],
'primary key' => ['key'],
];
return $schema;
}
/**
* Create the evi_overrides table.
*/
function entity_value_inheritance_update_10001() {
$tables = entity_value_inheritance_schema();
if (!\Drupal::database()->schema()->tableExists('evi_overrides')) {
\Drupal::database()->schema()->createTable('evi_overrides', $tables['evi_overrides']);
}
}
/**
* Install Action and Entity modules if not enabled.
*/
function entity_value_inheritance_update_10002() {
$modules = [];
if (!\Drupal::moduleHandler()->moduleExists('action')) {
$modules[] = 'action';
}
if (!\Drupal::moduleHandler()->moduleExists('entity')) {
$modules[] = 'entity';
}
if (!empty($modules)) {
\Drupal::service('module_installer')->install($modules);
}
}
/**
* Add composite indexes for evi_overrides lookups.
*/
function entity_value_inheritance_update_10003() {
// Load field specs so MySQL can normalize the index during updates.
$tables = entity_value_inheritance_schema();
$fields = $tables['evi_overrides']['fields'];
$connection = \Drupal::database();
$schema = $connection->schema();
if (!$schema->tableExists('evi_overrides')) {
// Table missing (module not fully installed); nothing to do.
return;
}
// Ensure (entity_type, entity_id) index exists.
if (!$schema->indexExists('evi_overrides', 'evi_overrides_et_eid')) {
$schema->addIndex('evi_overrides', 'evi_overrides_et_eid', ['entity_type', 'entity_id'], [
'fields' => [
'entity_type' => $fields['entity_type'],
'entity_id' => $fields['entity_id'],
],
]);
}
// Ensure (entity_type, entity_id, inheritance) index exists.
if (!$schema->indexExists('evi_overrides', 'evi_overrides_et_eid_inh')) {
$schema->addIndex('evi_overrides', 'evi_overrides_et_eid_inh', ['entity_type', 'entity_id', 'inheritance'], [
'fields' => [
'entity_type' => $fields['entity_type'],
'entity_id' => $fields['entity_id'],
'inheritance' => $fields['inheritance'],
],
]);
}
}
