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

Support RelationList field with StaticCatalogVocabulary and SelectWidget. #4614

Merged
merged 4 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
92 changes: 90 additions & 2 deletions docs/source/recipes/widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,102 @@ const applyConfig = (config) => {
Based on this setup, Volto will render this field with the `TokenWidget`.


```{seealso}
See [storybook](https://6.dev-docs.plone.org/storybook) with available widgets.
## Relation fields
ksuess marked this conversation as resolved.
Show resolved Hide resolved

ksuess marked this conversation as resolved.
Show resolved Hide resolved
### Single relation field

Relation field with named `StaticCatalogVocabulary`and `Select` widget:
ksuess marked this conversation as resolved.
Show resolved Hide resolved

```python
relationchoice_field_named_staticcatalogvocabulary = RelationChoice(
title="RelationChoice – named StaticCatalogVocabulary – Select widget",
description="field/relation: relationchoice_field_named_staticcatalogvocabulary",
vocabulary="relationchoice_field_named_staticcatalogvocabulary",
required=False,
)
directives.widget(
"relationchoice_field_named_staticcatalogvocabulary",
frontendOptions={
"widget": "select",
},
)
```


ksuess marked this conversation as resolved.
Show resolved Hide resolved
It is recommended to define the vocabulary as a named `StaticCatalogVocabulary` with the field/relation name as its name.
This allowes the relations control panel to respect the defined restrictions to potential relation targets.
ksuess marked this conversation as resolved.
Show resolved Hide resolved

{file}`vocabularies.py`
```python
from plone.app.vocabularies.catalog import StaticCatalogVocabulary
from zope.interface import provider
from zope.schema.interfaces import IVocabularyFactory


@provider(IVocabularyFactory)
def ExamplesVocabularyFactory(context=None):
return StaticCatalogVocabulary(
{
"portal_type": ["example"],
"review_state": "published",
"sort_on": "sortable_title",
}
)
```

{file}`configure.zcml`
```xml
<utility
name="relationchoice_field_named_staticcatalogvocabulary"
component="example.contenttype.vocabularies.ExamplesVocabularyFactory"
/>
```


ksuess marked this conversation as resolved.
Show resolved Hide resolved
The directive is by now default and can be ommitted.
ksuess marked this conversation as resolved.
Show resolved Hide resolved

```python
relationchoice_field_named_staticcatalogvocabulary = RelationChoice(
title="RelationChoice – named StaticCatalogVocabulary – Select widget",
description="field/relation: relationchoice_field_named_staticcatalogvocabulary",
vocabulary="relationchoice_field_named_staticcatalogvocabulary",
required=False,
)
```


ksuess marked this conversation as resolved.
Show resolved Hide resolved
### Multi relation field

Multi relation field with named `StaticCatalogVocabulary`and `Select` widget:
ksuess marked this conversation as resolved.
Show resolved Hide resolved

```python
relationlist_field_named_staticcatalogvocabulary = RelationList(
title="RelationList – named StaticCatalogVocabulary – Select widget",
description="field/relation: relationlist_field_named_staticcatalogvocabulary",
value_type=RelationChoice(
vocabulary="relationlist_field_named_staticcatalogvocabulary",
),
required=False,
)
directives.widget(
"relationlist_field_named_staticcatalogvocabulary",
frontendOptions={
"widget": "select",
},
)
```


## Widget `isDisabled` Props

We can disable the input of a widget by passing props `isDisabled: true`.


## Available widgets

See [Storybook](https://6.docs.plone.org/storybook) with available widgets.


## Write a new widget

```{note}
Expand Down
1 change: 1 addition & 0 deletions news/4614.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support RelationList field with named StaticCatalogVocabulary and SelectWidget. @ksuess
2 changes: 1 addition & 1 deletion src/components/manage/Widgets/SelectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function normalizeSingleSelectOption(value, intl) {
throw new Error(`Unknown value type of select widget: ${value}`);
}

const token = value.token ?? value.value ?? 'no-value';
const token = value.token ?? value.value ?? value.UID ?? 'no-value';
const label =
(value.title && value.title !== 'None' ? value.title : undefined) ??
value.label ??
Expand Down
2 changes: 1 addition & 1 deletion src/components/manage/Widgets/SelectWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class SelectWidget extends Component {

const isMulti = this.props.isMulti
? this.props.isMulti
: id === 'roles' || id === 'groups';
: id === 'roles' || id === 'groups' || this.props.type === 'array';

return (
<FormFieldWrapper {...this.props}>
Expand Down
1 change: 1 addition & 0 deletions src/config/Widgets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const widgetMapping = {
select_querystring_field: SelectMetadataWidget,
autocomplete: SelectAutoComplete,
color_picker: ColorPickerWidget,
select: SelectWidget,
},
vocabulary: {
'plone.app.vocabularies.Catalog': ObjectBrowserWidget,
Expand Down