You are here

public function UpdateService::update8804 in Apigee API Catalog 8.2

Convert API Doc entities to nodes, migrating data.

Parameters

array|null $sandbox: The sandbox for batch operations.

File

src/UpdateService.php, line 236

Class

UpdateService
Class UpdateService.

Namespace

Drupal\apigee_api_catalog

Code

public function update8804(&$sandbox) {

  // If 1.x was never installed, there is no apidoc entity saved previously.
  $entity_update_manager = \Drupal::entityDefinitionUpdateManager();
  if (!$entity_update_manager
    ->getEntityType('apidoc')) {
    $sandbox['#finished'] = 1;
    return 'No API Doc entities found, no data migration needed.';
  }
  $fieldMap = $this
    ->getFieldMap();
  $nodeStorage = $this->entityTypeManager
    ->getStorage('node');
  $apidocStorage = $this->entityTypeManager
    ->getStorage('apidoc');
  if (!isset($sandbox['progress'])) {
    $query = $apidocStorage
      ->getQuery();
    $total = $query
      ->count()
      ->execute();
    $sandbox['progress'] = 0;
    $sandbox['total'] = $total;
  }
  if (empty($sandbox['total'])) {
    $sandbox['#finished'] = 1;
    return 'No API Doc entities found, no data migration needed.';
  }

  // Migrate in chunks of 20.
  $query = $apidocStorage
    ->getQuery()
    ->sort('id')
    ->range($sandbox['progress'], 20);
  $ids = $query
    ->execute();
  $apidocs = $apidocStorage
    ->loadMultiple($ids);
  foreach ($apidocs as $apidoc) {
    $values = [
      'type' => 'apidoc',
      'title' => $apidoc
        ->label(),
      'body' => [
        'value' => $apidoc->description->value,
        'format' => 'full_html',
      ],
      'status' => $apidoc->status->value,
      'created' => $apidoc->created->value,
      'changed' => $apidoc->changed->value,
      'field_apidoc_spec_file_source' => $apidoc->spec_file_source->value,
      'field_apidoc_spec_md5' => $apidoc->spec_md5->value,
      'field_apidoc_fetched_timestamp' => $apidoc->fetched_timestamp->value,
      'field_apidoc_spec' => $apidoc->spec
        ->getValue(),
      'field_apidoc_file_link' => $apidoc->file_link
        ->getValue(),
    ];
    foreach ($fieldMap as $old => $new) {
      $values[$new] = $apidoc->{$old}
        ->getValue();
    }
    $node = $nodeStorage
      ->create($values);
    $node
      ->save();
    $apidoc
      ->delete();
    $sandbox['progress']++;
  }
  $sandbox['#finished'] = $sandbox['progress'] / $sandbox['total'];
  return 'Converted API Doc entities to nodes, migrating data.';
}