public static function BlocksController::importBlocksSafe in Structure Sync 8
Same name and namespace in other branches
- 2.x src/Controller/BlocksController.php \Drupal\structure_sync\Controller\BlocksController::importBlocksSafe()
Function to import blocks safely (only adding what isn't already there).
1 call to BlocksController::importBlocksSafe()
- BlocksController::importBlocks in src/
Controller/ BlocksController.php - Function to import custom blocks.
File
- src/
Controller/ BlocksController.php, line 343
Class
- BlocksController
- Controller for syncing custom blocks.
Namespace
Drupal\structure_sync\ControllerCode
public static function importBlocksSafe($blocks, &$context) {
$blocksFiltered = $blocks;
$entities = StructureSyncHelper::getEntityManager()
->getStorage('block_content')
->loadMultiple();
foreach ($entities as $entity) {
for ($i = 0; $i < count($blocks); $i++) {
if ($entity
->uuid() === $blocks[$i]['uuid']) {
unset($blocksFiltered[$i]);
}
}
}
foreach ($blocksFiltered as $block) {
$blockContent = BlockContent::create([
'info' => $block['info'],
'langcode' => $block['langcode'],
'uuid' => $block['uuid'],
'type' => $block['bundle'],
]);
if (array_key_exists('fields', $block)) {
foreach ($block['fields'] as $fieldName => $fieldValue) {
$blockContent
->set($fieldName, $fieldValue);
}
}
$blockContent
->save();
if (array_key_exists('drush', $context) && $context['drush'] === TRUE) {
drush_log('Imported "' . $block['info'] . '"', 'ok');
}
StructureSyncHelper::logMessage('Imported "' . $block['info'] . '"');
}
}