You are here

public function UpdateService::update8802 in Apigee API Catalog 8.2

Create API Doc node type and fields if updating from 1.x.

Return value

string A message to display.

File

src/UpdateService.php, line 104

Class

UpdateService
Class UpdateService.

Namespace

Drupal\apigee_api_catalog

Code

public function update8802() {
  $module = 'apigee_api_catalog';
  $configPath = drupal_get_path('module', $module) . '/config';
  $configToImport['install'] = [
    'node.type.apidoc',
    'core.base_field_override.node.apidoc.title',
    'field.field.node.apidoc.body',
    'core.entity_form_display.node.apidoc.default',
    'core.entity_view_display.node.apidoc.default',
  ];
  if ($this->moduleHandler
    ->moduleExists('views')) {
    $configToImport['optional'][] = 'views.view.api_catalog_admin';
    $configToImport['optional'][] = 'views.view.apigee_api_catalog';
  }
  foreach ($configToImport as $dir => $configs) {
    foreach ($configs as $config) {
      if (!$this->configFactory
        ->listAll($config)) {
        $raw = file_get_contents("{$configPath}/{$dir}/{$config}.yml");
        $data = Yaml::decode($raw);
        $this->configFactory
          ->getEditable($config)
          ->setData($data)
          ->set('uuid', $this->uuid
          ->generate())
          ->save(TRUE);
      }
    }
  }
  $fields = [
    'field_apidoc_spec_md5',
    'field_apidoc_fetched_timestamp',
    'field_apidoc_spec_file_source',
    'field_apidoc_spec',
    'field_apidoc_file_link',
  ];
  $source = new FileStorage("{$configPath}/install");
  foreach ($fields as $field) {
    if (!FieldStorageConfig::loadByName('node', $field)) {
      $contents = $source
        ->read("field.storage.node.{$field}");

      // Somehow it doesn't take allowed_values...
      if ($field == 'field_apidoc_spec_file_source') {
        $contents['settings']['allowed_values'] = [];
      }
      $this->entityTypeManager
        ->getStorage('field_storage_config')
        ->create($contents)
        ->save();
    }
    if (!FieldConfig::loadByName('node', 'apidoc', $field)) {
      $this->entityTypeManager
        ->getStorage('field_config')
        ->create($source
        ->read("field.field.node.apidoc.{$field}"))
        ->save();
    }
  }

  // Add back allowed_values to field_apidoc_spec_file_source.
  if ($field = FieldStorageConfig::loadByName('node', 'field_apidoc_spec_file_source')) {
    $field
      ->setSetting('allowed_values', [
      'file' => 'File',
      'url' => 'URL',
    ])
      ->save();
  }
  $this->entityTypeManager
    ->clearCachedDefinitions();
  return 'Created API Doc node type and fields.';
}