stacks-8.x-1.x-dev/stacks.install
stacks.install
<?php
use Drupal\stacks\Entity\WidgetInstanceEntity;
/**
* Implements hook_install().
*/
function stacks_install() {
$spec_widget_entity = [
'fields' => [
'id' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'status' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['id'],
'indexes' => [],
];
$spec_widget_instance_entity = [
'fields' => [
'id' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'enable_sharing' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'title' => [
'type' => 'varchar',
],
],
'primary key' => ['id'],
'indexes' => [],
];
\Drupal::database()->schema()->addIndex('widget_entity', 'status', ['status'], $spec_widget_entity);
\Drupal::database()->schema()->addIndex('widget_instance_entity', 'enable_sharing', ['enable_sharing'], $spec_widget_instance_entity);
\Drupal::database()->schema()->addIndex('widget_instance_entity', 'title', ['title'], $spec_widget_instance_entity);
}
/**
* Recreates widget types and their definitions.
*/
function stacks_update_8001() {
// Get configuration set for all widget types
$config_sets = \Drupal::configFactory()->listAll('stacks.widget_entity_type');
foreach ($config_sets as $config_set) {
$editable_config = \Drupal::configFactory()->getEditable($config_set);
// Set proper widget handlers to each widget type
if (strpos($config_set, '.contentfeed')) {
$editable_config->set('plugin', 'content_feed')->save();
}
elseif (strpos($config_set, '.article_content')) {
$editable_config->set('plugin', 'article_content')->save();
}
else {
$editable_config->set('plugin', 'default_widget')->save();
}
}
\Drupal::messenger()->addStatus(t('Widget Types mapped succesfully.'));
}
/**
* Add field status to Widget Instance Entity.
*/
function stacks_update_8002() {
\Drupal::entityDefinitionUpdateManager()->getChangeList();
}
/**
* Set default value in field status for old Widget Instance Entities.
*/
function stacks_update_8003(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['max'] = \Drupal::entityQuery('widget_instance_entity')->count()->execute();
}
$query = \Drupal::entityQuery('widget_instance_entity')
->sort('id')
->range($sandbox['progress'], 10);
$entities_id = $query->execute();
$widget_instance_entities = WidgetInstanceEntity::loadMultiple($entities_id);
/** @var \Drupal\stacks\Entity\WidgetInstanceEntity $widget_instance_entity */
foreach ($widget_instance_entities as $widget_instance_entity) {
$widget_instance_entity->setStatus(TRUE)->save();
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
$sandbox['progress'] += count($widget_instance_entities);
return t("Updated @count entities", ['@count' => $sandbox['progress']]);
}
/**
* Enable jquery_ui and jquery_ui_tabs new dependencies.
*/
function stacks_update_9200(&$sandbox) {
\Drupal::service('module_installer')->install(['jquery_ui', 'jquery_ui_tabs']);
}
