1

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?

2
  • You probably tried this, but have you restarted the application? I can't recall if a new Presenter would get autoloaded in dev mode or not.
    – cdmo
    Commented Jan 29, 2021 at 13:16
  • @cdmo yes, restarting didn't help, but found a solution in the meantime. I'll add it as an answer in a moment.
    – vstollen
    Commented Jan 29, 2021 at 13:24

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.