Override only the Description field of the Plone standard content type
Asked Answered
W

3

7

I would like to ovverride only the "view" of the classic "description field" of the Plone standard content type (Document, Folder, blabla), cause I need to "structure" the text of this field with structured text like:

This is my description<br/>
with many lines<br/>
bla bla<br/>
Wallas answered 22/10, 2011 at 9:31 Comment(0)
E
6

Changing the template that renders the standard description field to convert newlines to breaks isn't hard, but will require a little care to avoid creating a security hole.

Override the skin layer kss_generic_macros.pt template, either in a theming product or the custom folder.

Then, you may use Products.PythonScripts.standard.newline_to_br to convert newlines to breaks. You'll need to insert the converted text with "structure" to prevent escaping of the breaks.

Since you'll be using "structure," you absolutely must also manually html escape the description (use html_quote from standard) before applying newline_to_br, or you'll create a vector for a XSS attack.

The key section of the macro, when fixed, might read:

            <div metal:define-macro="description-field-view"
               id="parent-fieldname-description"
               tal:define="kss_class python:getKssClasses('description',
                           templateId='kss_generic_macros', macro='description-field-view');
                           pps modules/Products.PythonScripts.standard"
               tal:condition="context/Description"
               tal:attributes="class string:documentDescription$kss_class;">
               <span metal:define-slot="inside"
                     tal:replace="structure python:pps.newline_to_br(pps.html_quote(context.Description()))">Description</span>
            </div>
Engle answered 23/10, 2011 at 16:23 Comment(3)
This will change the view of the item itself, but not in folder listings.For this you also need to costumize the default view, e.g. for folder_summary_view it would look like: <p class="tileBody" tal:condition="item_description" tal:define="pps modules/Products.PythonScripts.standard"> <span class="description" tal:content="structure python:pps.newline_to_br(pps.html_quote(item_description))"> description </span> </p>Vermillion
If you also want to remove trailing newlines use rstrip in the method call above: context.Description().rstrip()Vermillion
For Dexterity one may use: <p tal:define="pps modules/Products.PythonScripts.standard; description python:pps.html_quote(context.description.strip()); description python:pps.newline_to_br(description)" tal:content="structure description" />Savarin
P
4

If you want to customize the description widget for all content types you could create an adapter using archetypes.schemaextender (notably the ISchemaModifier interface) like this:

from my.product.browser.interfaces import IMyProductLayer
from my.product.widgets import MyCustomWidget
from Products.ATContentTypes.interface.interfaces import IATContentType
from archetypes.schemaextender.interfaces import IBrowserLayerAwareExtender
from archetypes.schemaextender.interfaces import ISchemaModifier

class MyExtender(object):
    # you could choose a more specific interface for a more fine grained override
    adapts(IATContentType)
    implements(IBrowserLayerAwareExtender, ISchemaModifier)
    # this will limit out override to this browserlayer
    layer = IMyProductLayer

    def fiddle(self, schema):
        # if you want to customize just the template of the original widget
        # see links below
        schema['description'].widget=MyCustomWidget(
            label='...',
            ....
        )
        return schema

and then you can register it like this:

<adapter
    factory=".extender.MyExtender"
    provides="archetypes.schemaextender.interfaces.ISchemaModifier" />

Don't forget to register your browser layer IMyProductLayer or this adapter will never be used.

More info:

Partition answered 22/10, 2011 at 13:44 Comment(0)
D
4

You really don't want HTML in the description field. This field is used in a number of places and expects plain text.

You are best using the approach above to add an additional field with a different name.

Dyslalia answered 22/10, 2011 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.