Created
April 20, 2020 18:53
-
-
Save zakiya/563507d0b15aba4df6f6588e0857e86a to your computer and use it in GitHub Desktop.
Limit exposed filter options.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($, Drupal) { | |
'use strict'; | |
Drupal.behaviors.viewContactContacts = { | |
attach: function(context, drupalSettings) { | |
// List of tids from pacificsource_preprocess_views_view__contact_contacts(). | |
const $tids = drupalSettings.pacificsource.view_contact_contacts.contact_tids; | |
// Target the correct view. | |
const $view = $('.view-contact-contacts'); | |
if($view.length > 0) { | |
$view.find('option').each( function(){ | |
// Remove options not included in the $tids array. | |
if (!$tids.includes($(this).val()) && $(this).val() != 'All' ) { | |
$(this).remove(); | |
} | |
}) | |
} | |
} | |
}; | |
})(jQuery, Drupal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* * Implements hook_preprocess_views_view__NAME(). | |
*/ | |
function pacificsource_preprocess_views_view__contact_contacts(&$variables) { | |
$variables['#attached']['library'][] = 'pacificsource/view_contact_contacts'; | |
$tids = []; | |
/** @var \Drupal\views\ViewExecutable $view */ | |
$view = $variables['view']; | |
$results = $view->result; | |
// Get tids from from field_insurance_type. | |
foreach ($results as $result) { | |
/** @var Drupal\node\Entity\Node $node */ | |
$node = $result->_entity; | |
$taxonomy_values = array_merge( | |
$node->field_contact_role->getValue(), | |
$node->field_counties->getValue(), | |
$node->field_insurance_type->getValue(), | |
$node->field_region->getValue(), | |
$node->field_state->getValue(), | |
); | |
foreach ($taxonomy_values as $taxonomy_value) { | |
if (!(in_array($taxonomy_value['target_id'], $tids))) { | |
$tids[] = $taxonomy_value['target_id']; | |
} | |
} | |
} | |
// Send tids to drupalSettings so they're available for use in js. | |
$variables['#attached']['drupalSettings']['pacificsource']['view_contact_contacts']['contact_tids'] = $tids; | |
} |
Would have been better to get all terms instead of listing each field.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because the filter is rendered before the view has results I decided to get the tids from the results then send them to js via drupalSettings. Then I removed the unneeded filter options via js.