diff --git a/v8.0.0.alpha/16-other-config.md b/v8.0.0.alpha/16-other-config.md new file mode 100644 index 0000000..8352e41 --- /dev/null +++ b/v8.0.0.alpha/16-other-config.md @@ -0,0 +1,143 @@ +--- +layout: page +title: Other configuration +permalink: /v8.0.0.alpha/other-config/ +blacklight_version: v8.0.0.alpha +--- + +## Views + +Blacklight has the concept of a "view", which is a set of configuration options that are used to render a search results page. We call default view `list`. In this exercise, we'll go through steps to add a "gallery" view that highlights the thumbnails. + +```ruby +# app/controllers/catalog_controller.rb +configure_blacklight do |config| + config.view.gallery(default_thumbnail: :random_thumbnail) +end +``` + +```ruby +# app/helpers/application_helper.rb +def random_thumbnail(...) + image_tag "https://loremflickr.com/320/180?random=#{Random::DEFAULT.rand(100)}" +end +``` + +```css +/* app/assets/stylesheets/blacklight.scss */ + +.documents-gallery { + display: grid; + gap: 1rem; + grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); + + article { + @extend .card; + display: flex; + flex-direction: column-reverse; + justify-content: start; + overflow: hidden; + + .document-thumbnail { + @extend .card-img-top; + } + + .document-main-section { + @extend .card-body; + } + + dt { + text-align: left; + width: 100%; + } + } +} +``` + +As with fields, sometimes you want even more control over how Blacklight renders the documents. You can do this by creating a custom view component. In this example, we'll create a custom view component that renders the gallery view with the Boostrap card markup we want. + + +```ruby +# app/components/gallery_document_component.rb +class GalleryDocumentComponent < Blacklight::DocumentComponent +end +``` + +```ruby +# app/components/gallery_document_component.html.erb +
<%= @document[:author_tsim]&.to_sentence %>
+<%= JSON.pretty_generate(@documents.map(&:to_h)) %>+<% end %> +``` + +But what if we want to download it instead of displaying it in the browser? + + +```ruby +# app/controllers/catalog_controller.rb + config.add_show_tools_partial(:download, component: BlahComponent) + ... + def download + render json: action_documents.first.as_json + end +``` + +``` +class BlahComponent < Blacklight::Document::ActionComponent + def call + link_to 'Download', url, class: 'btn btn-primary', download: 'document.json' + end +end +``` + + +## Other configuration