vstollen
vstollen

Reputation: 425

How can I add custom logic for the title of a document in Blacklight?

Instead of defining a single title_field I would like to display a custom title depending on the contents of a document. According to the Blacklight Wiki this can be accomplished by setting config.index.document_presenter_class to a custom presenter:

# app/controllers/catalog_controller.rb
class CatalogController < ApplicationController
  ...
  configure_blacklight do |config|
    ...
    config.index.document_presenter_class = MyPresenter
    ...
  end
  ...
end

Where the custom presenter overrides the label method:

# app/presenters/my_presenter.rb
class MyPresenter < Blacklight::IndexPresenter
  def label(field_or_string_or_proc, opts = {})
    # Assuming that :main_title and :sub_title are field names on the Solr document.
    document.first(:main_title) + " - " + document.first(:sub_title)
  end
end

When I do this my custom label method doesn't seem to get called, which I checked by adding a puts statement and a debugger breakpoint.

Is there something I could have missed or another way to display custom document titles?

Upvotes: 1

Views: 71

Answers (1)

vstollen
vstollen

Reputation: 425

Overriding the heading method in my presenter class worked for me:

# app/presenters/my_presenter.rb
class MyPresenter < Blacklight::IndexPresenter
  def heading
    # Assuming that :main_title and :sub_title are field names on the Solr document.
    document.first(:main_title) + " - " + document.first(:sub_title)
  end
end

Upvotes: 2

Related Questions