diff --git a/js/facets-views-ajax.js b/js/facets-views-ajax.js index 0ee7077..d9ef90e 100644 --- a/js/facets-views-ajax.js +++ b/js/facets-views-ajax.js @@ -163,7 +163,11 @@ } }).join(); var block_selector = '#' + $(this).attr('id'); - facets_blocks[block_id] = block_selector; + facets_blocks[block_id] = { + selector: block_selector, + type: $(this).data('facet-type'), + id: $(this).data('facet-id') + }; }); return facets_blocks; diff --git a/src/Controller/FacetBlockAjaxController.php b/src/Controller/FacetBlockAjaxController.php index 6c52814..91c58d2 100644 --- a/src/Controller/FacetBlockAjaxController.php +++ b/src/Controller/FacetBlockAjaxController.php @@ -124,7 +124,17 @@ class FacetBlockAjaxController extends ControllerBase { } // Make sure we are not updating blocks multiple times. - $facets_blocks = array_unique($facets_blocks); + // Note: + // - This line was used back when $facet_blocks was an array of + // key/value where value was the block selector. + // - To make the same on this new structure, it would take to array_filter an + // keep referenced of traversed selectors. + // - Selectors are IDs (@see facets-views-ajax.js facetsBlocks() function) + // - IDs are generated unique for our blocks (@see FacetBlock.php) + // - Thus this dedupe process fills uselessly coslty on this new structure. + // Conclusion: I let this commented line in code in case edge use case needs to add it + // back. + // $facets_blocks = array_unique($facets_blocks); $new_request = Request::create($path); $request_stack = new DrupalRequestStack(); @@ -142,17 +152,26 @@ class FacetBlockAjaxController extends ControllerBase { $active_facet = $request->request->get('active_facet'); // Build the facets blocks found for the current request and update. - foreach ($facets_blocks as $block_id => $block_selector) { - $block_entity = $this->storage->load($block_id); - - if ($block_entity) { - // Render a block, then add it to the response as a replace command. - $block_view = $this->entityTypeManager - ->getViewBuilder('block') - ->view($block_entity); - + foreach ($facets_blocks as $block_id => $block) { + // If the block may have been inserted via Block Manager. was inserted as a block, + // @see Drupal\facets\Plugin\Block\FacetBlock::build(); + if ($block['type'] === 'block') { + $block_entity = $this->storage->load($block['id']); + if ($block_entity) { + // Render a block, then add it to the response as a replace command. + $block_view = $this->entityTypeManager + ->getViewBuilder('block') + ->view($block_entity); + + $block_view = (string) $this->renderer->renderPlain($block_view); + $response->addCommand(new ReplaceCommand($block['selector'], $block_view)); + } + } + // The block may have been render programmatically from the plugin_id. + if ($block['type'] === 'plugin') { + $block_view = \Drupal::service('plugin.manager.block')->createInstance($block['id'])->build(); $block_view = (string) $this->renderer->renderPlain($block_view); - $response->addCommand(new ReplaceCommand($block_selector, $block_view)); + $response->addCommand(new ReplaceCommand($block['selector'], $block_view)); } } diff --git a/src/Plugin/Block/FacetBlock.php b/src/Plugin/Block/FacetBlock.php index 00c68f1..5192731 100644 --- a/src/Plugin/Block/FacetBlock.php +++ b/src/Plugin/Block/FacetBlock.php @@ -3,6 +3,7 @@ namespace Drupal\facets\Plugin\Block; use Drupal\Core\Access\AccessResult; +use Drupal\Component\Utility\Html; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; @@ -117,9 +118,16 @@ class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface { // The configuration block id isn't always set in the configuration. if (isset($this->configuration['block_id'])) { $build['#attributes']['class'][] = 'js-facet-block-id-' . $this->configuration['block_id']; + $build['#attributes']['data-facet-type'] = 'block'; + $build['#attributes']['data-facet-id'] = $this->configuration['block_id']; } else { - $build['#attributes']['class'][] = 'js-facet-block-id-' . $this->pluginId; + $build['#attributes']['data-facet-type'] = 'plugin'; + $build['#type'] = 'container'; + $unique_id = Html::getUniqueId($this->pluginId); + $build['#attributes']['data-facet-id'] = $this->pluginId; + $build['#attributes']['id'] = $unique_id; + $build['#attributes']['class'][] = 'js-facet-block-id-' . $unique_id; } } }