Possible to remove older items from nav

While I know there is a check-box on a lot of content types to disable it from appearing in any navigation, which I believe also includes the Navigation portlet. Is there a preferred method to say have it auto-remove items from the navigation if they are older than a specific date? I haven't fully explored all the control panel items, and do see a Content Rules, but I'm not sure if this would work given the possible triggers it has.

My other thought is to create a Python script, which can then just iterate through all the objects of a specific Dexterity Folder I want to perform this task on, and then just manually run this Python Script every so often to perform a clean-up of navigation links.

Any thoughts on this, or how do other people manage this aspect of their Plone site?

Either using a script setting excludeFromNav as need or perhaps set the expiration date on related content object in order to make them inaccessible?

The expiration date is an interesting option, but then it does make it completely inaccessible, which isn't what I want. I am looking for it to archive, so it can still be found on the overall site, is seen by collections, and can be found by the catalog.

Since I've been learning more about creating Plone add-ons, I might look into adding a new behavior. Reading about how they are created last night, I feel that this might be the way to go in terms of having to build my own solution.

Although I still haven't looked too deeply into the overall Plone API of these objects in Zope, so I might be-able to create a short Python (Script) which could iterate through all the items in a Dexterity Folder each month to change this property on each of the objects.

This is fairly trivial:

  • you write a a dedicated script and run it using bin/instance run your_script.py
  • the script would use the portal_catalog for search the content items as list of brains
  • for every brain, you get the object using getObject()
  • you manipulate the metadata as needed
  • you reindex the related object
  • at the end transaction.commit()
  • the complete manipulation must be done as a Manager. plone.api.env has a decorator for running code in the context of the Manager role.
  • run the script regularly through cron or systemd

Some AI slop:




"""Bulk-update metadata in a Plone site.

Run with:
    bin/instance run update_metadata.py
"""

import transaction

from plone import api
from zope.component.hooks import setSite


SITE_ID = "Plone"


@api.env.adopt_roles(["Manager"])
def update_metadata(portal):
    catalog = portal.portal_catalog

    # Search for content objects using the catalog.
    brains = catalog(portal_type="Document")

    print(f"Found {len(brains)} documents")

    updated = 0

    for brain in brains:
        obj = brain.getObject()

        if obj is None:
            continue

        # Example: update the description metadata.
        old_description = obj.description or ""
        new_description = old_description.replace(
            "Old text", "New text"
        )

        if new_description == old_description:
            continue

        obj.description = new_description

        # Reindex the modified content object.
        obj.reindexObject()

        updated += 1
        print(f"Updated: {brain.getPath()}")

    # Persist all changes.
    transaction.commit()

    print(f"Successfully updated {updated} documents")


def main(app):
    # Get Plone site from the Zope application root.
    portal = app[SITE_ID]

    # Establish the active Plone site context.
    setSite(portal)

    try:
        update_metadata(portal)
    except Exception:
        transaction.abort()
        raise


if "app" in globals():
    main(app)

```
2 Likes

I may be completely misunderstanding the request, but if the need is a list of items in a portlet, with older items falling away, why not make use of a collection portlet that uses a collection to do the heavy lifting around what it shows. This also doesn't change the state or the accessibility of the item.

That way you're working with built-in Plone functionalities and the things to things to customize already exist - the portlet, the collection's settings, etc.

While portlets are an interesting way of tackling this problem, and you do make a good point at they can be limited to only a specific amount of items. This might actually be the solution I'll go with.

At the moment, I resolved the problem with a cluttered navigation with other suggestions and my own solutions. In the actual collection or folder views, the items are technically batched, so a visitor does need to click onto later pages to see these older posts.

Perhaps I am turning a non-issue into an issue that I feel so compelled to solve, when there is actually nothing to solve here. As you mention, Plone does have some useful utilities built right in to handle these sort of scenarios.

Although, I would like to note one thing, I do appreciate the script provided by @zopyx as it helped me to learn a bit more on how I can script Zope and Plone from the command-line using scripts. The command I believe isn't bin/instance, but rather bin/zconsole, but regardless of that small error, this script is an incredible resource for getting started down the path of scripting Zope from external scripts like this.

I'll consider this post resolved, hopefully these replies help others who might just be starting out with using Plone, and might be confused about certain details as I am.

1 Like