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

Add ability to filter the attributes that are saved in the ObjectBrow… #2053

Merged
merged 1 commit into from
Dec 11, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Feature

- Add ability to filter the attributes that are saved in the ObjectBrowserWidget @sneridagh
- Add `object_browser` as widget @sneridagh

### Bugfix

### Internal
Expand Down
30 changes: 30 additions & 0 deletions docs/source/blocks/editcomponent.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ if we use object browser widget for fields:
- **image**: dataName is _url_ and propDataName is null
- **link**: dataName is _href_ and propDataName is null

#### Usage in blocks schema

Used in along with `InlineForm`, one can instantiate and configure it using the widget props like this:

```js
{
title: 'Item',
fieldsets: [
{
id: 'default',
title: 'Default',
fields: ['href'],
},
],
properties: {
href: {
title: 'title',
widget: 'object_browser',
mode: 'link',
selectedItemAttrs: ['Title', 'Description'],
},
}
```
#### selectedItemAttrs
You can select the attributes from the object (coming from the metadata brain from
@search endpoint used in the browser) using the `selectedItemAttrs` prop as shown in the
last example.
#### ObjectBrowserWidgetMode()
Returns the component widget with _mode_ passed as argument.
Expand Down
23 changes: 20 additions & 3 deletions src/components/manage/Widgets/ObjectBrowserWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class ObjectBrowserWidget extends Component {
if (maxSize === 1 && value.length === 1) {
value = []; //enable replace of selected item with another value, if maxsize is 1
}

let exists = false;
let index = -1;
value.forEach((_item, _index) => {
Expand All @@ -150,8 +149,26 @@ class ObjectBrowserWidget extends Component {
// '@id': flattenToAppURL(item['@id']),
// });
if (!exists) {
//add item
value.push(item);
// add item
// Check if we want to filter the attributes of the selected item
let resultantItem = item;
if (this.props.selectedItemAttrs) {
const allowedItemKeys = [
...this.props.selectedItemAttrs,
// Add the required attributes for the widget to work
'@id',
'title',
];
resultantItem = Object.keys(item)
.filter((key) => allowedItemKeys.includes(key))
.reduce((obj, key) => {
obj[key] = item[key];
return obj;
}, {});
}
// Add required @id field, just in case
resultantItem = { ...resultantItem, '@id': item['@id'] };
value.push(resultantItem);
this.props.onChange(this.props.id, value);
} else {
//remove item
Expand Down
1 change: 1 addition & 0 deletions src/config/Widgets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const widgetMapping = {
align: AlignWidget,
url: UrlWidget,
email: EmailWidget,
object_browser: ObjectBrowserWidget,
},
vocabulary: {
'plone.app.vocabularies.Catalog': ObjectBrowserWidget,
Expand Down