Created
February 7, 2019 14:03
-
-
Save DuaelFr/90aed6d0e956b2351f6cd6327f2d4a61 to your computer and use it in GitHub Desktop.
Drupal 8 Search API processor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\hc_core\Plugin\search_api\processor; | |
use Drupal\node\Entity\Node; | |
use Drupal\search_api\Datasource\DatasourceInterface; | |
use Drupal\search_api\IndexInterface; | |
use Drupal\search_api\Item\ItemInterface; | |
use Drupal\search_api\Processor\ProcessorPluginBase; | |
use Drupal\search_api\Processor\ProcessorProperty; | |
/** | |
* Indexes only the last date of a reference. | |
* | |
* @SearchApiProcessor( | |
* id = "hc_reference_last_date", | |
* label = @Translation("Reference last date"), | |
* description = @Translation("Only gets the last date of the content"), | |
* stages = { | |
* "add_properties" = 0, | |
* }, | |
* locked = true, | |
* hidden = false, | |
* ) | |
*/ | |
class ReferenceLastDate extends ProcessorPluginBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function supportsIndex(IndexInterface $index) { | |
foreach ($index->getDatasources() as $datasource) { | |
$allowed = ['reference']; | |
if ($datasource->getEntityTypeId() == 'node' && !empty(array_intersect(array_keys($datasource->getBundles()), $allowed))) { | |
return TRUE; | |
} | |
} | |
return FALSE; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { | |
$properties = array(); | |
if (!$datasource) { | |
$definition = array( | |
'label' => $this->t('Reference last date'), | |
'description' => $this->t('The last date of the reference'), | |
'type' => 'date', | |
'processor_id' => $this->getPluginId(), | |
); | |
$properties['search_api_hc_reference_last_date'] = new ProcessorProperty($definition); | |
} | |
return $properties; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function addFieldValues(ItemInterface $item) { | |
/** @var Node $node */ | |
$node = $item->getOriginalObject()->getValue(); | |
// Default value in case there is no timeline. | |
$value = $node->getCreatedTime(); | |
// Loop through paragraphs to find a timeline. | |
foreach ($node->field_description as $description_item) { | |
$paragraph = $description_item->entity; | |
if ($paragraph->bundle() === 'timeline') { | |
$delta = $paragraph->field_timeline_dates->count() - 1; | |
$value = $paragraph->field_timeline_dates->get($delta)->entity->field_date->value; | |
break; | |
} | |
} | |
$fields = $this->getFieldsHelper() | |
->filterForPropertyPath($item->getFields(), NULL, 'search_api_hc_reference_last_date'); | |
foreach ($fields as $field) { | |
if (!$field->getDatasourceId()) { | |
$field->addValue($value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment