You are here

protected function WebTestBase::createFacet in Core Views Facets 8

Add a facet trough the UI.

Parameters

string $name: The facet name.

string $id: The facet id.

string $field: The facet field.

string $display_id: The display id.

string $source: Facet source.

string $source_type: Either exposed or contextual.

bool $allowBlockCreation: Automatically create a block.

2 calls to WebTestBase::createFacet()
CoreViewsIntegrationTest::testContextualFilterUrlAlias in tests/src/Functional/CoreViewsIntegrationTest.php
Tests that an url alias works correctly.
CoreViewsIntegrationTest::testExposedFilterUrlAlias in tests/src/Functional/CoreViewsIntegrationTest.php
Tests that an url alias works correctly.

File

tests/src/Functional/WebTestBase.php, line 196

Class

WebTestBase
Provides the base class for web tests for Search API.

Namespace

Drupal\Tests\core_views_facets\Functional

Code

protected function createFacet($name, $id, $field = 'type', $display_id = 'page_1', $source = 'core_views_facets_basic_integration', $source_type = 'exposed', $allowBlockCreation = TRUE) {
  switch ($source_type) {
    case 'contextual':
      list($facet_source_id) = explode(':', $this->contextualFiltersFacetSourceId);
      $source_id = str_replace(':', '__', $this->contextualFiltersFacetSourceId);
      break;
    case 'exposed':
    default:
      list($facet_source_id) = explode(':', $this->exposedFiltersFacetSourceId);
      $source_id = str_replace(':', '__', $this->exposedFiltersFacetSourceId);
      break;
  }

  // We didn't have a facet source config entity yet for this facet source
  // plugin, so we create it on the fly.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('facets_facet_source');
  $storage
    ->create([
    'id' => $source_id,
    'name' => str_replace('__', ':', $source_id),
    'filter_key' => '',
    'url_processor' => 'core_views_url_processor',
  ])
    ->save();
  $facet_source = "{$facet_source_id}:{$source}__{$display_id}";

  /** @var \Drupal\facets\FacetInterface $facet */
  $facet = Facet::create([
    'id' => $id,
    'name' => $name,
    'weight' => 0,
  ]);
  $facet
    ->setFacetSourceId($facet_source);
  $facet
    ->setFieldIdentifier($field);
  $facet
    ->setUrlAlias($id);
  $facet
    ->setWidget('links', [
    'show_numbers' => TRUE,
  ]);
  $facet
    ->addProcessor([
    'processor_id' => 'url_processor_handler',
    'weights' => [
      'pre_query' => -10,
      'build' => -10,
    ],
    'settings' => [],
  ]);
  $facet
    ->setEmptyBehavior([
    'behavior' => 'none',
  ]);
  $facet
    ->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
  $facet
    ->save();
  if ($allowBlockCreation) {
    $this->blocks[$id] = $this
      ->createBlock($id);
  }
}