You are here

public static function BlocksController::importBlocksFull in Structure Sync 8

Same name and namespace in other branches
  1. 2.x src/Controller/BlocksController.php \Drupal\structure_sync\Controller\BlocksController::importBlocksFull()

Function to fully import the custom blocks.

Basically a safe import with update actions for already existing custom blocks.

1 call to BlocksController::importBlocksFull()
BlocksController::importBlocks in src/Controller/BlocksController.php
Function to import custom blocks.

File

src/Controller/BlocksController.php, line 259

Class

BlocksController
Controller for syncing custom blocks.

Namespace

Drupal\structure_sync\Controller

Code

public static function importBlocksFull($blocks, &$context) {
  $uuidsInConfig = [];
  foreach ($blocks as $block) {
    $uuidsInConfig[] = $block['uuid'];
  }
  $entities = [];
  if (!empty($uuidsInConfig)) {
    $query = StructureSyncHelper::getEntityQuery('block_content');
    $query
      ->condition('uuid', $uuidsInConfig, 'IN');
    $ids = $query
      ->execute();
    $controller = StructureSyncHelper::getEntityManager()
      ->getStorage('block_content');
    $entities = $controller
      ->loadMultiple($ids);
  }
  $context['sandbox']['max'] = count($blocks);
  $context['sandbox']['progress'] = 0;
  foreach ($blocks as $block) {
    $query = StructureSyncHelper::getEntityQuery('block_content');
    $query
      ->condition('uuid', $block['uuid']);
    $ids = $query
      ->execute();
    if (count($ids) <= 0) {
      $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'] . '"');
    }
    else {
      foreach ($entities as $entity) {
        if ($block['uuid'] === $entity
          ->uuid()) {
          $blockContent = BlockContent::load($entity
            ->id());
          if (!empty($blockContent)) {
            $blockContent
              ->setInfo($block['info'])
              ->set('langcode', $block['langcode']);
            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('Updated "' . $block['info'] . '"', 'ok');
          }
          StructureSyncHelper::logMessage('Updated "' . $block['info'] . '"');
          break;
        }
      }
    }
    $context['sandbox']['progress']++;
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
  $context['finished'] = 1;
}