druxt-entity 0.19.0 → 0.21.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,72 @@
1
1
  # druxt-entity
2
2
 
3
+ ## 0.21.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [9161b38]
8
+ - druxt-router@0.24.0
9
+
10
+ ## 0.21.1
11
+
12
+ ### Patch Changes
13
+
14
+ - 6ade3a2: Fixed Vuex mutation error
15
+
16
+ ## 0.21.0
17
+
18
+ ### Minor Changes
19
+
20
+ - 025315a: Added **include** option and the ability to filter related resources.
21
+
22
+ ```js
23
+ export default {
24
+ druxt: {
25
+ query: {
26
+ include: ["field_media_image", "field_media_image.field_media_image"],
27
+ fields: [
28
+ ["file--file", ["uri"]],
29
+ ["media--image", []]
30
+ ]
31
+ }
32
+ }
33
+ };
34
+ ```
35
+
36
+ - 025315a: Added **settings** property to the DruxtEntity and DruxtEntityForm components.
37
+
38
+ ```vue
39
+ <template>
40
+ <DruxtEntity
41
+ :settings="{ query: { include: ['uid'] } }"
42
+ type="node--page"
43
+ :uuid="uuid"
44
+ />
45
+ </template>
46
+ ```
47
+
48
+ - e7b1533: Refactored DruxtModule fetch hooks
49
+
50
+ ### Patch Changes
51
+
52
+ - Updated dependencies [77ab204]
53
+ - druxt-schema@0.9.0
54
+ - druxt@0.13.0
55
+ - druxt-router@0.23.0
56
+
57
+ ## 0.20.0
58
+
59
+ ### Minor Changes
60
+
61
+ - 897dcbc: Updated DruxtEntityForm to use new DruxtClient methods
62
+
63
+ ### Patch Changes
64
+
65
+ - Updated dependencies [4504a2f]
66
+ - druxt@0.12.0
67
+ - druxt-router@0.22.0
68
+ - druxt-schema@0.8.1
69
+
3
70
  ## 0.19.0
4
71
 
5
72
  ### Minor Changes
@@ -40,6 +40,27 @@ export default {
40
40
  default: 'default'
41
41
  },
42
42
 
43
+ /**
44
+ * Module settings object.
45
+ *
46
+ * @type {ModuleSettings}
47
+ * @default {}
48
+ */
49
+ settings: {
50
+ type: Object,
51
+ default: () => ({}),
52
+ },
53
+
54
+ /**
55
+ * Drupal display schema type, 'view' or 'form'.
56
+ *
57
+ * @type {('view'|'form')}
58
+ */
59
+ schemaType: {
60
+ type: String,
61
+ default: undefined,
62
+ },
63
+
43
64
  /**
44
65
  * JSON:API resource type.
45
66
  *
@@ -59,16 +80,6 @@ export default {
59
80
  type: [Boolean, String],
60
81
  default: false
61
82
  },
62
-
63
- /**
64
- * Drupal display schema type, 'view' or 'form'.
65
- *
66
- * @type {('view'|'form')}
67
- */
68
- schemaType: {
69
- type: String,
70
- default: undefined,
71
- },
72
83
  },
73
84
 
