You are here

class JsonApiResponseWidget in JSON:API Search API 8

A simple widget class that returns for inclusion in JSON:API Search API.

@FacetsWidget( id = "jsonapi_search_api", label = @Translation("JSON:API Search API"), description = @Translation("A widget that builds an array with results. Used only for integrating into JSON:API Search API."), )

@note: This widget is almost identical to ArrayWidget except changing how URLs are being generated as to not leak cacheable metadata.

Hierarchy

Expanded class hierarchy of JsonApiResponseWidget

File

modules/jsonapi_search_api_facets/src/Plugin/facets/widget/JsonApiResponseWidget.php, line 21

Namespace

Drupal\jsonapi_search_api_facets\Plugin\facets\widget
View source
class JsonApiResponseWidget extends WidgetPluginBase {

  /**
   * {@inheritdoc}
   */
  public function build(FacetInterface $facet) {
    $build = [
      'id' => $facet
        ->id(),
      'label' => $facet
        ->getName(),
      'path' => $facet
        ->getUrlAlias(),
      'terms' => [],
    ];
    $configuration = $facet
      ->getWidget();
    $this->showNumbers = empty($configuration['show_numbers']) ? FALSE : (bool) $configuration['show_numbers'];
    foreach ($facet
      ->getResults() as $result) {
      if ($result
        ->getUrl() === NULL) {
        $build['terms'][] = $this
          ->generateValues($result);
      }
      else {
        $build['terms'][] = $this
          ->buildListItems($facet, $result);
      }
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function buildListItems(FacetInterface $facet, ResultInterface $result) {
    if ($children = $result
      ->getChildren()) {
      $items = $this
        ->prepare($result);
      $children_markup = [];
      foreach ($children as $child) {
        $children_markup[] = $this
          ->buildChildren($child);
      }
      $items['children'] = [
        $children_markup,
      ];
    }
    else {
      $items = $this
        ->prepare($result);
    }
    return $items;
  }

  /**
   * Prepares the URL and values for the facet.
   *
   * @param \Drupal\facets\Result\ResultInterface $result
   *   A result item.
   *
   * @return array
   *   The results.
   */
  protected function prepare(ResultInterface $result) {
    $values = $this
      ->generateValues($result);
    $url = $result
      ->getUrl();
    if ($url === NULL) {
      $facet_values = $values;
    }
    else {
      $facet_values['url'] = $url
        ->setAbsolute(TRUE)
        ->toString(TRUE)
        ->getGeneratedUrl();
      $facet_values['values'] = $values;
    }
    return $facet_values;
  }

  /**
   * Builds an array for children results.
   *
   * @param \Drupal\facets\Result\ResultInterface $child
   *   A result item.
   *
   * @return array
   *   An array with the results.
   */
  protected function buildChildren(ResultInterface $child) {
    $values = $this
      ->generateValues($child);
    $url = $child
      ->getUrl();
    if ($url !== NULL) {
      $facet_values['url'] = $url
        ->setAbsolute(TRUE)
        ->toString(TRUE)
        ->getGeneratedUrl();
      $facet_values['values'] = $values;
    }
    else {
      $facet_values = $values;
    }
    return $facet_values;
  }

  /**
   * Generates the value and the url.
   *
   * @param \Drupal\facets\Result\ResultInterface $result
   *   The result to extract the values.
   *
   * @return array
   *   The values.
   */
  protected function generateValues(ResultInterface $result) {
    $values = [
      'value' => $result
        ->getRawValue(),
      'label' => $result
        ->getDisplayValue(),
      'active' => $result
        ->isActive(),
    ];
    if ($this->configuration['show_numbers']) {
      $values['count'] = (int) $result
        ->getCount();
    }
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
JsonApiResponseWidget::build public function Builds the facet widget for rendering. Overrides WidgetPluginBase::build
JsonApiResponseWidget::buildChildren protected function Builds an array for children results.
JsonApiResponseWidget::buildListItems protected function Builds a renderable array of result items. Overrides WidgetPluginBase::buildListItems
JsonApiResponseWidget::generateValues protected function Generates the value and the url.
JsonApiResponseWidget::prepare protected function Prepares the URL and values for the facet.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WidgetPluginBase::$facet protected property The facet the widget is being built for.
WidgetPluginBase::$showNumbers protected property Show the amount of results next to the result.
WidgetPluginBase::buildConfigurationForm public function Provides a configuration form for this widget. Overrides WidgetPluginInterface::buildConfigurationForm 3
WidgetPluginBase::buildResultItem protected function Builds a facet result item.
WidgetPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
WidgetPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
WidgetPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
WidgetPluginBase::getFacetItemListThemeHook protected function Provides a full array of possible theme functions to try for a given hook.
WidgetPluginBase::getQueryType public function Picks the preferred query type for this widget. Overrides WidgetPluginInterface::getQueryType 3
WidgetPluginBase::isPropertyRequired public function Checks is a specific property is required for this widget. Overrides WidgetPluginInterface::isPropertyRequired 2
WidgetPluginBase::prepareLink protected function Returns the text or link for an item.
WidgetPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
WidgetPluginBase::supportsFacet public function Checks if the facet is supported by this processor. Overrides WidgetPluginInterface::supportsFacet 1
WidgetPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct