DisplayPluginBase.php in Search API 8
File
src/Display/DisplayPluginBase.php
View source
<?php
namespace Drupal\search_api\Display;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Url;
use Drupal\search_api\Plugin\HideablePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DisplayPluginBase extends HideablePluginBase implements DisplayInterface {
protected $currentPath;
protected $entityTypeManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$display = new static($configuration, $plugin_id, $plugin_definition);
$display
->setCurrentPath($container
->get('path.current'));
$display
->setEntityTypeManager($container
->get('entity_type.manager'));
return $display;
}
public function getCurrentPath() {
return $this->currentPath ?: \Drupal::service('path.current');
}
public function setCurrentPath(CurrentPathStack $current_path) {
$this->currentPath = $current_path;
return $this;
}
public function getEntityTypeManager() {
return $this->entityTypeManager ?: \Drupal::service('entity_type.manager');
}
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
return $this;
}
public function label() {
$plugin_definition = $this
->getPluginDefinition();
return $plugin_definition['label'];
}
public function getDescription() {
$plugin_definition = $this
->getPluginDefinition();
return $plugin_definition['description'];
}
public function getIndex() {
$plugin_definition = $this
->getPluginDefinition();
return $this
->getEntityTypeManager()
->getStorage('search_api_index')
->load($plugin_definition['index']);
}
public function getUrl() {
@trigger_error('\\Drupal\\search_api\\Display\\DisplayInterface::getUrl() is deprecated in Search API 8.x-1.0 Beta 5. Use ::getPath() instead. See https://www.drupal.org/node/2856050', E_USER_DEPRECATED);
if ($path = $this
->getPath()) {
return Url::fromUserInput($path);
}
return NULL;
}
public function getPath() {
$plugin_definition = $this
->getPluginDefinition();
if (!empty($plugin_definition['path'])) {
return $plugin_definition['path'];
}
return NULL;
}
public function isRenderedInCurrentRequest() {
if ($path = $this
->getPath()) {
$current_path = $this
->getCurrentPath()
->getPath();
return $current_path == $path;
}
return FALSE;
}
public function calculateDependencies() {
$dependencies = [];
$definition = $this
->getPluginDefinition();
$dependencies['module'][] = $definition['provider'];
$index = $this
->getIndex();
$dependencies[$index
->getConfigDependencyKey()][] = $index
->getConfigDependencyName();
return $dependencies;
}
}