Jurnal

keep calm and conquer the world

Menu
  • Home
  • Plimbări
  • Echipamente
  • Recomand
  • Contact
Menu

Plone 6, Volto: find items containing a block

Posted on 22.01.202522.01.2025 by GhitaB

In my case I searched for items that contains any kind of “form” block:

Register a view (MyView):

configure.zcml

  <browser:page
      name="my-view"
      for="*"
      class=".admin.MyView"
      permission="cmf.ManagePortal"
      />

admin.py

from Products.Five.browser import BrowserView
from plone import api
from zope.component.hooks import getSite


def find_items_with_form_blocks():
    SEARCH_WORD = 'form'
    portal = getSite()
    catalog = api.portal.get_tool("portal_catalog")

    results = []

    def search_in_blocks(blocks):
        if not isinstance(blocks, dict):
            return False

        for block_id, block_data in blocks.items():
            if SEARCH_WORD in block_data.get("@type", ""):
                return True

            nested_blocks = block_data.get("blocks", {})
            if search_in_blocks(nested_blocks):
                return True

        return False

    for brain in catalog():
        obj = brain.getObject()
        if hasattr(obj, "blocks") and search_in_blocks(obj.blocks):
            results.append(
                {
                    "title": obj.Title(),
                    "url": obj.absolute_url(),
                }
            )

    return results


class MyView(BrowserView):
    def __call__(self):
        results = find_items_with_form_blocks()
        if results:
            for result in results:
                print(f"Title: {result['title']}")
                print(f"URL: {result['url']}")
                print("-" * 20)
        else:
            print("No results found.")

        import pdb

        pdb.set_trace()

        return results

Then go to localhost:8080/Plone/my-view

Title: Lorem Ipsum form
URL: http://localhost:8080/Plone/en/some/path
--------------------
Title: Lorem 2
URL: http://localhost:8080/Plone/en/another/path
--------------------
> /app/sources/my.addon/my/addon/browser/admin.py(54)__call__()
-> return results
(Pdb) results
[{'title': 'Lorem Ipsum form', 'url': 'http://localhost:8080/Plone/en/some/path'}, {'title': 'Lorem 2', 'url': 'http://localhost:8080/Plone/en/another/path'}]
(Pdb) q
Tags: docs, plone, solutions, Volto, web development
Category: Docs

Post navigation

← Omul desculț
Câinele i-a mâncat ochiul. →

Archives

  • May 2025 (4)
  • April 2025 (3)
  • March 2025 (19)
  • February 2025 (20)
  • January 2025 (33)
  • December 2024 (8)
  • November 2024 (4)
  • October 2024 (8)
  • September 2024 (4)
  • August 2024 (9)
  • July 2024 (21)
  • June 2024 (17)
  • May 2024 (13)
  • April 2024 (1)
  • March 2024 (7)
  • February 2024 (15)
  • January 2024 (24)
  • December 2023 (11)
  • November 2023 (53)
  • October 2023 (17)
  • September 2023 (20)
  • August 2023 (40)
  • July 2023 (39)
  • June 2023 (38)

Categories

  • 27 (4)
  • Acasă (64)
  • Amintiri (13)
  • Creștine (33)
  • Desen (8)
  • Design (1)
  • Docs (51)
  • Duldy & Țâcă (5)
  • Fotografie (42)
  • Gânduri (104)
  • Grădină (10)
  • Interesant (14)
  • Istorie (4)
  • Jurnal (163)
  • Limba română (8)
  • Mașină (2)
  • Muzică (46)
  • Personal (100)
  • Plimbări (24)
  • Programare (24)
  • Sport (12)
  • Știri (36)
  • Vegan (16)

Tags

2 mese pe zi (5) acasă (57) am auzit ceva nou (5) amintiri (8) am scris asta (7) biblice (11) Bihor (15) biserică (5) creștine (56) câine (12) câinele e prietenul omului (12) desen (8) docs (46) fotografie (45) funny (63) grădină (10) gânduri (122) interesant (8) istorie (7) jurnal (199) jurnal sportiv (11) Krita (5) limba română (10) momente (193) muzică (48) muzică preluată (28) natură (6) Padiș (11) personal (142) plimbare (22) plone (27) povești (6) programare (27) recomand (8) rețete vegane (10) România (29) Rusia (5) sketchbook (5) solutions (30) spam (5) sport (6) vegan (17) versuri (6) web development (15) știri (20)

RSS Feed Subscribe
© 2025 Jurnal | Powered by Minimalist Blog WordPress Theme