如何在 Blacklight 中为文档标题添加自定义逻辑?
How can I add custom logic for the title of a document in Blacklight?
我不想定义单个 title_field
,而是想根据文档的内容显示自定义标题。根据 Blacklight Wiki 这可以通过将 config.index.document_presenter_class
设置为自定义演示者来实现:
# app/controllers/catalog_controller.rb
class CatalogController < ApplicationController
...
configure_blacklight do |config|
...
config.index.document_presenter_class = MyPresenter
...
end
...
end
自定义演示者覆盖 label
方法的地方:
# 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
当我这样做时,我的自定义标签方法似乎没有被调用,我通过添加 puts
语句和调试器断点进行了检查。
是否有我可能遗漏的内容或显示自定义文档标题的其他方式?
在我的演示者中覆盖 heading
方法 class 对我有用:
# 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
我不想定义单个 title_field
,而是想根据文档的内容显示自定义标题。根据 Blacklight Wiki 这可以通过将 config.index.document_presenter_class
设置为自定义演示者来实现:
# app/controllers/catalog_controller.rb
class CatalogController < ApplicationController
...
configure_blacklight do |config|
...
config.index.document_presenter_class = MyPresenter
...
end
...
end
自定义演示者覆盖 label
方法的地方:
# 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
当我这样做时,我的自定义标签方法似乎没有被调用,我通过添加 puts
语句和调试器断点进行了检查。
是否有我可能遗漏的内容或显示自定义文档标题的其他方式?
在我的演示者中覆盖 heading
方法 class 对我有用:
# 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