Question

What could be the best way to achieve a functionality that

  • All items get new id based no title edits automatically, no need to rename items manually

  • This would use the id normalization + counter format used by Plone by default

  • Can be enabled globally for all Archetypes content types

Was it helpful?

Solution

Yuri's on the right track - for a quick and very dirty, I believe this will work:

context.setTitle('lorem ipsum')
context.unmarkCreationFlag()
context.processForm()

Strictly, that's not what the documentation says - as it shouldn't do a rename if it doesn't have the temporary ID generated in the portal factory, but I was getting renaming happening when I modified titles of objects that still had the CreationFlag marked

OTHER TIPS

Apart from the fact that this is a bad idea (all your URLs break every time you edit a title), I'd do this with a custom event. You'll have to copy some of the functionality built into Archetypes used when renaming objects on create, because you don't want to rename every time you edit:

from Products.Archetypes.interfaces import (
    IBaseObject,
    IObjectEditedEvent,
)
import re
from zope.component import adapter


endsWithNumber = re.compile('-\d+$')


@adapter(IBaseObject, IObjectEditedEvent)
def renameOnEdit(obj, event):
    old_id = obj.getId()
    without_number = endsWithNumber.sub('', old_id)

    # New id based on Title
    new_id = obj.generateNewId()
    if new_id == old_id or new_id == without_number:
        # No change
        return

    new_id = obj._findUniqueId(new_id)
    if new_id is None:
        # Couldn't find a new unique id (out of sequence numbers?)
        return

    obj.setId(new_id)

I think archetypes has this built in:

http://plone.org/documentation/kb/richdocument/controlling-creation

If you need more fine-grained control over how titles are generated, you can re-define the _renameAfterCreation() method from 'Archetypes/BaseObject.py'

So redefining it should be the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top