74
85
  /**
@@ -79,55 +90,13 @@ export default {
79
90
  model: {
80
91
  attributes: {},
81
92
  relationships: {},
93
+ included: undefined,
82
94
  type,
83
95
  ...value,
84
96
  },
85
97
  schema: null,
86
98
  }),
87
99
 
88
- /**
89
- * Nuxt.js fetch method.
90
- */
91
- async fetch() {
92
- // Fetch Schema.
93
- try {
94
- this.schema = await this.getSchema({
95
- resourceType: this.type,
96
- mode: this.mode,
97
- schemaType: this.schemaType || 'view',
98
- })
99
- } catch(e) {
100
- // TODO: Handle error
101
- }
102
-
103
- // Build wrapper component object.
104
- const options = this.getModuleComponents()
105
- let component = {
106
- is: (((options.filter(o => o.global) || [])[0] || {}).name || 'DruxtWrapper'),
107
- options: options.map(o => o.name) || [],
108
- }
109
-
110
- // Get wrapper component data to merge with module settings.
111
- const wrapperData = await this.getWrapperData(component.is)
112
- component.settings = merge(this.$druxt.settings.entity || {}, wrapperData.druxt || {}, { arrayMerge: (dest, src) => src })
113
-
114
- // Fetch Entity resource.
115
- if (this.uuid && !this.value) {
116
- const query = this.getQuery(component.settings)
117
- const entity = (await this.getResource({ type: this.type, id: this.uuid, query })).data
118
- this.model = JSON.parse(JSON.stringify(entity || {}))
119
- }
120
-
121
- // Get scoped slots.
122
- component.slots = Object.keys(this.getScopedSlots())
123
-
124
- // Build wrapper component propsData.
125
- component = { ...component, ...this.getModulePropsData(wrapperData.props) }
126
-
127
- // Set component data.
128
- this.component = component
129
- },
130
-
131
100
  fetchKey(getCounter) {
132
101
  const parts = ['DruxtEntity', this.type, this.uuid, this.mode, this.schemaType].filter((o) => o)
133
102
  return [...parts, getCounter(parts.join(':'))].join(':')
@@ -206,15 +175,42 @@ export default {
206
175
  getQuery(settings) {
207
176
  const query = new DrupalJsonApiParams()
208
177
 
209
- // Build fields list.
210
- const fields = (settings.query || {}).schema
211
- ? [...((this.schema || {}).fields || []).map((o) => o.id), ...((settings.query || {}).fields || [])]
212
- : ((settings.query || {}).fields || [])
213
- if (fields.length) {
214
- query.addFields(this.type, fields)
215
- return query
178
+ // Add includes.
179
+ if (settings.query.include && Array.isArray(settings.query.include)) {
180
+ query.addInclude(settings.query.include)
216
181
  }
217
- return false
182
+
183
+ let rootFields = []
184
+ if (settings.query.fields && Array.isArray(settings.query.fields)) {
185
+ // If the first item is a string, this is a root level filter array.
186
+ if (settings.query.fields.length === 0 || typeof settings.query.fields[0] === 'string' || typeof settings.query.fields[0] === 'undefined') {
187
+ rootFields = settings.query.fields
188
+ // Otherwise this is an array structure field map.
189
+ } else if (Array.isArray(settings.query.fields[0])) {
190
+ rootFields = settings.query.fields.find((a) => a.length === 0 || typeof [...a].pop() === 'string' || typeof [...a].pop() === 'undefined') || []
191
+
192
+ // Apply included field mapping.
193
+ const fieldMaps = settings.query.fields.filter(
194
+ (a) => a.length == 2 && typeof a[0] === 'string' && Array.isArray(a[1])
195
+ )
196
+ for (const fieldMap of fieldMaps) {
197
+ query.addFields(fieldMap[0], fieldMap[1])
198
+ }
199
+ }
200
+ }
201
+
202
+ // If Schema mode, generate list including schema fields and explicitly
203
+ // defined fields.
204
+ if (settings.query.schema) {
205
+ rootFields = [...((this.schema || {}).fields || []).map((o) => o.id), ...rootFields]
206
+ }
207
+
208
+ // Apply root fields filter.
209
+ if (rootFields.length) {
210
+ query.addFields(this.type, rootFields)
211
+ }
212
+
213
+ return query
218
214
  },
219
215
 
220
216
  /**
@@ -257,6 +253,34 @@ export default {
257
253
  [((schema || {}).config || {}).mode || mode],
258
254
  ]),
259
255
 
256
+ /**
257
+ * Fetch Schema.
258
+ */
259
+ async fetchConfig() {
260
+ try {
261
+ this.schema = await this.getSchema({
262
+ resourceType: this.type,
263
+ mode: this.mode,
264
+ schemaType: this.schemaType || 'view',
265
+ })
266
+ } catch(e) {
267
+ // TODO: Handle error
268
+ }
269
+ },
270
+
271
+ /**
272
+ * Fetch Entity resource.
273
+ */
274
+ async fetchData(settings) {
275
+ if (this.uuid && !this.value) {
276
+ const query = this.getQuery(settings)
277
+ const resource = await this.getResource({ type: this.type, id: this.uuid, query })
278
+ const entity = { ...(resource.data || {}) }
279
+ entity.included = resource.included
280
+ this.model = JSON.parse(JSON.stringify(entity || {}))
281
+ }
282
+ },
283
+
260
284
  /**
261
285
  * Provides propsData for the DruxtWrapper.
262
286
  *
@@ -265,6 +289,21 @@ export default {
265
289
  */
266
290
  propsData: ({ fields, model, schema }) => ({ entity: model, fields, schema, value: model }),
267
291
 
292
+ /**
293
+ * Component settings.
294
+ */
295
+ settings: ({ $druxt, settings }, wrapperSettings) => {
296
+ // Start with the `nuxt.config.js` `druxt.settings.entity` settings and
297
+ // merge the Wrapper component settings on top.
298
+ let mergedSettings = merge($druxt.settings.entity || {}, wrapperSettings, { arrayMerge: (dest, src) => src })
299
+ // Merge the DruxtEntity component `settings` property on top.
300
+ mergedSettings = merge(mergedSettings || {}, settings, { arrayMerge: (dest, src) => src })
301
+ // Currently only returning the query settings.
302
+ return {
303
+ query: mergedSettings.query || {},
304
+ }
305
+ },
306
+
268
307
  /**
269
308
  * Provides the scoped slots object for the Module render function.
270
309
  *
@@ -345,33 +384,46 @@ export default {
345
384
  */
346
385
 
347
386
  /**
348
- * Provides settings for the Entity module, via the `nuxt.config.js` `druxt.entity`
349
- * or the Wrapper component `druxt` object.
387
+ * Provides settings for the Entity module, via the `nuxt.config.js` `druxt.entity`,
388
+ * the Wrapper component `druxt` object or the DruxtEntity component `settings`
389
+ * property.
350
390
  *
351
391
  * @typedef {object} ModuleSettings
352
- * @param {string[]} fields - An array of fields to filter from the JSON:API Resource.
353
- * @param {boolean} schema - Whether to automatically detect fields to filter, per the Display mode.
392
+ * @param {object} query - Entity Query settings:
393
+ * @param {(string[]|array[])} query.fields - An array or arrays of fields to filter from the JSON:API Resources.
394
+ * @param {string[]} query.include - An array of relationships to include alongside the JSON:API Resource.
395
+ * @param {boolean} query.schema - Whether to automatically detect fields to filter, per the Display mode.
354
396
  *
355
- * @example @lang js
356
- * {
357
- * fields: [],
358
- * schema: false,
359
- * }
360
- *
361
- * @example @lang vue
397
+ * @example <caption>DruxtEntity Wrapper component</caption> @lang vue
362
398
  * <script>
363
399
  * export default {
364
400
  * druxt: {
365
401
  * query: {
366
- * fields: ['title']
402
+ * fields: [['title'], ['user--user', ['display_name']]],
403
+ * include: ['uid']
367
404
  * schema: true,
368
405
  * },
369
406
  * }
370
407
  * }
408
+ *
409
+ * @example <caption>DruxtEntity component with settings</caption> @lang vue
410
+ * <template>
411
+ * <DruxtEntity
412
+ * type="node--article"
413
+ * :uuid="uuid"
414
+ * :settings="{
415
+ * query: {
416
+ * fields: [['title'], ['user--user', ['display_name']]],
417
+ * include: ['uid']
418
+ * schema: true,
419
+ * }
420
+ * }"
421
+ * />
422
+ * </template>
371
423
  */
372
424
 
373
425
  /**
374
- * Provides propsData for use in the Wrapper component.
426
+ * Provides property data for use in the Wrapper component.
375
427
  *
376
428
  * @typedef {object} PropsData
377
429
  * @param {object} entity - The Drupal Entity JSON:API resource data.
@@ -88,34 +88,23 @@ export default {
88
88
  if (this.submitting) return false
89
89
  this.submitting = true
90
90
 
91
- let url = this.schema.config.href
92
- let method = 'post'
93
- if (this.entity.id) {
94
- // @todo - Reduce size of payload by only sending changed data.
95
- url = [url, this.entity.id].join('/')
96
- method = 'patch'
91
+ let method = 'createResource'
92
+ if (this.model.id) {
93
+ method = 'updateResource'
97
94
  }
95
+ this.response = await this.$druxt[method](this.model)
98
96
 
99
- // Try to send data to backend, and catch any resulting errors.
100
- try {
101
- this.response = await this.$druxt.axios[method](
102
- url,
103
- { data: this.model },
104
- {
105
- headers: {
106
- 'Content-Type': 'application/vnd.api+json',
107
- },
108
- }
109
- )
110
-
111
- // Update the Vuex store.
97
+ // Handle the response
98
+ if (!this.response.errors) {
112
99
  const resource = this.response.data.data
100
+ // Update the Vuex store.
113
101
  this.$store.commit('druxt/addResource', { resource })
114
102
  this.$emit('submit', resource)
115
- } catch (e) {
116
- this.response = (e.response || {}).data || e.message
103
+ }
104
+ else {
117
105
  this.$emit('error', this.response)
118
106
  }
107
+
119
108
  this.submitting = false
120
109
  },
121
110
  },
@@ -5,7 +5,7 @@ import { DruxtClient } from 'druxt';
5
5
  const titleFn = (parts) => parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1).replace(/_/g, " ")).join("/");
6
6
  async function DruxtEntityStorybook({ stories }) {
7
7
  const { addTemplate, options } = this;
8
- const druxt = new DruxtClient(options.druxt.baseUrl, options.druxt);
8
+ const druxt = new DruxtClient(options.druxt.baseUrl, { ...options.druxt, proxy: { api: false } });
9
9
  const [view, form] = (await Promise.all(["view", "form"].map(async (type) => await druxt.getCollectionAll(`entity_${type}_display--entity_${type}_display`, new DrupalJsonApiParams().addFilter("status", 1).addFields(`entity_${type}_display--entity_${type}_display`, [
10
10
  "bundle",
11
11
  "mode",
@@ -9,7 +9,7 @@ const druxt = require('druxt');
9
9
  const titleFn = (parts) => parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1).replace(/_/g, " ")).join("/");
10
10
  async function DruxtEntityStorybook({ stories }) {
11
11
  const { addTemplate, options } = this;
12
- const druxt$1 = new druxt.DruxtClient(options.druxt.baseUrl, options.druxt);
12
+ const druxt$1 = new druxt.DruxtClient(options.druxt.baseUrl, { ...options.druxt, proxy: { api: false } });
13
13
  const [view, form] = (await Promise.all(["view", "form"].map(async (type) => await druxt$1.getCollectionAll(`entity_${type}_display--entity_${type}_display`, new drupalJsonapiParams.DrupalJsonApiParams().addFilter("status", 1).addFields(`entity_${type}_display--entity_${type}_display`, [
14
14
  "bundle",
15
15
  "mode",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "druxt-entity",
3
- "version": "0.19.0",
3
+ "version": "0.21.2",
4
4
  "description": "Drupal Display Mode powered Entity, Form and Field Druxt components.",
5
5
  "keywords": [
6
6
  "cms",
@@ -46,9 +46,9 @@
46
46
  "dependencies": {
47
47
  "deepmerge": "^4.2.2",
48
48
  "drupal-jsonapi-params": "^1.2.2",
49
- "druxt": "^0.11.0",
50
- "druxt-router": "^0.21.0",
51
- "druxt-schema": "^0.8.0"
49
+ "druxt": "^0.13.0",
50
+ "druxt-router": "^0.24.0",
51
+ "druxt-schema": "^0.9.0"
52
52
  },
53
53
  "publishConfig": {
54
54
  "access": "public"