public function BlocksController::importBlocks in Structure Sync 8
Same name and namespace in other branches
- 2.x src/Controller/BlocksController.php \Drupal\structure_sync\Controller\BlocksController::importBlocks()
Function to import custom blocks.
When this function is used without the designated form, you should assign an array with a key value pair for form with key 'style' and value 'full', 'safe' or 'force' to apply that import style.
File
- src/
Controller/ BlocksController.php, line 109
Class
- BlocksController
- Controller for syncing custom blocks.
Namespace
Drupal\structure_sync\ControllerCode
public function importBlocks(array $form, FormStateInterface $form_state = NULL) {
StructureSyncHelper::logMessage('Custom blocks import started');
// Check if the import style has been defined in the form (state) and else
// get it from the form array.
if (is_object($form_state) && $form_state
->hasValue('import_block_list')) {
$blocksSelected = $form_state
->getValue('import_block_list');
$blocksSelected = array_filter($blocksSelected, 'is_string');
}
if (array_key_exists('style', $form)) {
$style = $form['style'];
}
else {
StructureSyncHelper::logMessage('No style defined on custom blocks import', 'error');
return;
}
StructureSyncHelper::logMessage('Using "' . $style . '" style for custom blocks import');
// Get custom blocks from config.
$blocksConfig = $this->config
->get('blocks');
$blocks = [];
if (isset($blocksSelected)) {
foreach ($blocksConfig as $block) {
if (in_array($block['uuid'], array_keys($blocksSelected))) {
$blocks[] = $block;
}
}
}
else {
$blocks = $blocksConfig;
}
if (array_key_exists('drush', $form) && $form['drush'] === TRUE) {
$context = [];
$context['drush'] = TRUE;
switch ($style) {
case 'full':
self::deleteDeletedBlocks($blocks, $context);
self::importBlocksFull($blocks, $context);
self::blocksImportFinishedCallback(NULL, NULL, NULL);
break;
case 'safe':
self::importBlocksSafe($blocks, $context);
self::blocksImportFinishedCallback(NULL, NULL, NULL);
break;
case 'force':
self::deleteBlocks($context);
self::importBlocksForce($blocks, $context);
self::blocksImportFinishedCallback(NULL, NULL, NULL);
break;
}
return;
}
// Import the custom blocks with the chosen style of importing.
switch ($style) {
case 'full':
$batch = [
'title' => $this
->t('Importing custom blocks...'),
'operations' => [
[
'\\Drupal\\structure_sync\\Controller\\BlocksController::deleteDeletedBlocks',
[
$blocks,
],
],
[
'\\Drupal\\structure_sync\\Controller\\BlocksController::importBlocksFull',
[
$blocks,
],
],
],
'finished' => '\\Drupal\\structure_sync\\Controller\\BlocksController::blocksImportFinishedCallback',
];
batch_set($batch);
break;
case 'safe':
$batch = [
'title' => $this
->t('Importing custom blocks...'),
'operations' => [
[
'\\Drupal\\structure_sync\\Controller\\BlocksController::importBlocksSafe',
[
$blocks,
],
],
],
'finished' => '\\Drupal\\structure_sync\\Controller\\BlocksController::blocksImportFinishedCallback',
];
batch_set($batch);
break;
case 'force':
$batch = [
'title' => $this
->t('Importing custom blocks...'),
'operations' => [
[
'\\Drupal\\structure_sync\\Controller\\BlocksController::deleteBlocks',
[],
],
[
'\\Drupal\\structure_sync\\Controller\\BlocksController::importBlocksForce',
[
$blocks,
],
],
],
'finished' => '\\Drupal\\structure_sync\\Controller\\BlocksController::blocksImportFinishedCallback',
];
batch_set($batch);
break;
default:
StructureSyncHelper::logMessage('Style not recognized', 'error');
break;
}
}