You are here

class GlossaryAZWidget in Search API AZ Glossary 8

Same name and namespace in other branches
  1. 8.4 src/Plugin/facets/widget/GlossaryAZWidget.php \Drupal\search_api_glossary\Plugin\facets\widget\GlossaryAZWidget
  2. 8.2 src/Plugin/facets/widget/GlossaryAZWidget.php \Drupal\search_api_glossary\Plugin\facets\widget\GlossaryAZWidget
  3. 8.3 src/Plugin/facets/widget/GlossaryAZWidget.php \Drupal\search_api_glossary\Plugin\facets\widget\GlossaryAZWidget

The GlossaryAZ widget.

Plugin annotation


@FacetsWidget(
  id = "glossaryaz",
  label = @Translation("Glossary AZ"),
  description = @Translation("A simple widget that shows a Glossary AZ"),
)

Hierarchy

Expanded class hierarchy of GlossaryAZWidget

File

src/Plugin/facets/widget/GlossaryAZWidget.php, line 21

Namespace

Drupal\search_api_glossary\Plugin\facets\widget
View source
class GlossaryAZWidget extends WidgetPluginBase implements WidgetPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function build(FacetInterface $facet) {

    /** @var \Drupal\facets\Result\Result[] $results */
    $results = $facet
      ->getResults();
    $items = [];
    $configuration = $facet
      ->getWidget()['config'];
    $enable_default_theme = empty($configuration['enable_default_theme']) ? FALSE : (bool) $configuration['enable_default_theme'];
    foreach ($results as $result) {
      $items[] = $this
        ->buildListItems($result);
    }
    $build = [
      '#theme' => 'item_list',
      '#items' => $items,
      '#cache' => [
        'contexts' => [
          'url.path',
          'url.query_args',
        ],
      ],
    ];
    if ($enable_default_theme) {
      $build['#attached'] = array(
        'library' => array(
          'search_api_glossary/drupal.search_api_glossary.facet_css',
        ),
      );
    }
    return $build;
  }

  /**
   * Builds a renderable array of result items.
   *
   * @param \Drupal\facets\Result\ResultInterface $result
   *   A result item.
   *
   * @return array
   *   A renderable array of the result.
   */
  protected function buildListItems(ResultInterface $result) {
    $classes = [
      'facet-item',
      'glossaryaz',
    ];

    // Not sure if glossary will have children.
    // Removed chilren processing for now.
    $items = $this
      ->prepareLink($result);
    if ($result
      ->isActive()) {
      $items['#attributes'] = [
        'class' => 'is-active',
      ];
      $classes[] = 'is-active';
    }
    else {
      $items['#attributes'] = [
        'class' => 'is-inactive',
      ];
    }

    // Add result, no result classes.
    if ($result
      ->getCount() == 0) {
      $classes[] = 'no-results';
    }
    else {
      $classes[] = 'yes-results';
    }
    $items['#wrapper_attributes'] = [
      'class' => $classes,
    ];
    return $items;
  }

  /**
   * Returns the text or link for an item.
   *
   * @param \Drupal\facets\Result\ResultInterface $result
   *   A result item.
   *
   * @return array
   *   The item, as a renderable array.
   */
  protected function prepareLink(ResultInterface $result) {
    $configuration = $this
      ->getConfiguration();
    $show_count = empty($configuration['show_count']) ? FALSE : (bool) $configuration['show_count'];
    $text = $result
      ->getDisplayValue();
    if ($show_count) {
      $text .= ' (' . $result
        ->getCount() . ')';
    }
    if (is_null($result
      ->getUrl()) || $result
      ->getCount() == 0) {
      $link = [
        '#markup' => $text,
      ];
    }
    else {
      $link = new Link($text, $result
        ->getUrl());
      $link = $link
        ->toRenderable();
    }
    return $link;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
    $form['show_count'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show count per Glossary item'),
    ];
    $form['enable_default_theme'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use default Glossary AZ Theme'),
    ];
    $config = $facet
      ->getWidget()['config'];
    if (!is_null($config)) {
      if (isset($config['show_count'])) {
        $form['show_count']['#default_value'] = $config['show_count'];
      }
      if (isset($config['enable_default_theme'])) {
        $form['enable_default_theme']['#default_value'] = $config['enable_default_theme'];
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getQueryType(array $query_types) {
    return $query_types['string'];
  }

}

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
GlossaryAZWidget::build public function Builds the facet widget for rendering. Overrides WidgetPluginBase::build
GlossaryAZWidget::buildConfigurationForm public function Provides a configuration form for this widget. Overrides WidgetPluginBase::buildConfigurationForm
GlossaryAZWidget::buildListItems protected function Builds a renderable array of result items. Overrides WidgetPluginBase::buildListItems
GlossaryAZWidget::getQueryType public function Picks the preferred query type for this widget. Overrides WidgetPluginBase::getQueryType
GlossaryAZWidget::prepareLink protected function Returns the text or link for an item. Overrides WidgetPluginBase::prepareLink
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::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::isPropertyRequired public function Checks is a specific property is required for this widget. Overrides WidgetPluginInterface::isPropertyRequired 2
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