You are here

function social_content_block_update_8002 in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_block/social_content_block.install \social_content_block_update_8002()
  2. 10.3.x modules/social_features/social_content_block/social_content_block.install \social_content_block_update_8002()
  3. 10.0.x modules/social_features/social_content_block/social_content_block.install \social_content_block_update_8002()
  4. 10.1.x modules/social_features/social_content_block/social_content_block.install \social_content_block_update_8002()
  5. 10.2.x modules/social_features/social_content_block/social_content_block.install \social_content_block_update_8002()

Fill in fields for plugin ID and plugin fields in existing blocks.

File

modules/social_features/social_content_block/social_content_block.install, line 315
Install, update and uninstall functions for the social_content_block module.

Code

function social_content_block_update_8002(&$sandbox) {
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = \Drupal::entityQuery('block_content')
      ->condition('type', 'custom_content_list')
      ->notExists('field_plugin_id')
      ->count()
      ->execute();
    if (!$sandbox['total']) {
      return;
    }
    $sandbox['processed'] = 0;
    $sandbox['plugin_id'] = 'topic_content_block';
    $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);

    /** @var \Drupal\social_content_block\ContentBlockManagerInterface $content_block_manager */
    $content_block_manager = \Drupal::service('plugin.manager.content_block');
    $definition = $content_block_manager
      ->getDefinition($sandbox['plugin_id']);
    $sandbox['fields'] = $definition['fields'];
  }
  $entity_ids = \Drupal::entityQuery('block_content')
    ->condition('type', 'custom_content_list')
    ->notExists('field_plugin_id')
    ->sort('id')
    ->range(0, $sandbox['limit'])
    ->execute();
  $storage = \Drupal::entityTypeManager()
    ->getStorage('block_content');
  foreach ($entity_ids as $entity_id) {

    /** @var \Drupal\block_content\BlockContentInterface $entity */
    $entity = $storage
      ->load($entity_id);
    $entity->field_plugin_id->value = $sandbox['plugin_id'];
    $fields = [];
    foreach ($sandbox['fields'] as $field) {
      if (!$entity
        ->get($field)
        ->isEmpty()) {
        $fields[] = $field;
      }
    }
    if (count($fields) === 1) {
      $entity->field_plugin_field->value = reset($fields);
    }
    $entity
      ->save();
  }
  $sandbox['processed'] += count($entity_ids);
  $sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
}