-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'City-of-Helsinki:main' into main
- Loading branch information
Showing
4 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
90 changes: 90 additions & 0 deletions
90
helfi_features/helfi_react_search/src/Plugin/search_api/processor/IsSchool.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\helfi_react_search\Plugin\search_api\processor; | ||
|
||
use Drupal\search_api\Processor\ProcessorPluginBase; | ||
use Drupal\search_api\IndexInterface; | ||
use Drupal\search_api\Item\ItemInterface; | ||
|
||
/** | ||
* Checks if given TPR entity is a school. | ||
* | ||
* @SearchApiProcessor( | ||
* id = "is_school", | ||
* label = @Translation("School filter"), | ||
* description = @Translation("Exclude non-school entities from index"), | ||
* stages = { | ||
* "alter_items" = 0, | ||
* } | ||
* ) | ||
*/ | ||
class IsSchool extends ProcessorPluginBase { | ||
|
||
/** | ||
* Checks the entity against these to determine if it should index. | ||
* | ||
* @var array | ||
*/ | ||
const SCHOOL_SERVICE_IDS = [ | ||
'3105', | ||
'3106', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function supportsIndex(IndexInterface $index): bool { | ||
foreach ($index->getDatasources() as $datasource) { | ||
$entityTypeId = $datasource->getEntityTypeId(); | ||
if (!$entityTypeId) { | ||
continue; | ||
} | ||
|
||
if ($entityTypeId === 'tpr_unit') { | ||
return TRUE; | ||
} | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function alterIndexedItems(array &$items): void { | ||
foreach ($items as $id => $item) { | ||
$shouldIndex = $this->shouldIndex($item); | ||
|
||
if (!$shouldIndex) { | ||
unset($items[$id]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Determine if entity should be indexed. | ||
* | ||
* @param \Drupal\search_api\Item\ItemInterface $item | ||
* Item to check. | ||
* | ||
* @return bool | ||
* The result. | ||
*/ | ||
protected function shouldIndex(ItemInterface $item): bool { | ||
$object = $item->getOriginalObject()->getValue(); | ||
$fieldValues = $object->get('services')->getValue(); | ||
|
||
$flatValues = array_map(function (array $fieldValue) { | ||
return $fieldValue['target_id']; | ||
}, $fieldValues); | ||
|
||
if (count(array_intersect($flatValues, self::SCHOOL_SERVICE_IDS))) { | ||
return TRUE; | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
||
} |
145 changes: 145 additions & 0 deletions
145
helfi_features/helfi_react_search/src/Plugin/search_api/processor/MediaReferenceToObject.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\helfi_react_search\Plugin\search_api\processor; | ||
|
||
use Drupal\Core\Field\BaseFieldDefinition; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Plugin\PluginFormInterface; | ||
use Drupal\image\Entity\ImageStyle; | ||
use Drupal\search_api\Datasource\DatasourceInterface; | ||
use Drupal\search_api\Item\ItemInterface; | ||
use Drupal\search_api\Plugin\PluginFormTrait; | ||
use Drupal\search_api\Processor\ProcessorPluginBase; | ||
use Drupal\search_api\Processor\ProcessorProperty; | ||
|
||
/** | ||
* Converts media reference fields to objects in index. | ||
* | ||
* @SearchApiProcessor( | ||
* id = "media_reference_to_object", | ||
* label = @Translation("Convert media reference to object"), | ||
* description = @Translation("Converts media reference fields to objects in index"), | ||
* stages = { | ||
* "add_properties" = 0, | ||
* } | ||
* ) | ||
*/ | ||
class MediaReferenceToObject extends ProcessorPluginBase implements PluginFormInterface { | ||
|
||
use PluginFormTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPropertyDefinitions(DataSourceInterface $datasource = NULL): array { | ||
$properties = []; | ||
|
||
if (!$datasource) { | ||
$definition = [ | ||
'label' => $this->t('Media objects'), | ||
'description' => $this->t('Media entities as objects. Define desired media reference fields from processor settings.'), | ||
'type' => 'object', | ||
'processor_id' => $this->getPluginId(), | ||
]; | ||
|
||
$properties['media_as_objects'] = new ProcessorProperty($definition); | ||
} | ||
|
||
return $properties; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildConfigurationForm(array $form, FormStateInterface $formState): array { | ||
$form['#description'] = $this->t('Select fields to apply the filter to.'); | ||
|
||
$datasources = $this->index->getDatasources(); | ||
$fieldDefs = []; | ||
|
||
foreach ($datasources as $datasource) { | ||
$entityTypeId = $datasource->getEntityTypeId(); | ||
$bundles = $datasource->getBundles(); | ||
$entityFieldManager = $datasource->getEntityFieldManager(); | ||
|
||
$fieldDefs = array_map(function ($bundle) use ($entityFieldManager, $entityTypeId) { | ||
return $entityFieldManager->getFieldDefinitions($entityTypeId, $bundle); | ||
}, array_keys($bundles)); | ||
} | ||
|
||
foreach (array_merge(...$fieldDefs) as $def) { | ||
if (!$def instanceof BaseFieldDefinition || $def->getType() !== 'entity_reference') { | ||
continue; | ||
} | ||
|
||
$targetDef = $def->getPropertyDefinition('entity')->getTargetDefinition()->getEntityTypeId(); | ||
|
||
if ($targetDef !== 'media') { | ||
continue; | ||
} | ||
|
||
$enabled = !empty($this->configuration['fields'][$def->getName()]); | ||
$form['fields'][$def->getName()] = [ | ||
'#type' => 'checkbox', | ||
'#title' => $def->getLabel()->render(), | ||
'#default_value' => $enabled, | ||
]; | ||
} | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFieldValues(ItemInterface $item): void { | ||
$fields = $this->getEnabledFields(); | ||
$object = $item->getOriginalObject()->getValue(); | ||
|
||
foreach ($fields as $key => $field) { | ||
$media = $object->get($key)->entity; | ||
|
||
if ( | ||
!$media || | ||
!($image = $media->get('field_media_image')) || | ||
!($file = $image->entity) | ||
) { | ||
continue; | ||
} | ||
|
||
$imageStyle = ImageStyle::load('3_2_l'); | ||
$imagePath = $file->getFileUri(); | ||
$imageUri = $imageStyle->buildUri($imagePath); | ||
|
||
if (!file_exists($imageUri)) { | ||
$imageStyle->createDerivative($imagePath, $imageUri); | ||
} | ||
|
||
$url = $imageStyle->buildUrl($imagePath); | ||
|
||
$values = [ | ||
'alt' => $image->alt, | ||
'photographer' => $media->get('field_photographer')->value, | ||
'title' => $image->title, | ||
'url' => $url, | ||
]; | ||
|
||
$itemFields = $item->getFields(); | ||
$itemFields = $this->getFieldsHelper() | ||
->filterForPropertyPath($itemFields, NULL, 'media_as_objects'); | ||
foreach ($itemFields as $itemField) { | ||
$itemField->addValue([$key => $values]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return enabled fields. | ||
*/ | ||
protected function getEnabledFields() { | ||
return array_filter($this->configuration['fields']); | ||
} | ||
|
||
} |
This file contains 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