Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override only the Description field of the Plone standard content type

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/>
like image 369
Vito Avatar asked Oct 22 '11 09:10

Vito


3 Answers

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>
like image 65
SteveM Avatar answered Oct 11 '22 17:10

SteveM


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.

like image 31
Matt Hamilton Avatar answered Oct 11 '22 15:10

Matt Hamilton


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:

  • http://pypi.python.org/pypi/archetypes.schemaextender
  • http://weblion.psu.edu/services/documentation/developing-for-plone/fsd-extender/hiding-fields
  • https://svn.plone.org/svn/archetypes/MoreFieldsAndWidgets/Products.MasterSelectWidget (a custom widget example)
  • http://docs.plone.org/old-reference-manuals/plone_3_theming/buildingblocks/skin/templates/customizing-at-templates/customizing-widgets.html (customize just the template of a widget)
like image 4
Giacomo Spettoli Avatar answered Oct 11 '22 15:10

Giacomo Spettoli