Pergunta

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 code. Any help would be appreciated.

Thanks, PK

Foi útil?

Solução

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

Outras dicas

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.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top