You are here

class SolrDocumentDeriver in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/DataType/Deriver/SolrDocumentDeriver.php \Drupal\search_api_solr\Plugin\DataType\Deriver\SolrDocumentDeriver

Provides data type plugins for each index.

Hierarchy

Expanded class hierarchy of SolrDocumentDeriver

File

src/Plugin/DataType/Deriver/SolrDocumentDeriver.php, line 15

Namespace

Drupal\search_api_solr\Plugin\DataType\Deriver
View source
class SolrDocumentDeriver extends DeriverBase implements ContainerDeriverInterface {

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

  /**
   * The base plugin ID this derivative is for.
   *
   * @var string
   */
  protected $basePluginId;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * SolrDocumentDeriver constructor.
   *
   * @param string $base_plugin_id
   *   Base plugin ID.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   */
  public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager) {
    $this->basePluginId = $base_plugin_id;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   Thrown if the entity type doesn't exist.
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   *   Thrown if the storage handler couldn't be loaded.
   * @throws \Drupal\search_api\SearchApiException
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    // Also keep the 'solr_document' defined without any index.
    $this->derivatives[''] = $base_plugin_definition;

    // Load all indexes and filter out solr-based.
    $indexes = $this->entityTypeManager
      ->getStorage('search_api_index')
      ->loadMultiple();

    /* @var \Drupal\search_api\Entity\Index $entity */
    foreach ($indexes as $index_id => $entity) {
      $server = $entity
        ->getServerInstance();
      if ($server && $server
        ->getBackend() instanceof SolrBackendInterface && Utility::hasIndexSolrDatasources($entity)) {

        // @todo Does it make sense to get constraints from index entity?
        $this->derivatives[$index_id] = [
          'label' => $base_plugin_definition['label'] . ':' . $entity
            ->label(),
        ] + $base_plugin_definition;
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
SolrDocumentDeriver::$basePluginId protected property The base plugin ID this derivative is for.
SolrDocumentDeriver::$derivatives protected property List of derivative definitions. Overrides DeriverBase::$derivatives
SolrDocumentDeriver::$entityTypeManager protected property The entity type manager.
SolrDocumentDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
SolrDocumentDeriver::getDerivativeDefinitions public function Overrides DeriverBase::getDerivativeDefinitions
SolrDocumentDeriver::__construct public function SolrDocumentDeriver constructor.