2

I would like to apply badges to numbers found in the sidebar facets.

From this:

original sidebar

To this:

enter image description here

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:

enter image description here

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.

enter image description here

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.

enter image description here

euler
  • 1,401
  • 2
  • 18
  • 39

3 Answers3

1

At this point, I think you just need to style your span.badge elements.

Looking at the University of Waikato Reseach Commons, I see the following CSS.

list-group-item>.badge {
    float: right;
}
.badge {
    display: inline-block;
    min-width: 10px;
    padding: 3px 7px;
    font-size: 12px;
    font-weight: bold;
    color: #fff;
    line-height: 1;
    vertical-align: baseline;
    white-space: nowrap;
    text-align: center;
    background-color: #777;
    border-radius: 10px;
}
terrywb
  • 3,740
  • 3
  • 25
  • 50
1

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.

schweerelos
  • 2,189
  • 2
  • 17
  • 25
  • Hi Andrea, thank you so much for this! I have a question though. After I applied the code above in `preprocess.xsl`, I got my desired output except for the **Has Fiels(s)**. Instead of showing the figures, it is showing **No** but with the badge applied. I believe this sidebar facet was only introduced in version 6x. I think the code above is overriding the code found in `core/utils.xsl`: [utils.xsl](https://github.com/DSpace/DSpace/blob/dspace-6_x/dspace-xmlui-mirage2/src/main/webapp/xsl/core/utils.xsl#L144-L156). Any ideas? Thanks! – euler Oct 17 '18 at 06:45
  • Hi euler, you should be able to prevent the template from being applied to that facet by changing the match in my first 2 templates to exclude the DRI list element for the "has files" facet, eg `dri:list[@id='aspect.discovery.Navigation.list.discovery']/dri:list[@id!='aspect.discovery.SidebarFacetsTransformer.list.has_content_in_original_bundle']/dri:item/dri:xref` - ah right, it looks like that facet gets special treatment somewhere - so you may have to combine the exclusion with modifying utils.xsl further. We use 5.x so don't have that facet. – schweerelos Oct 17 '18 at 22:27
1

Thanks for @schweerelos for her answer, I managed to apply her code with some modifications to address the issue in the Has Files(s) facet which was introduced in DSpace version 6.x.

I uncommented this code in utils.xsl:

<xsl:template match="//dri:list[@id='aspect.discovery.SidebarFacetsTransformer.list.has_content_in_original_bundle']/dri:item//text()">
    <xsl:choose>
        <xsl:when test="contains(.,'true')">
            <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_true</i18n:text>
        </xsl:when>
        <xsl:otherwise>
            <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_false</i18n:text>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text> </xsl:text>
    <xsl:value-of select="substring-after(.,' ')"/>
</xsl:template>

and then I modified Andrea's code to display No or Yes for records that have an ORIGINAL bundle:

<!-- 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" priority="9">
    <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:choose>
                    <xsl:when test="contains(.,'true')">
                        <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_true</i18n:text>
                    </xsl:when>
                    <xsl:when test="contains(.,'false')">
                        <i18n:text>xmlui.ArtifactBrowser.AdvancedSearch.value_has_content_in_original_bundle_false</i18n:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$title"/>
                    </xsl:otherwise>
                </xsl:choose>
                <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>

Now it is showing my desired output:

enter image description here

euler
  • 1,401
  • 2
  • 18
  • 39