Hello, we have created a Nested XBlock and configured some of the available child XBlock types with single_instance=True
as shown below.
class CaseReportXBlock(StudioContainerWithNestedXBlocksMixin, XBlock):
display_name = String(
display_name="Display Name",
help="The display name for this component.",
scope=Scope.content,
default="Case Report",
)
completion_mode = XBlockCompletionMode.AGGREGATOR
show_in_read_only_mode = True
icon_class = "other"
@property
def allowed_nested_blocks(self):
from ..case_file import AmbraCaseFileXBlock, CaseFileXBlock, CaseHistoryXBlock
from ..case_report import CaseReportSubmissionXBlock
return [
NestedXBlockSpec(
CaseHistoryXBlock,
label="Case History",
category="case_history",
single_instance=True,
),
NestedXBlockSpec(
CaseFileXBlock,
label="Case File (URL)",
category="case_file",
),
NestedXBlockSpec(
AmbraCaseFileXBlock,
label="Case File (Ambra)",
category="ambra",
),
NestedXBlockSpec(
CaseReportSubmissionXBlock,
label="Case Report Submission",
category="case_report_submission",
single_instance=True,
),
]
Using single_instance=True
, we are able to successfully disable the buttons for the XBlock types in the “Add New Component” section, however this does not disable the ability to duplicate already existing XBlocks providing a way around the “single instance” configuration as shown in the screenshot below.
Based on my initial investigation, whether or not an XBlock can be duplicated is controlled by the can_add
context in this template edx-platform/cms/templates/studio_xblock_wrapper.html at 5071f28e20055f7039326cd0d2e2f8ca933217ed · openedx/edx-platform · GitHub, which is appears to be added to the context here edx-platform/cms/djangoapps/contentstore/views/preview.py at 5071f28e20055f7039326cd0d2e2f8ca933217ed · openedx/edx-platform · GitHub.
Setting can_add
to false does successfully remove the duplicate option, but it also removes the delete option from the menu (this also comes from studio_xblock_wrapper.html
). While not ideal, this is something we could likely find a work around for. However, it is not clear how to set this can_add
context for a given XBlock within the nested spec since depending on the XBlock type we may want to allow for multiple/duplication if it’s a direct child of a Vertical XBlock for instance.
I had some luck overriding the render
method of a child XBlock to update the context, however that unexpectedly prevented duplication for all children within the nested XBlock and the parent nested XBlock as well instead of just the XBlock type I modified.
It’s worth noting that we are on the Redwood 3 release and are still using old Studio, we have not made the move to the Content Authoring MFE.
If anyone has experience with nested XBlocks and has suggestions, they would be greatly appreciated.