How can a theme modify the behaviour of NodeJS?



  • Hi.

    In my theme, I want to modify the breadcrumb on pages like the category page and the topic page so that the top-level category is not a link.
    For example in the following breadcrumb, I want Home, B and C do be links, but A and D to be simple text:

    Home > A > B > C > D
    

    My problem is that the breadcrumb.tpl doesn't "receive" the required data to be able to see which breadcrumb entry is what in the loop.

    I want to modify (replace) the helpers#buildCategoryBreadcrumbs function with my theme.
    The idea is too add the category's parent CID to each breadcrumb entry, so that I could check if it exists in the template to see if it's a top-level category.

    How could I do that?

    • I'm not sure if it's a good thing if it's in NodeBB itself, so I don't know if I should make a PR for it
    • I don't know if a theme is able to replace js files like that
    • I don't know if a plugin (as themes are plugins afaik) can do that either

    Thanks

    EDIT: For now, I've got this:

    • breadcrumb.tpl in theme:
    <ol class="breadcrumb">
    	<!-- BEGIN breadcrumbs -->
    	<li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" <!-- IF @last -->class="active"<!-- ENDIF @last -->>
    		<!-- IF !@last --><!-- IF !breadcrumbs.isTopLevelCategory --><a href="{breadcrumbs.url}" itemprop="url"><!-- ENDIF !breadcrumbs.isTopLevelCategory --><!-- ENDIF !@last -->
    			<span itemprop="title">
    				{breadcrumbs.text}
    				<!-- IF @last -->
    				<!-- IF !feeds:disableRSS -->
    				<!-- IF rssFeedUrl --><a target="_blank" href="{rssFeedUrl}"><i class="fa fa-rss-square"></i></a><!-- ENDIF rssFeedUrl --><!-- ENDIF !feeds:disableRSS -->
    				<!-- ENDIF @last -->
    			</span>
    		<!-- IF !@last --><!-- IF !breadcrumbs.isTopLevelCategory --></a><!-- ENDIF !breadcrumbs.isTopLevelCategory --><!-- ENDIF !@last -->
    	</li>
    	<!-- END breadcrumbs -->
    </ol>
    
    • helpers#buildCategoryBreadcrumbs in src/controllers/helpers.js
    helpers.buildCategoryBreadcrumbs = function(cid, callback) {
    	var breadcrumbs = [];
    
    	async.whilst(function() {
    		return parseInt(cid, 10);
    	}, function(next) {
    		categories.getCategoryFields(cid, ['name', 'slug', 'parentCid'], function(err, data) {
    			if (err) {
    				return next(err);
    			}
    
    			breadcrumbs.unshift({
    				text: validator.escape(data.name),
    				url: nconf.get('relative_path') + '/category/' + data.slug,
    				isTopLevelCategory: data.parentCid == 0 ? 1 : 0
    			});
    
    			cid = data.parentCid;
    			next();
    		});
    	}, function(err) {
    		if (err) {
    			return callback(err);
    		}
    
    		breadcrumbs.unshift({
    			text: '[[global:home]]',
    			url: nconf.get('relative_path') + '/'
    		});
    
    		callback(null, breadcrumbs);
    	});
    };
    


  • Bumb, this single line of JS added is the onyl change I have to do to NodeBB's sources for my theme to work, would be nice if I could get rid of it!


  • Admin

    @Ribesg Building the breadcrumbs for a topic page is different from the regular breadcrumbs because we need to climb up through the parents to find the path back to the index page.

    So perhaps we could just add hasParent to the breadcrumb data, and then you could use it in your template.

    Would that work for you?



  • @julian I think, yes, as this would have the same value than my own change.


  • Admin

    Feel free to add a pull request for this if you haven't already 🙂


 

| |

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.