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
Category: Docs