From 4fee9e6216812e457f615333b0b632666f191de4 Mon Sep 17 00:00:00 2001 From: "desarrollo2.0" Date: Wed, 5 Dec 2018 17:03:47 +0100 Subject: [PATCH] Issue #2984465 by Nick_vh, borisson_, seanB, akalam, ChristianAdamski: Hide facets when it is configured to hide the block without losing support for bigpipe and ajax facets --- facets.libraries.yml | 8 ++++++++ js/facets-hide-when-empty.js | 24 ++++++++++++++++++++++++ src/FacetManager/DefaultFacetManager.php | 10 +++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 js/facets-hide-when-empty.js diff --git a/facets.libraries.yml b/facets.libraries.yml index 51b2bec..9ab23da 100644 --- a/facets.libraries.yml +++ b/facets.libraries.yml @@ -68,3 +68,11 @@ drupal.facets.views-ajax: - core/drupal - core/drupalSettings - core/drupal.ajax + +facets-hide-when-empty: + version: VERSION + js: + js/facets-hide-when-empty.js: {} + dependencies: + - core/jquery + - core/drupal diff --git a/js/facets-hide-when-empty.js b/js/facets-hide-when-empty.js new file mode 100644 index 0000000..d26f3a1 --- /dev/null +++ b/js/facets-hide-when-empty.js @@ -0,0 +1,24 @@ +/** + * @file + * Hide facets that should not be shown in javascript, as we need to support + * AJAX & Bigpipe. + */ + +(function ($) { + + 'use strict'; + + Drupal.behaviors.facetsEditForm = { + attach: function (context, settings) { + // if the .block-facets wrapper contains .facet-hide-when-empty somewhere, + // hide the parent. + $('.block-facets').each(function () { + var $this = $(this); + if ($this.find('.facet-hide-when-empty').length) { + $this.hide(); + } + }); + } + }; + +})(jQuery); diff --git a/src/FacetManager/DefaultFacetManager.php b/src/FacetManager/DefaultFacetManager.php index 699f2e6..5dcc7c0 100644 --- a/src/FacetManager/DefaultFacetManager.php +++ b/src/FacetManager/DefaultFacetManager.php @@ -343,7 +343,15 @@ class DefaultFacetManager { '#type' => 'container', '#attributes' => [ 'data-drupal-facet-id' => $facet->id(), - 'class' => ['facet-empty'], + 'class' => [ + 'facet-empty', + 'facet-hide-when-empty', + ], + ], + '#attached' => [ + 'library' => [ + 'facets/facets-hide-when-empty', + ], ], ], ]; -- 2.14.1