Skip to content

Commit

Permalink
Adding API product field in API Doc
Browse files Browse the repository at this point in the history
  • Loading branch information
phdhiren committed Dec 14, 2021
1 parent bbfc357 commit e9f71b3
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apigee_api_catalog.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ core_version_requirement: ^8.8.0 || ^9
package: 'Apigee'
dependencies:
- drupal:text
- drupal:entity
- drupal:file
- drupal:user
- drupal:node
- drupal:path
- drupal:options
- drupal:file_link
- drupal:apigee_edge
7 changes: 7 additions & 0 deletions apigee_api_catalog.install
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ function apigee_api_catalog_update_8806() {
function apigee_api_catalog_update_8807() {
return \Drupal::service('apigee_api_catalog.updates')->update8807();
}

/**
* Add API Product field.
*/
function apigee_api_catalog_update_8808() {
return \Drupal::service('apigee_api_catalog.updates')->update8808();
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"require": {
"php": ">=7.1",
"drupal/entity": "^1.1",
"drupal/file_link": "^2.0"
"drupal/file_link": "^2.0",
"drupal/apigee_edge": "^2.0"
},
"require-dev": {
"cweagans/composer-patches": "^1.6",
Expand Down
11 changes: 11 additions & 0 deletions config/install/core.entity_form_display.node.apidoc.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ status: true
dependencies:
config:
- field.field.node.apidoc.body
- field.field.node.apidoc.field_api_product
- field.field.node.apidoc.field_apidoc_fetched_timestamp
- field.field.node.apidoc.field_apidoc_file_link
- field.field.node.apidoc.field_apidoc_spec
Expand Down Expand Up @@ -54,6 +55,16 @@ content:
third_party_settings: { }
type: options_select
region: content
field_api_product:
weight: 7
settings:
match_operator: CONTAINS
match_limit: 10
size: 60
placeholder: ''
third_party_settings: { }
type: entity_reference_autocomplete
region: content
path:
type: path
weight: 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ status: true
dependencies:
config:
- field.field.node.apidoc.body
- field.field.node.apidoc.field_api_product
- field.field.node.apidoc.field_apidoc_fetched_timestamp
- field.field.node.apidoc.field_apidoc_file_link
- field.field.node.apidoc.field_apidoc_spec
Expand Down Expand Up @@ -42,3 +43,4 @@ hidden:
field_apidoc_file_link: true
field_apidoc_spec_file_source: true
field_apidoc_spec_md5: true
field_api_product: true
22 changes: 22 additions & 0 deletions config/install/field.field.node.apidoc.field_api_product.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_api_product
- node.type.apidoc
id: node.apidoc.field_api_product
field_name: field_api_product
entity_type: node
bundle: apidoc
label: 'API Product'
description: 'Apigee API Product'
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:api_product'
handler_settings:
target_bundles: null
auto_create: false
field_type: entity_reference
22 changes: 22 additions & 0 deletions config/install/field.storage.node.field_api_product.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
langcode: en
status: true
dependencies:
enforced:
module:
- apigee_api_catalog
module:
- apigee_edge
- node
id: node.field_api_product
field_name: field_api_product
entity_type: node
type: entity_reference
settings:
target_type: api_product
module: apigee_api_catalog
locked: false
cardinality: -1
translatable: false
indexes: { }
persist_with_no_fields: false
custom_storage: false
66 changes: 66 additions & 0 deletions src/UpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Utility\UpdateException;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

Expand Down Expand Up @@ -347,6 +349,70 @@ public function update8807() {
return 'Updated field_apidoc_spec_file_source required attribute to false.';
}

/**
* This will add the field API Product.
*/
public function update8808() {
$module = 'apigee_api_catalog';
$configPath = drupal_get_path('module', $module) . '/config';
$configToImport['install'] = [
'node.type.apidoc',
'field.field.node.apidoc.field_api_product',
'core.entity_form_display.node.apidoc.default',
'core.entity_view_display.node.apidoc.default',
];
if (!$this->moduleHandler->moduleExists('apigee_edge')) {
throw new UpdateException('Apigee Edge is required to add API Product field, install the Apigee Edge and update again.');
}

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_api_product',
];
$source = new FileStorage("$configPath/install");
foreach ($fields as $field) {

if (!FieldStorageConfig::loadByName('node', $field)) {
$contents = $source->read("field.storage.node.$field");

$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();
}
}

// Display field_api_product on the form display.
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.apidoc.default')
->setComponent('field_api_product', ['weight' => 7])
->save();

$this->entityTypeManager->clearCachedDefinitions();

return 'Added API Product field for apidoc.';

}

/**
* Get the field map from apidoc fields to node fields.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Kernel/ApidocEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ApidocEntityTest extends KernelTestBase {
'link',
'file_link',
'path_alias',
'apigee_edge',
'apigee_api_catalog',
];

Expand All @@ -72,6 +73,7 @@ protected function setUp() {
$this->installEntitySchema('node');
$this->installEntitySchema('node_type');
$this->installEntitySchema('path_alias');
$this->installEntitySchema('apigee_edge');
$this->installConfig(static::$modules);

$this->entityTypeManager = $this->container->get('entity_type.manager');
Expand Down

0 comments on commit e9f71b3

Please sign in to comment.