|
| 1 | +.. _inline_editing_custom_components: |
| 2 | + |
| 3 | +################################################ |
| 4 | +How to Use Inline Editing with Custom Components |
| 5 | +################################################ |
| 6 | + |
| 7 | +.. versionadded:: 2.0 |
| 8 | + |
| 9 | +``djangocms-frontend`` introduces simplified support for **inline editing** in custom |
| 10 | +components, making it easier to edit content directly on the page without opening the |
| 11 | +plugin'S change form. This guide will walk you through using the ``inline_field`` |
| 12 | +template tag to enable inline editing for your custom frontend components. |
| 13 | + |
| 14 | +.. note:: |
| 15 | + |
| 16 | + Inline editing requires djangocms-text to be installed and configured in your project. |
| 17 | + |
| 18 | + |
| 19 | +Understanding the ``inline_field`` Template Tag |
| 20 | +============================================= |
| 21 | + |
| 22 | +The ``inline_field`` template tag allows you to make specific fields editable directly from |
| 23 | +the page. It works by wrapping a field in an inline editing context, which is recognized by |
| 24 | +django CMS's edit mode. |
| 25 | + |
| 26 | +Syntax: |
| 27 | + |
| 28 | +.. code-block:: django |
| 29 | +
|
| 30 | + {% load frontend_tags %} |
| 31 | + {% inline_field instance "field_name" %} |
| 32 | +
|
| 33 | +- ``instance`` - The plugin or model instance. |
| 34 | +- ``"field_name"`` - The name of the field you want to make editable inline. |
| 35 | + |
| 36 | +If not in edit mode the template tag will render the field as plain text. |
| 37 | + |
| 38 | +.. note:: |
| 39 | + |
| 40 | + The ``inline_field`` tag is only available for fields that are explicitly listed in |
| 41 | + the ``frontend_editable_fields`` property of the plugin. The tag itself is a shortcut |
| 42 | + for django CMS's ``render_model`` tag. Since django CMS 5, this tags works for all |
| 43 | + CMS plugins (and not only for third-party models). For earlier versions of django CMS |
| 44 | + djangocms-frontend includes a custom extension for frontend plugins to support inline editing. |
| 45 | + |
| 46 | + |
| 47 | +Step-by-Step: Adding Inline Editing to a Custom Component |
| 48 | +========================================================= |
| 49 | + |
| 50 | +1. **Define Your Custom Component Plugin** |
| 51 | + |
| 52 | + First, create a djangoCMS plugin for your custom component. Example: |
| 53 | + |
| 54 | + .. code-block:: python |
| 55 | +
|
| 56 | + from cms.plugin_base import CMSPluginBase |
| 57 | + from cms.plugin_pool import plugin_pool |
| 58 | + from django.utils.translation import gettext_lazy as _ |
| 59 | + from djangocms_frontend.models import FrontendUIItem |
| 60 | +
|
| 61 | + class CustomComponentPlugin(CMSPluginBase): |
| 62 | + model = FrontendUIItem # Base model |
| 63 | + name = _("Custom Component") |
| 64 | + module = _("Frontend") |
| 65 | + render_template = "frontend/custom_component.html" |
| 66 | +
|
| 67 | + plugin_pool.register_plugin(CustomComponentPlugin) |
| 68 | +
|
| 69 | +2. **Modify the Component Template to Support Inline Editing** |
| 70 | + |
| 71 | + Open the template file (``frontend/custom_component.html``) and update it using the |
| 72 | + ``inline_field`` tag: |
| 73 | + |
| 74 | + .. code-block:: django |
| 75 | +
|
| 76 | + {% load frontend_tags %} |
| 77 | +
|
| 78 | + <div class="custom-component"> |
| 79 | + <h2>{% inline_field instance "title" %}</h2> |
| 80 | + <p>{% inline_field instance "description" %}</p> |
| 81 | + </div> |
| 82 | +
|
| 83 | +3. **Explicitly allow the field for inline editing** |
| 84 | + |
| 85 | + In the plugin definition, add the field to the ``frontend_editable_fields`` list: |
| 86 | + |
| 87 | + .. code-block:: python |
| 88 | +
|
| 89 | + class CustomComponentPlugin(CMSPluginBase): |
| 90 | + model = FrontendUIItem # Base model |
| 91 | + name = _("Custom Component") |
| 92 | + module = _("Frontend") |
| 93 | + render_template = "frontend/custom_component.html" |
| 94 | + frontend_editable_fields = ["title", "description"] |
| 95 | +
|
| 96 | +4. **Test Inline Editing** |
| 97 | + |
| 98 | + - Run the Django server: |
| 99 | + |
| 100 | + .. code-block:: bash |
| 101 | +
|
| 102 | + python manage.py runserver |
| 103 | +
|
| 104 | + - Log in as an admin user and enter **Edit Mode**. |
| 105 | + - Add your custom component to a page. |
| 106 | + - Click on the text fields to edit them inline. |
| 107 | + - Leave the field, and changes will be stored automatically in the database. |
| 108 | + |
| 109 | + |
| 110 | +Additional Considerations |
| 111 | +========================= |
| 112 | + |
| 113 | +- **Rich Text Editing:** If the field is a ``HTMLField``, django CMS text will automatically use |
| 114 | + a rich text editor for inline editing. |
| 115 | +- **CSS & JavaScript Adjustments:** In rare cases custom component's styles can interfere |
| 116 | + with django CMS text's inline editing interface. More specific rules typically solve the issue. |
0 commit comments