How to add a menu item to CKAN's naivigation menu?

后端 未结 3 1570
青春惊慌失措
青春惊慌失措 2021-01-27 00:30

I want to make the functionality of a custom extension available via a menu item in the CKAN\'s main navigation menu. Not sure how I can add this new menu item from my extension

相关标签:
3条回答
  • 2021-01-27 00:44

    You just need to add the relevant templates into your extension with the HTML for your menu items. See http://docs.ckan.org/en/latest/theming/index.html

    0 讨论(0)
  • 2021-01-27 00:46

    If you want to take advantage of CKAN's main navigation menu, it gets a little tricky. (We just had to figure this out. Not sure if we did it the right way, but we did get it to work with CKAN 2.2):

    First, you need the additional content in your extension path somewhere. Assuming it's at my_extension/amazing.html, you want to add the following to the my_extension/templates/header.html file (in your custom extension):

    {% ckan_extends %}
    
    {% block header_site_navigation_tabs %}
      {{ h.build_nav_main(
        ('search', _('Datasets')),
        ('organizations_index', _('Organizations')),
        ('group_index', _('Groups')),
        ('about', _('About')),
        ('amazing', _('Amazing Extension'))
      ) }}
    {% endblock %}
    

    This will cause a server error, since "amazing" hasn't been registered in pylons as a mapped route. We'll fix that next. If you've built your extension properly, you should have a my_extension/plugins.py, to which you'll need to add the following to your plugin's class definition:

    class AmazingPlugin(plugins.SingletonPlugin, tk.DefaultDatasetForm):
        #or
        plugins.implements(plugins.IRoutes, inherit=True)
        def before_map(self, m):
            m.connect('amazing', #name of path route
                '/amazing', #url to map path to
                controller='ckanext.my_extension.controller:AmazingController', #controller
                action='amazing') #controller action (method)
            return m
    

    ..where ckanext.my_extension.controller is the include path to my_extension/controller.py, which we'll create next. It should consist of:

    import ckan.plugins as p
    from ckan.lib.base import BaseController
    
    class AmazingController(BaseController):
        def amazing(self):
            return p.toolkit.render('amazing.html')
    

    That's it. Now you've got a controller mapping a url to a nav item that you can call with h.build_nav_main(). Simple, right? ;) One last thing. You'll need to restart apache for it to take effect.

    Edit: there was a misspelling in the header.html part

    0 讨论(0)
  • 2021-01-27 01:01

    The CKAN Pages extension (for CKAN 2.2+) allows the addition of basic pages to CKAN, including the addition of appropriate links to the main menu. Admin users can configure the order of the pages within the menu and use CKAN's preferred Markdown language to set the content of the pages. In essence, it makes CKAN into a basic CMS.

    More information: https://github.com/ckan/ckanext-pages

    The extension is an official extension originally coined by the OKNF (makers of CKAN), so it's well done and (currently) well supported.

    0 讨论(0)
提交回复
热议问题