|
28 | 28 | use Drupal\Core\Extension\ModuleHandlerInterface;
|
29 | 29 | use Drupal\Core\Entity\EntityTypeManagerInterface;
|
30 | 30 | use Drupal\Core\Entity\EntityFieldManagerInterface;
|
| 31 | +use Drupal\Core\Field\BaseFieldDefinition; |
| 32 | +use Drupal\Core\Utility\UpdateException; |
31 | 33 | use Drupal\field\Entity\FieldConfig;
|
32 | 34 | use Drupal\field\Entity\FieldStorageConfig;
|
33 | 35 |
|
@@ -347,6 +349,70 @@ public function update8807() {
|
347 | 349 | return 'Updated field_apidoc_spec_file_source required attribute to false.';
|
348 | 350 | }
|
349 | 351 |
|
| 352 | + /** |
| 353 | + * This will add the field API Product. |
| 354 | + */ |
| 355 | + public function update8808() { |
| 356 | + $module = 'apigee_api_catalog'; |
| 357 | + $configPath = drupal_get_path('module', $module) . '/config'; |
| 358 | + $configToImport['install'] = [ |
| 359 | + 'node.type.apidoc', |
| 360 | + 'field.field.node.apidoc.field_api_product', |
| 361 | + 'core.entity_form_display.node.apidoc.default', |
| 362 | + 'core.entity_view_display.node.apidoc.default', |
| 363 | + ]; |
| 364 | + if (!$this->moduleHandler->moduleExists('apigee_edge')) { |
| 365 | + throw new UpdateException('Apigee Edge is required to add API Product field, install the Apigee Edge and update again.'); |
| 366 | + } |
| 367 | + |
| 368 | + foreach ($configToImport as $dir => $configs) { |
| 369 | + foreach ($configs as $config) { |
| 370 | + if (!$this->configFactory->listAll($config)) { |
| 371 | + $raw = file_get_contents("$configPath/$dir/$config.yml"); |
| 372 | + $data = Yaml::decode($raw); |
| 373 | + $this->configFactory |
| 374 | + ->getEditable($config) |
| 375 | + ->setData($data) |
| 376 | + ->set('uuid', $this->uuid->generate()) |
| 377 | + ->save(TRUE); |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + $fields = [ |
| 383 | + 'field_api_product', |
| 384 | + ]; |
| 385 | + $source = new FileStorage("$configPath/install"); |
| 386 | + foreach ($fields as $field) { |
| 387 | + |
| 388 | + if (!FieldStorageConfig::loadByName('node', $field)) { |
| 389 | + $contents = $source->read("field.storage.node.$field"); |
| 390 | + |
| 391 | + $this->entityTypeManager->getStorage('field_storage_config') |
| 392 | + ->create($contents) |
| 393 | + ->save(); |
| 394 | + } |
| 395 | + |
| 396 | + if (!FieldConfig::loadByName('node', 'apidoc', $field)) { |
| 397 | + $this->entityTypeManager->getStorage('field_config') |
| 398 | + ->create($source->read("field.field.node.apidoc.$field")) |
| 399 | + ->save(); |
| 400 | + } |
| 401 | + } |
| 402 | + |
| 403 | + // Display field_api_product on the form display. |
| 404 | + \Drupal::entityTypeManager() |
| 405 | + ->getStorage('entity_form_display') |
| 406 | + ->load('node.apidoc.default') |
| 407 | + ->setComponent('field_api_product', ['weight' => 7]) |
| 408 | + ->save(); |
| 409 | + |
| 410 | + $this->entityTypeManager->clearCachedDefinitions(); |
| 411 | + |
| 412 | + return 'Added API Product field for apidoc.'; |
| 413 | + |
| 414 | + } |
| 415 | + |
350 | 416 | /**
|
351 | 417 | * Get the field map from apidoc fields to node fields.
|
352 | 418 | *
|
|
0 commit comments