Skip to content

Commit 764b8a8

Browse files
committed
autodetect inline fields
1 parent e87df95 commit 764b8a8

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

djangocms_frontend/templatetags/frontend.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@
2424
register = template.Library()
2525

2626

27+
def is_registering_component(context: template.Context) -> bool:
28+
return (
29+
"_cms_components" in context
30+
and "cms_component" in context["_cms_components"]
31+
and len(context["_cms_components"]["cms_component"]) == 1
32+
)
33+
34+
35+
def update_component_properties(context: template.Context, key: str, value: typing.Any, append: bool = False) -> None:
36+
""""Adds or appends the value to the property "key" of a component during delcaration"""
37+
args, kwargs = context["_cms_components"]["cms_component"][0]
38+
if append:
39+
# Populate slots with plugin_type and verbose_name
40+
if key in kwargs:
41+
kwargs[key].append(value)
42+
else:
43+
kwargs[key] = [value]
44+
else:
45+
kwargs[key] = value
46+
context["_cms_components"]["cms_component"][0] = (args, kwargs)
47+
48+
49+
2750
@register.simple_tag
2851
def get_attributes(attribute_field, *add_classes):
2952
"""Joins a list of classes with an attributes field and returns all html attributes"""
@@ -237,21 +260,13 @@ class RenderChildPluginsTag(Tag):
237260
)
238261

239262
def render_tag(self, context, instance, plugin_type, verbose_name, nodelist):
240-
if (
241-
"_cms_components" in context
242-
and "cms_component" in context["_cms_components"]
243-
and len(context["_cms_components"]["cms_component"]) == 1
244-
):
263+
if is_registering_component(context):
245264
args, kwargs = context["_cms_components"]["cms_component"][0]
246265
if plugin_type is None:
247266
# If tag is used, default to allow_children=True
248267
kwargs.setdefault("allow_children", True)
249268
if plugin_type and verbose_name:
250-
# Populate slots with plugin_type and verbose_name
251-
if "slots" in kwargs:
252-
kwargs["slots"].append((plugin_type, verbose_name))
253-
else:
254-
kwargs["slots"] = [(plugin_type, verbose_name)]
269+
update_component_properties(context, "slots", (plugin_type, verbose_name), append=True)
255270
context["_cms_components"]["cms_component"][0] = (args, kwargs)
256271

257272
if not instance:
@@ -290,16 +305,19 @@ class InlineField(CMSEditableObject):
290305
Argument("varname", required=False, resolve=False),
291306
)
292307

293-
def render_tag(self, context, **kwargs):
294-
if (
308+
def render_tag(self, context, instance, attribute, **kwargs):
309+
if is_registering_component(context) and attribute:
310+
update_component_properties(context, "frontend_editable_fields", attribute, append=True)
311+
312+
elif (
295313
context["request"].session.get("inline_editing", True)
296-
and isinstance(kwargs["instance"], CMSPlugin)
297-
and kwargs["instance"].pk
314+
and isinstance(instance, CMSPlugin)
315+
and instance.pk
298316
):
299317
# Only allow inline field to be rendered if inline editing is active and the instance is a CMSPlugin
300318
# DummyPlugins of the ``plugin`` tag are cannot be edited (they have no pk in their model class)
301-
kwargs["edit_fields"] = kwargs["attribute"]
302-
return super().render_tag(context, **kwargs)
319+
kwargs["edit_fields"] = attribute
320+
return super().render_tag(context, instance=instance, attribute=attribute, **kwargs)
303321
else:
304322
return getattr(kwargs["instance"], kwargs["attribute"], "")
305323

0 commit comments

Comments
 (0)