Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewilkybarkid/6bfab0162382c204cb75 to your computer and use it in GitHub Desktop.
Save thewilkybarkid/6bfab0162382c204cb75 to your computer and use it in GitHub Desktop.
diff --git a/facetapi_bonus.info b/facetapi_bonus.info
index f9ba779..a716118 100644
--- a/facetapi_bonus.info
+++ b/facetapi_bonus.info
@@ -7,6 +7,7 @@ dependencies[] = facetapi
files[] = plugins/facetapi/dependency_facet.inc
files[] = plugins/facetapi/filter_rewrite_items.inc
files[] = plugins/facetapi/filter_exclude_specified_items.inc
+files[] = plugins/facetapi/widget_links_radio.inc
files[] = plugins/facetapi/filter_narrow_results.inc
files[] = plugins/facetapi/filter_show_if_minimum_items.inc
files[] = plugins/facetapi/filter_show_deepest_level_items.inc
diff --git a/facetapi_bonus.module b/facetapi_bonus.module
index 3df1e21..5ded35a 100644
--- a/facetapi_bonus.module
+++ b/facetapi_bonus.module
@@ -50,3 +50,18 @@ function facetapi_bonus_facetapi_filters() {
),
);
}
+
+/**
+ * Implements hook_facetapi_widgets().
+ */
+function facetapi_bonus_facetapi_widgets() {
+ return array(
+ 'facetapi_radio_links' => array(
+ 'handler' => array(
+ 'label' => t('Links with radio buttons'),
+ 'class' => 'FacetapiWidgetRadioLinks',
+ 'query types' => array('term', 'date'),
+ ),
+ ),
+ );
+}
diff --git a/facetapi_bonus_widget_links_radio.js b/facetapi_bonus_widget_links_radio.js
new file mode 100644
index 0000000..3ed9869
--- /dev/null
+++ b/facetapi_bonus_widget_links_radio.js
@@ -0,0 +1,65 @@
+(function ($) {
+
+ Drupal.behaviors.facetapi_bonus = {
+ attach: function(context, settings) {
+ if (settings.facetapi) {
+ for (var index in settings.facetapi.facets) {
+ if (null != settings.facetapi.facets[index].makeRadios) {
+ // Find all radio facet links and give them a radio button.
+ var elements = $(settings.facetapi.facets[index].selector + ' a.facetapi-radio', context);
+ if (elements.length === 0) {
+ $('#' + settings.facetapi.facets[index].id + ' a.facetapi-radio', context).each(Drupal.facetapi_bonus.makeRadio);
+ }
+ else {
+ $(settings.facetapi.facets[index].selector + ' a.facetapi-radio', context).each(Drupal.facetapi_bonus.makeRadio);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Class containing functionality for Facet API.
+ */
+ Drupal.facetapi_bonus = {};
+
+ /**
+ * Replace an unclick link with a checked radio button.
+ */
+ Drupal.facetapi_bonus.makeRadio = function() {
+ var $link = $(this);
+ if (!$link.hasClass('facetapi-radio-processed')) {
+ var active;
+ if ($link.hasClass('facetapi-inactive')) {
+ active = false;
+ }
+ else if ($link.hasClass('facetapi-active')) {
+ active = true;
+ }
+ else {
+ // Not a facet link.
+ return;
+ }
+ // Derive an ID and label for the radio based on the associated link.
+ // The label is required for accessibility, but it duplicates information
+ // in the link itself, so it should only be shown to screen reader users.
+ var id = this.id + '--radio';
+ var description = $link.find('.element-invisible').html();
+ var label = $('<label class="element-invisible" for="' + id + '">' + description + '</label>');
+ var radio = active ? $('<input type="radio" class="facetapi-radio" id="' + id + '" checked="true" />') : $('<input type="radio" class="facetapi-radio" id="' + id + '" />');
+ // Get the href of the link that is this DOM object.
+ var href = $link.attr('href');
+ redirect = new Drupal.facetapi.Redirect(href);
+ if (active) {
+ // Add the radio and label, hide the link.
+ $link.before(label).before(radio).hide();
+ }
+ else {
+ $link.before(label).before(radio);
+ radio.click($.proxy(redirect, 'gotoHref'));
+ }
+ $link.removeClass('facetapi-radio').addClass('facetapi-radio-processed');
+ }
+ }
+})(jQuery);
diff --git a/plugins/facetapi/widget_links_radio.inc b/plugins/facetapi/widget_links_radio.inc
new file mode 100644
index 0000000..84fa96d
--- /dev/null
+++ b/plugins/facetapi/widget_links_radio.inc
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * Widget that renders facets as a list of clickable radio buttons.
+ *
+ * This widget renders facets in the same way as FacetapiWidgetLinks but uses
+ * JavaScript to transform the links into radio buttons followed by the facet.
+ */
+class FacetapiWidgetRadioLinks extends FacetapiWidgetLinks {
+
+ /**
+ * Overrides FacetapiWidgetLinks::init().
+ *
+ * Adds additional JavaScript file & settings.
+ */
+ public function init() {
+ parent::init();
+ $this->jsSettings['makeRadios'] = 1;
+ $this->jsSettings['selector'] = '.' . $this->id . '_' . str_replace(':', '', $this->key);
+
+ drupal_add_js(drupal_get_path('module', 'facetapi_bonus') . '/facetapi_bonus_widget_links_radio.js');
+ }
+
+ /**
+ * Overrides FacetapiWidgetLinks::getItemClasses().
+ *
+ * Sets the base class for radio facet items.
+ */
+ public function getItemClasses() {
+ return array('facetapi-radio');
+ }
+}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment