@dropsh/plugin-display-builder 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
drupal-cli is dual-licensed. You may use this software under the terms of
|
|
2
|
+
either the MIT License or the GNU General Public License version 2 or any
|
|
3
|
+
later version, at your option.
|
|
4
|
+
|
|
5
|
+
SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
|
|
6
|
+
|
|
7
|
+
================================================================================
|
|
8
|
+
MIT License
|
|
9
|
+
================================================================================
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 Christian Wiedemann
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
|
|
31
|
+
================================================================================
|
|
32
|
+
GNU General Public License, version 2 or later
|
|
33
|
+
================================================================================
|
|
34
|
+
|
|
35
|
+
The full text of the GNU GPL v2 is available at:
|
|
36
|
+
https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
|
37
|
+
|
|
38
|
+
The full text of any later GPL version is available at:
|
|
39
|
+
https://www.gnu.org/licenses/
|
|
40
|
+
|
|
41
|
+
Copyright (C) 2026 Christian Wiedemann
|
|
42
|
+
|
|
43
|
+
This program is free software; you can redistribute it and/or modify it under
|
|
44
|
+
the terms of the GNU General Public License as published by the Free Software
|
|
45
|
+
Foundation; either version 2 of the License, or (at your option) any later
|
|
46
|
+
version.
|
|
47
|
+
|
|
48
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
49
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
50
|
+
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
51
|
+
details.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Display Builder DropSH Plugin
|
|
2
|
+
|
|
3
|
+
Extends JSON:API operation schemas for entity view displays managed by Display Builder.
|
|
4
|
+
|
|
5
|
+
## Drupal Requirements
|
|
6
|
+
|
|
7
|
+
Enable these Drupal modules on the target site:
|
|
8
|
+
|
|
9
|
+
- `display_builder`
|
|
10
|
+
- `display_builder_entity_view`
|
|
11
|
+
- `jsonapi_sdc`
|
|
12
|
+
|
|
13
|
+
## Metadata Sources
|
|
14
|
+
|
|
15
|
+
The plugin reads Display Builder metadata from standard JSON:API config resources:
|
|
16
|
+
|
|
17
|
+
```http
|
|
18
|
+
GET /jsonapi/entity_view_display/entity_view_display?filter[drupal_internal__id]={entity_type}.{bundle}.{view_mode}
|
|
19
|
+
GET /jsonapi/display_builder_profile/display_builder_profile?filter[drupal_internal__id]={profile_id}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The factory requests the `default` view mode while building operation schemas. Component metadata
|
|
23
|
+
comes from `jsonapi_sdc`; Display Builder profile `component_library` settings are emitted as
|
|
24
|
+
schema metadata so clients can derive the configured component selection without a patched endpoint.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { fetchSdcComponents } from "@dropsh/sdc-client";
|
|
2
|
+
import { HttpError } from "dropsh/plugin";
|
|
3
|
+
import { fetchDisplayBuilderMetadata } from "./metadata-client.js";
|
|
4
|
+
import { extendDisplayBuilderSchema } from "./schema.js";
|
|
5
|
+
export function displayBuilderPlugin() {
|
|
6
|
+
return {
|
|
7
|
+
id: "display-builder",
|
|
8
|
+
requiredModules: ["display_builder", "display_builder_entity_view", "jsonapi_sdc"],
|
|
9
|
+
async extendSchema(_entityType, _bundle, schema) {
|
|
10
|
+
return schema;
|
|
11
|
+
},
|
|
12
|
+
async extendOperationSchema(entityType, bundle, _operation, schema, ctx) {
|
|
13
|
+
const metadata = await fetchDisplayBuilderMetadata(ctx, entityType, bundle, "default");
|
|
14
|
+
if (!metadata.enabled || !metadata.overrideField) {
|
|
15
|
+
return schema;
|
|
16
|
+
}
|
|
17
|
+
let components;
|
|
18
|
+
try {
|
|
19
|
+
components = await fetchSdcComponents(ctx);
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
if (err instanceof HttpError && err.status === 404) {
|
|
23
|
+
throw new HttpError(404, "Display Builder plugin requires Drupal module jsonapi_sdc to build component schemas.", err.body);
|
|
24
|
+
}
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
return extendDisplayBuilderSchema(schema, metadata, components);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { HttpError } from "dropsh/plugin";
|
|
2
|
+
const invalidJsonMessage = "Display Builder JSON:API config response did not return valid JSON.";
|
|
3
|
+
const invalidMetadataObjectMessage = "Display Builder JSON:API config response did not return a valid object.";
|
|
4
|
+
function asObject(value) {
|
|
5
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
6
|
+
return {};
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
function isObject(value) {
|
|
11
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
function asString(value, fallback = "") {
|
|
14
|
+
return typeof value === "string" ? value : fallback;
|
|
15
|
+
}
|
|
16
|
+
async function fetchJson(ctx, url) {
|
|
17
|
+
const request = await ctx.auth.apply({
|
|
18
|
+
method: "GET",
|
|
19
|
+
url,
|
|
20
|
+
headers: { Accept: "application/vnd.api+json" },
|
|
21
|
+
});
|
|
22
|
+
const response = await ctx.http.send(request);
|
|
23
|
+
let body;
|
|
24
|
+
try {
|
|
25
|
+
body = JSON.parse(response.body);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new HttpError(502, invalidJsonMessage, response.body);
|
|
29
|
+
}
|
|
30
|
+
if (!isObject(body)) {
|
|
31
|
+
throw new HttpError(502, invalidMetadataObjectMessage, body);
|
|
32
|
+
}
|
|
33
|
+
return body;
|
|
34
|
+
}
|
|
35
|
+
function singleResource(body) {
|
|
36
|
+
if (!Array.isArray(body.data) || body.data.length === 0) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return asObject(body.data[0]);
|
|
40
|
+
}
|
|
41
|
+
async function fetchJsonApiConfigResource(ctx, resourceType, internalId) {
|
|
42
|
+
const baseUrl = ctx.baseUrl.replace(/\/+$/, "");
|
|
43
|
+
const body = await fetchJson(ctx, `${baseUrl}/jsonapi/${resourceType}/${resourceType}?filter%5Bdrupal_internal__id%5D=${encodeURIComponent(internalId)}`);
|
|
44
|
+
const resource = singleResource(body);
|
|
45
|
+
return resource ? asObject(resource.attributes) : null;
|
|
46
|
+
}
|
|
47
|
+
function configLabel(config, fallback) {
|
|
48
|
+
return asString(config.label, fallback);
|
|
49
|
+
}
|
|
50
|
+
function profileSummary(id, config) {
|
|
51
|
+
return {
|
|
52
|
+
id,
|
|
53
|
+
label: configLabel(config ?? {}, id),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Fetches Display Builder schema metadata from standard JSON:API config
|
|
58
|
+
* resources: entity_view_display and display_builder_profile.
|
|
59
|
+
*/
|
|
60
|
+
export async function fetchDisplayBuilderMetadata(ctx, entityType, bundle, viewMode = "default") {
|
|
61
|
+
const display = await fetchJsonApiConfigResource(ctx, "entity_view_display", `${entityType}.${bundle}.${viewMode}`);
|
|
62
|
+
if (!display) {
|
|
63
|
+
return { enabled: false };
|
|
64
|
+
}
|
|
65
|
+
const displayBuilderSettings = asObject(asObject(display.third_party_settings).display_builder);
|
|
66
|
+
const profileId = asString(displayBuilderSettings.profile);
|
|
67
|
+
if (!profileId) {
|
|
68
|
+
return { enabled: false };
|
|
69
|
+
}
|
|
70
|
+
const profile = await fetchJsonApiConfigResource(ctx, "display_builder_profile", profileId);
|
|
71
|
+
if (!profile) {
|
|
72
|
+
return { enabled: false };
|
|
73
|
+
}
|
|
74
|
+
const overrideProfileId = asString(displayBuilderSettings.override_profile, profileId);
|
|
75
|
+
const overrideProfile = overrideProfileId && overrideProfileId !== profileId
|
|
76
|
+
? await fetchJsonApiConfigResource(ctx, "display_builder_profile", overrideProfileId)
|
|
77
|
+
: profile;
|
|
78
|
+
const componentLibrary = asObject(asObject(profile.islands).component_library);
|
|
79
|
+
return {
|
|
80
|
+
enabled: true,
|
|
81
|
+
entityType: asString(display.targetEntityType, entityType),
|
|
82
|
+
bundle: asString(display.bundle, bundle),
|
|
83
|
+
viewMode: asString(display.mode, viewMode),
|
|
84
|
+
profile: profileSummary(profileId, profile),
|
|
85
|
+
profileConfig: profile,
|
|
86
|
+
overrideField: asString(displayBuilderSettings.override_field),
|
|
87
|
+
...(overrideProfileId
|
|
88
|
+
? {
|
|
89
|
+
overrideProfile: profileSummary(overrideProfileId, overrideProfile),
|
|
90
|
+
overrideProfileConfig: overrideProfile,
|
|
91
|
+
}
|
|
92
|
+
: {}),
|
|
93
|
+
...(displayBuilderSettings.sources !== undefined
|
|
94
|
+
? { sourceTree: displayBuilderSettings.sources }
|
|
95
|
+
: {}),
|
|
96
|
+
componentLibrary,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function cloneValue(value) {
|
|
2
|
+
if (value === undefined) {
|
|
3
|
+
return value;
|
|
4
|
+
}
|
|
5
|
+
return JSON.parse(JSON.stringify(value));
|
|
6
|
+
}
|
|
7
|
+
function cloneSchema(schema) {
|
|
8
|
+
return cloneValue(schema);
|
|
9
|
+
}
|
|
10
|
+
function ensureObjectProperty(parent, key) {
|
|
11
|
+
parent.properties ??= {};
|
|
12
|
+
const properties = parent.properties;
|
|
13
|
+
properties[key] ??= { type: "object" };
|
|
14
|
+
const child = properties[key];
|
|
15
|
+
child.type ??= "object";
|
|
16
|
+
child.properties ??= {};
|
|
17
|
+
return child;
|
|
18
|
+
}
|
|
19
|
+
function idValue(value) {
|
|
20
|
+
if (typeof value === "string") {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
24
|
+
const id = value.id;
|
|
25
|
+
return typeof id === "string" ? id : undefined;
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
function sourceTreeSchema() {
|
|
30
|
+
return {
|
|
31
|
+
type: "array",
|
|
32
|
+
description: "Display Builder source tree for the configured entity-view override field.",
|
|
33
|
+
items: {
|
|
34
|
+
type: "object",
|
|
35
|
+
additionalProperties: true,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function componentMetadata(component) {
|
|
40
|
+
return {
|
|
41
|
+
id: component.id,
|
|
42
|
+
name: component.name,
|
|
43
|
+
description: component.description,
|
|
44
|
+
provider: component.provider,
|
|
45
|
+
status: component.status,
|
|
46
|
+
props: cloneValue(component.props),
|
|
47
|
+
slots: cloneValue(component.slots),
|
|
48
|
+
variants: cloneValue(component.variants),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function extendDisplayBuilderSchema(baseSchema, metadata, components) {
|
|
52
|
+
if (!metadata.enabled || !metadata.overrideField) {
|
|
53
|
+
return baseSchema;
|
|
54
|
+
}
|
|
55
|
+
const schema = cloneSchema(baseSchema);
|
|
56
|
+
const data = ensureObjectProperty(schema, "data");
|
|
57
|
+
const attributes = ensureObjectProperty(data, "attributes");
|
|
58
|
+
const activeMetadata = metadata;
|
|
59
|
+
const attributeProperties = attributes.properties;
|
|
60
|
+
const sortedComponents = [...components].sort((a, b) => a.id.localeCompare(b.id));
|
|
61
|
+
attributeProperties[activeMetadata.overrideField] = sourceTreeSchema();
|
|
62
|
+
schema["x-dropsh-builder"] = "display-builder";
|
|
63
|
+
schema["x-dropsh-display-builder"] = {
|
|
64
|
+
entity_type: activeMetadata.entityType,
|
|
65
|
+
bundle: activeMetadata.bundle,
|
|
66
|
+
view_mode: activeMetadata.viewMode,
|
|
67
|
+
profile: idValue(activeMetadata.profile),
|
|
68
|
+
profile_config: cloneValue(activeMetadata.profileConfig),
|
|
69
|
+
override_field: activeMetadata.overrideField,
|
|
70
|
+
override_profile: idValue(activeMetadata.overrideProfile),
|
|
71
|
+
override_profile_config: cloneValue(activeMetadata.overrideProfileConfig),
|
|
72
|
+
source_tree: cloneValue(activeMetadata.sourceTree),
|
|
73
|
+
component_library: cloneValue(activeMetadata.componentLibrary),
|
|
74
|
+
};
|
|
75
|
+
schema["x-dropsh-components"] = sortedComponents.map(componentMetadata);
|
|
76
|
+
return schema;
|
|
77
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dropsh/plugin-display-builder",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/plugins/display-builder/src/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/plugins/display-builder/src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/plugins/display-builder/src",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"dropsh": "*"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@dropsh/sdc-client": "0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.6.0",
|
|
24
|
+
"vitest": "^4.1.8",
|
|
25
|
+
"dropsh": "0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc",
|
|
29
|
+
"test": "vitest run --passWithNoTests",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
31
|
+
}
|
|
32
|
+
}
|