How to customize ckan header site navigation tabs

1k views Asked by At

I would like to add extra header site navigation tabs to the default ones.

I have tried working with the solution given here but it is not working for me. I am getting Exception: menu itemapicannot be found error

This is my plugin.py code

import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit

class ApiPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IRoutes, inherit=True)
    def before_map(self, m):
        m.connect('api', #name of path route
            '/api', #url to map path to
            controller='ckanext.kimetrica_theme.controller:ApiController', #controller
            action='api') #controller action (method)
        return m

This is my header.html code

{% ckan_extends %}

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

And this is my controller.py code

import ckan.plugins as p
from ckan.lib.base import BaseController

class ApiController(BaseController):
    def api(self):
        return p.toolkit.render('api.html')

I expect to have the api menu work like the rest of the menu do. I also have my template(api.html) in place

3

There are 3 answers

0
joseluke On BEST ANSWER

I solved this question by using ckanext-pages extension This extension allows you to add simple static pages and blogs and edit their contents.

2
user3366016 On

Based on what you posted it looks like you haven't setup plugins.implements(plugins.IConfigurer, inherit=True) to register your new template. Try referencing this extension as an example. https://github.com/ckan/ckan/blob/2.8/ckanext/stats/plugin.py for setting up a new page.

You're on the right track for the menu.

Also what version of CKAN are you using? You may want to pswitch this to a flask blueprint. Like this https://github.com/ckan/ckan/blob/2.8/ckanext/example_flask_iblueprint/plugin.py

If you are using 2.9 (in alphha) check this issue out and the comments ckan 2.9.0 iroute before_map not invoking custom controller

0
mjacke On

I solved it by creating a new HTML file for the header, e.g. header_foo.html. Additionally, you have to change the page.html:

…
  {%- block header %}
    {% include "header_foo.html" %}
  {% endblock -%}
…

In the same way, you can hide the navigation tabs.