I would like to apply badges to numbers found in the sidebar facets.
From this:
To this:
I can't seem to find the correct file to edit. I tried to edit the SidebarFacetsTransformer.java
in line 248 and line 260:
if (i < shownFacets - 1) {
String displayedValue = value.getDisplayedValue();
String filterQuery = value.getAsFilterQuery();
String filterType = value.getFilterType();
if (fqs.contains(getSearchService().toFilterQuery(context, field.getIndexFieldName(), value.getFilterType(), value.getAsFilterQuery()).getFilterQuery())) {
filterValsList.addItem(Math.random() + "", "selected").addContent(displayedValue + " <span class=\"badge\">" + value.getCount() + "</span>");
} else {
String paramsQuery = retrieveParameters(request);
filterValsList.addItem().addXref(
contextPath +
(dso == null ? "" : "/handle/" + dso.getHandle()) +
"/discover?" +
paramsQuery +
"filtertype=" + field.getIndexFieldName() +
"&filter_relational_operator="+ filterType +
"&filter=" + encodeForURL(filterQuery),
displayedValue + " <span class=\"badge\">" + value.getCount() + "</span>"
);
}
}
but after rebuilding DSpace, the tags <span class="badge">
were also displayed:
So what file should I edit to apply the badges to the sidebar counts? I've seen one repository using Mirage2 as its base theme and applying the badges in the sidebar counts: University of Waikato Research Commons.
Thanks in advance!
UPDATE
After applying the code from @schweerelos answer, I got the desired result except for the sidebar for Has Files(s). Instead of figures, it is just showing No.
For the Research Commons badges, we modified dspace-xmlui-mirage2/src/main/webapp/xsl/preprocess.xsl
and intercepted the rendering of the corresponding elements, ie
<!-- render frequency counts in sidebar facets as badges -->
<xsl:template match="dri:list[@id='aspect.discovery.Navigation.list.discovery']/dri:list/dri:item/dri:xref">
<xref>
<xsl:call-template name="copy-attributes"/>
<xsl:choose>
<xsl:when test="contains(text(), ' (') and contains(substring-after(text(), ' ('), ')')">
<xsl:variable name="title">
<xsl:call-template name="substring-before-last">
<xsl:with-param name="string" select="text()"/>
<xsl:with-param name="separator"><xsl:text> (</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="count">
<xsl:call-template name="remove-parens">
<xsl:with-param name="string" select="normalize-space(substring-after(text(), $title))"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$title"/>
<xsl:text> </xsl:text>
<hi rend="badge">
<xsl:value-of select="$count"/>
</hi>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xref>
</xsl:template>
And a similar template for the currently selected facet value (that IIRC isn't a link):
<!-- better highlight active co-author/subject etc in facet list, incl show count as badge -->
<xsl:template match="dri:list[@id='aspect.discovery.Navigation.list.discovery']/dri:list/dri:item[@rend='selected']">
<item>
<xsl:call-template name="copy-attributes"/>
<xsl:attribute name="rend"><xsl:value-of select="@rend"/> disabled</xsl:attribute>
<xsl:choose>
<xsl:when test="contains(text(), ' (') and contains(substring-after(text(), ' ('), ')')">
<xsl:variable name="title">
<xsl:call-template name="substring-before-last">
<xsl:with-param name="string" select="text()"/>
<xsl:with-param name="separator"><xsl:text> (</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="count">
<xsl:call-template name="remove-parens">
<xsl:with-param name="string" select="normalize-space(substring-after(text(), $title))"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$title"/>
<xsl:text> </xsl:text>
<hi rend="badge">
<xsl:value-of select="$count"/>
</hi>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</item>
</xsl:template>
You'll see that there is a bit of XSL/T trickery involved to cope with cases where the facet value itself contains parentheses, with a couple of utility templates:
<xsl:template name="substring-before-last">
<xsl:param name="string" select="''" />
<xsl:param name="separator" select="''" />
<xsl:if test="$string != '' and $separator != ''">
<xsl:variable name="head" select="substring-before($string, $separator)" />
<xsl:variable name="tail" select="substring-after($string, $separator)" />
<xsl:value-of select="$head" />
<xsl:if test="contains($tail, $separator)">
<xsl:value-of select="$separator" />
<xsl:call-template name="substring-before-last">
<xsl:with-param name="string" select="$tail" />
<xsl:with-param name="separator" select="$separator" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="remove-parens">
<xsl:param name="string" select="''" />
<xsl:if test="starts-with($string, '(') and substring($string, string-length($string))=')'">
<xsl:value-of select="substring($string, 2, string-length($string) - 2)"/>
</xsl:if>
</xsl:template>
Just a note to add that we have a local preference for modifying the theme XSL files over modifying the Java code. You could do this in the Java code as an alternative, but you'd need to work within the DRI API, eg make .addHighlight("badge")
Java method calls together with the addXref
(DRI hi
gets transformed to HTML span
). See DRI documentation at https://wiki.duraspace.org/display/DSDOC5x/DRI+Schema+Reference (for 5.x but unchanged for 6.x).
The Cocoon pipeline is Java produces DRI (XML) -> preprocess.xsl and related files modify the DRI and output is still DRI -> theme.xsl and related files transform the DRI into HTML. You're trying to create HTML "too early" in the pipeline. Your raw HTML is escaped further down the track, which is why you're seeing the tags on screen.