Hi everyone,
I'm working on extending a content type in Plone using custom behaviors. Currently, there's a behavior that allows setting the title and URL ID (final URL segment) based on a "filename" field.
I'm aiming to create a new behavior that accomplishes the following:
- Separate Title and URL Name: I want to introduce a new field called "short_name" that will be used to dynamically generate the URL ID (last part of the URL). The existing title field should remain independent and be used for display purposes.
In essence, I want to achieve a setup where:
- Title: Used for display (can be edited independently)
- URL Name: Derived from the "short_name" field (automatically generated)
# INameFromFileName
from plone.app.content.interfaces import INameFromTitle
from plone.base.utils import safe_hasattr
from plone.rfc822.interfaces import IPrimaryFieldInfo
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface
class INameFromFileName(Interface):
"""Marker interface to enable name from filename behavior"""
@implementer(INameFromTitle)
@adapter(INameFromFileName)
class NameFromFileName:
def __new__(cls, context):
info = IPrimaryFieldInfo(context, None)
if info is None:
return None
filename = getattr(info.value, "filename", None)
if not isinstance(filename, str) or not filename:
return None
instance = super().__new__(cls)
instance.title = filename
if safe_hasattr(context, "title") and not context.title:
context.title = filename
return instance
def __init__(self, context):
pass