Snippet.php in Views search snippet 2.x
File
src/Plugin/views/field/Snippet.php
View source
<?php
namespace Drupal\views_search_snippet\Plugin\views\field;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\views\Annotation\ViewsField;
use Drupal\views\Plugin\views\field\NumericField;
use Drupal\views\ResultRow;
class Snippet extends NumericField {
public function query() {
foreach ($this->view->filter as $handler) {
if (isset($handler->search_score) && $handler->relationship == $this->relationship) {
$this->searchFilterPresent = TRUE;
$this
->ensureMyTable();
$this->tableAlias = $handler->tableAlias;
$this->field_alias = $this->query
->addField($this->tableAlias, 'langcode', NULL, array());
return;
}
}
$this->options['exclude'] = TRUE;
}
public function render(ResultRow $values) {
if (isset($this->searchFilterPresent)) {
$node_renderer = \Drupal::entityManager()
->getViewBuilder('node');
$node = $this
->getEntity($values);
$build = $node_renderer
->view($node, 'search_result');
unset($build['#theme']);
$node_rendered = drupal_render($build);
$keys = $this->view
->getExposedInput()['keys'];
$output = $node_rendered
->__toString();
$snippet = search_excerpt($keys, $output);
return $snippet;
}
}
}
Classes
Name |
Description |
Snippet |
Field handler for search snippet. |