acquia_commercemanager-8.x-1.122/modules/acm_sku/acm_sku.install
modules/acm_sku/acm_sku.install
<?php
/**
* @file
* Contains install hooks for acm_sku.
*/
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\Entity\Role;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_update_N().
*
* Manage stock through separate module acm_sku_stock.
*/
function acm_sku_update_8018() {
\Drupal::service('module_installer')->install(['acm_sku_stock']);
$db = \Drupal::database();
// Copy existing stock data from SKU entity to new table.
$query = $db->select('acm_sku_field_data', 'sku');
$query->fields('sku', ['sku', 'stock']);
$result = $query->execute()->fetchAllAssoc('sku');
// To start with we will let everything be handled by quantity only.
foreach ($result as $row) {
$db->merge('acm_sku_stock')
->key(['sku' => $row->sku])
->fields(['quantity' => $row->stock, 'status' => 1])
->execute();
}
// Apply updates to remove stock field.
\Drupal::service('entity.definition_update_manager')->applyUpdates();
}
/**
* Implements hook_install().
*/
function acm_sku_install() {
// Add permissions into ACM role.
$role = Role::load('acm');
if (!is_null($role)) {
$permissions = [
'delete terms in acm_product_category',
'delete terms in sku_product_option',
'edit terms in acm_product_category',
'edit terms in sku_product_option',
'create terms in acm_product_category',
'create terms in sku_product_option',
'restful post acm_categorysync',
'restful post acm_productsync',
];
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->save();
}
}
/**
* Implements hook_uninstall().
*/
function acm_sku_uninstall() {
// Remove permissions from ACM role.
$role = Role::load('acm');
if (!is_null($role)) {
$permissions = [
'delete terms in acm_product_category',
'delete terms in sku_product_option',
'edit terms in acm_product_category',
'edit terms in sku_product_option',
'create terms in acm_product_category',
'create terms in sku_product_option',
'restful post acm_categorysync',
'restful post acm_productsync',
];
foreach ($permissions as $permission) {
if ($role->hasPermission($permission)) {
$role->revokePermission($permission);
}
}
$role->save();
}
}
/**
* Implements hook_update_N().
*/
function acm_sku_update_8001() {
$config_path = drupal_get_path('module', 'acm_sku') . '/config/install/acm_sku.configurable_form_settings.yml';
$data = Yaml::parse((file_get_contents($config_path)));
\Drupal::configFactory()
->getEditable('acm_sku.configurable_form_settings')
->setData($data)
->save(TRUE);
}
/**
* Add status field to acm_sku table.
*
* This is required before applying entity updates.
* Otherwise, 'drush entup' throws an error -
* "Null value not allowed"
*/
function acm_sku_update_8101() {
try {
$status_storage_schema = [
'acm_sku' => [
'fields' => [
'status' => [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
],
],
],
];
\Drupal::keyValue('entity.storage_schema.sql')
->set('acm_sku.field_schema_data.status', $status_storage_schema);
$database = \Drupal::database();
$table_name = 'acm_sku';
$column_name = 'status';
if ($database->schema()->tableExists($table_name) && !$database->schema()->fieldExists($table_name, $column_name)) {
$database->schema()->addField($table_name, $column_name, [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'description' => 'Whether the SKU is available or not.',
'default' => 1,
]);
\Drupal::entityDefinitionUpdateManager()->applyUpdates();
}
}
catch (\Exception $e) {
\Drupal::logger('acm_sku')->error($e->getMessage());
}
}
/**
* Change type of attributes field.
*/
function acm_sku_update_8102() {
try {
$database = \Drupal::database();
$asa_table_name = $database->tablePrefix('acm_sku__attributes') . 'acm_sku__attributes';
// Clone the DB table.
$database->query("CREATE TABLE acm_sku__attributes_clone SELECT * FROM $asa_table_name")->execute();
// Wipe it.
$database->truncate('acm_sku__attributes')->execute();
// Do the magic.
$field_definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions('acm_sku', 'acm_sku');
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $attributes_field_def */
$attributes_field_def = $field_definitions['attributes'];
\Drupal::entityDefinitionUpdateManager()
->updateFieldStorageDefinition($attributes_field_def);
// Restore the DB table.
$database->query("INSERT INTO $asa_table_name SELECT * FROM acm_sku__attributes_clone")->execute();
// Drop cloned table.
$database->query('DROP TABLE acm_sku__attributes_clone')->execute();
}
catch (\Exception $e) {
\Drupal::logger('acm_sku')->error($e->getMessage());
}
}
/**
* Create 'Include in Menu' field.
*/
function acm_sku_update_8103() {
$entityTypeManager = \Drupal::entityTypeManager();
$storages = $entityTypeManager->getStorage('field_storage_config')
->loadByProperties(['id' => 'taxonomy_term.field_category_include_menu']);
if (empty($storages)) {
$storage = FieldStorageConfig::create([
'field_name' => 'field_category_include_menu',
'entity_type' => 'taxonomy_term',
'type' => 'boolean',
'cardinality' => '1',
'translatable' => TRUE,
]);
$storage->save();
}
else {
$storage = reset($storages);
}
$fields = $entityTypeManager->getStorage('field_config')
->loadByProperties(['id' => 'taxonomy_term.acm_product_category.field_category_include_menu']);
if (empty($fields)) {
FieldConfig::create([
'field_storage' => $storage,
'bundle' => 'acm_product_category',
'label' => 'Include in Menu',
'required' => FALSE,
'translatable' => TRUE,
'settings' => [
'on_label' => 'Yes',
'off_label' => 'No',
],
])->save();
}
}
/**
* Implements hook_update_N().
*/
function acm_sku_update_8104() {
// Apply updates to add stock field.
\Drupal::service('entity.definition_update_manager')->applyUpdates();
$config_yaml = Yaml::parse(file_get_contents(drupal_get_path('module', 'acm_sku') . '/config/install/rest.resource.acm_productstocksync.yml'));
\Drupal::configFactory()
->getEditable('rest.resource.acm_productstocksync')
->setData($config_yaml)
->save();
}
/**
* Add taxonomy-related and product REST-related permissions to ACM role.
*/
function acm_sku_update_8105() {
// Add permissions into ACM role.
$role = Role::load('acm');
if (!is_null($role)) {
$permissions = [
'delete terms in acm_product_category',
'delete terms in sku_product_option',
'edit terms in acm_product_category',
'edit terms in sku_product_option',
'create terms in acm_product_category',
'create terms in sku_product_option',
'restful post acm_categorysync',
'restful post acm_productsync',
];
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->save();
}
}
/**
* Implements hook_update_N().
*
* Add field to store commerce category status.
*/
function acm_sku_update_8106() {
$config_path = drupal_get_path('module', 'acm_sku') . '/config/install/';
$storage_yaml = Yaml::parse(file_get_contents($config_path . 'field.storage.taxonomy_term.field_commerce_status.yml'));
// Some issue with array conversion in allowed values, we handle
// exception with workaround for now. We'll re-save it later.
$storage_yaml['settings']['allowed_values'] = [];
// Create field storage config.
FieldStorageConfig::create($storage_yaml)->save();
// Reload config from yaml.
$storage_yaml = Yaml::parse(file_get_contents($config_path . 'field.storage.taxonomy_term.field_commerce_status.yml'));
// Update config in DB.
\Drupal::configFactory()->getEditable('field.storage.taxonomy_term.field_commerce_status')->setData($storage_yaml)->save();
// Load field storage config and save again.
$field_storage = FieldStorageConfig::loadByName($storage_yaml['entity_type'], $storage_yaml['field_name']);
$field_storage->save();
// Save the Field Config.
$field_yaml = Yaml::parse(file_get_contents($config_path . 'field.field.taxonomy_term.acm_product_category.field_commerce_status.yml'));
FieldConfig::create($field_yaml)->save();
}
/**
* Implements hook_update_N().
*
* Make price, final_price and special_price non-translatable.
*/
function acm_sku_update_8107() {
\Drupal::entityDefinitionUpdateManager()->applyUpdates();
}
/**
* Implements hook_update_N().
*
* Add attribute set as parent key for attribute_weights.
*/
function acm_sku_update_8108() {
$config = \Drupal::configFactory()->getEditable('acm_sku.configurable_form_settings');
$config->set('attribute_weights', ['default' => $config->get('attribute_weights')]);
$config->save(TRUE);
}
/**
* Implements hook_update_N().
*
* Make field_configured_skus non-translatable.
*/
function acm_sku_update_8109() {
$field = FieldConfig::loadByName('acm_sku', 'configurable', 'field_configured_skus');
$field->setTranslatable(FALSE);
$field->save();
$field = FieldConfig::loadByName('acm_sku', 'variant', 'field_configured_skus');
$field->setTranslatable(FALSE);
$field->save();
}
