以防万一您仍然对此感兴趣,这是我将 Anchor 插件插入菜单的实现:
from menus.base import Modifier, NavigationNode
from menus.menu_pool import menu_pool
from cms.models import Page
from cms.utils.plugins import get_plugins
from cms.templatetags.cms_tags import _get_placeholder
class AnchorPluginMenuModifier(Modifier):
"""
"""
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
# if the menu is not yet cut, don't do anything
if post_cut:
return nodes
# otherwise loop over the nodes
newnodes = []
for node in nodes:
try:
if "is_page" in node.attr and node.attr["is_page"]:
page_obj = Page.objects.get(id=node.id)
template_context = {
"request" : request,
}
placeholder_name = "content"
placeholder = _get_placeholder(request.current_page, page_obj,
template_context, placeholder_name) # placeholder_name is a string
plugins = get_plugins(request, placeholder, page_obj.get_template())
for plugin in plugins:
if type(plugin).__name__ == "AnchorPluginModel":
newnode = NavigationNode(
plugin.anchor_menutitle,
node.url+"#"+plugin.anchor_name,
"{}-{}".format(page_obj.id,plugin.id),
node
)
newnode.parent_id = node.id
newnodes.append(newnode)
setattr(newnode, "selected", False)
node.children.append(newnode)
except Exception, e:
print e
# client.captureException()
return nodes
menu_pool.register_modifier(AnchorPluginMenuModifier)
代码位于 cms_menus.py 文件中。