Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding API product field in API Doc #144

Merged
merged 4 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ public function drupalVersion($drupalCoreVersion)
default:
break;
}
$config->extra->{"patches"}->{"drupal/core"}->{"Support entities that are neither content nor config entities"} = 'https://www.drupal.org/files/issues/2020-12-02/3042467-50.patch';

file_put_contents('composer.json', json_encode($config, JSON_PRETTY_PRINT));
}
Expand Down
1 change: 1 addition & 0 deletions apigee_api_catalog.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dependencies:
- 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
1 change: 1 addition & 0 deletions tests/src/Functional/ApiDocsJsonApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ApiDocsJsonApi extends BrowserTestBase {
* @var array
*/
public static $modules = [
'apigee_edge',
'apigee_api_catalog',
'jsonapi',
'basic_auth',
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,8 @@ class ApidocEntityTest extends KernelTestBase {
'link',
'file_link',
'path_alias',
'key',
'apigee_edge',
'apigee_api_catalog',
];

Expand Down