layout | title | permalink | redirect_from | blacklight_version |
---|---|---|---|---|
page |
Helper method overrides |
/v7.11.1/helper-method-overrides/ |
/overrides/ |
v7.11.1 |
Much of Blacklight is written in a way that is overridable, helper methods are no different.
Let's take a look at the module that is used to help with some of the layout for Blacklight. This module is mixed into the Blacklight::BlacklightHelperBehavior
which allows us to override methods mixed in.
For example, the html_tag_attributes
method is used to inject HTML tag attributes into the main Blacklight layout.
{% highlight ruby %}
def html_tag_attributes { lang: I18n.locale } end {% endhighlight %}
If we want to modify this method to return something different, we first need to create the CustomLayoutHelper
module in our local application.
{% highlight bash %} $ touch app/helpers/custom_layout_helper.rb {% endhighlight %}
And then within custom_layout_helper.rb
we need to include the Blacklight::LayoutHelperBehavior
mixin.
{% highlight ruby %}
module CustomLayoutHelper include Blacklight::LayoutHelperBehavior end {% endhighlight %}
Now we are free to override methods to meet our custom application needs. For example, lets override the html_tag_attributes
method.
{% highlight ruby %}
module CustomLayoutHelper include Blacklight::LayoutHelperBehavior
def html_tag_attributes super.merge(dir: 'rtl') end end {% endhighlight %}
This overridden method adds an additional attribute dir="rtl"
to display our page right-to-left. While this is not a fully sufficient solution to implementing right-to-left behavior in your site, it does demonstrate how this helper can be extended.
This is just one way that the Blacklight code can be overridden and customized. Similar patterns can be used to customize controllers and search behavior.