You are here

class SearchApiSortsBlockDeriver in Search API sorts 8

This deriver creates a block for every index that has been created.

Hierarchy

Expanded class hierarchy of SearchApiSortsBlockDeriver

File

src/Plugin/Block/SearchApiSortsBlockDeriver.php, line 13

Namespace

Drupal\search_api_sorts\Plugin\Block
View source
class SearchApiSortsBlockDeriver implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * List of derivative definitions.
   *
   * @var array
   */
  protected $derivatives = [];

  /**
   * The entity storage used for search api sorts.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface $indexStorage
   */
  protected $indexStorage;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    $deriver = new static($container, $base_plugin_id);
    $deriver->indexStorage = $container
      ->get('entity_type.manager')
      ->getStorage('search_api_index');
    return $deriver;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
    $derivatives = $this
      ->getDerivativeDefinitions($base_plugin_definition);
    return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $base_plugin_id = $base_plugin_definition['id'];
    if (!isset($this->derivatives[$base_plugin_id])) {
      $plugin_derivatives = [];

      /** @var \Drupal\search_api\Display\DisplayPluginManagerInterface $sapi_display_manager */
      $sapi_display_manager = \Drupal::service('plugin.manager.search_api.display');
      foreach ($sapi_display_manager
        ->getInstances() as $display) {
        $machine_name = $display
          ->getPluginId();
        $plugin_derivatives[$machine_name] = [
          'search_api_index' => $display
            ->getIndex(),
          'search_api_display' => $display,
          'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name,
          'admin_label' => $this
            ->t('Sort by (@index)', [
            '@index' => strip_tags($display
              ->label()),
          ]),
        ] + $base_plugin_definition;
      }
      $this->derivatives[$base_plugin_id] = $plugin_derivatives;
    }
    return $this->derivatives[$base_plugin_id];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiSortsBlockDeriver::$derivatives protected property List of derivative definitions.
SearchApiSortsBlockDeriver::$indexStorage protected property The entity storage used for search api sorts.
SearchApiSortsBlockDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
SearchApiSortsBlockDeriver::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
SearchApiSortsBlockDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverInterface::getDerivativeDefinitions
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.