kb-8.x-1.x-dev/kb.install
kb.install
<?php
/**
* Created by PhpStorm.
* User: cerium
* Date: 2/10/18
* Time: 5:34 PM
*/
/**
* Implements hook_install().
*
* This will install/reinstall all of the defaults for this module - if you are using custom settings,
* please CLONE views and groups and use these as a template.
*/
function kb_install() {
\Drupal::service('config.installer')->installDefaultConfig('module', 'kb');
drupal_set_message(t('The KnowledgeBase Module has been installed.'), 'status', TRUE);
}
/**
* Implements hook_uninstall().
*/
function kb_uninstall() {
\Drupal::service('config.manager')->uninstall('module', 'kb');
}
/**
* Implements hook_update_N(). Updates body field to field_kb_text.
*
* BEFORE UPDATING: The module now requires views_jump_menu. Make sure you have this first.
*/
function kb_update_8101(&$sandbox) {
// Initialize some variables during the first pass through.
if (!isset($sandbox['total'])) {
if (!\Drupal::moduleHandler()->moduleExists('views_jump_menu')) {
\Drupal::service('module_installer')->install(['views_jump_menu']);
}
\Drupal::service('config.installer')->installDefaultConfig('module', 'kb');
$nids = \Drupal::entityQuery('node')->condition('type', 'kb_content')->execute();
$sandbox['total'] = count($nids);
$sandbox['current'] = 0;
}
$nodes_per_batch = 10;
$nids = \Drupal::entityQuery('node')
->condition('type', 'kb_content')
->range($sandbox['current'], $sandbox['current'] + $nodes_per_batch)
->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
foreach ($nodes as $node) {
$node->field_kb_text->setValue($node->body->getValue());
$node->save();
$sandbox['current']++;
}
drupal_set_message($sandbox['current'] . ' KB Items updated');
$sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
}