Skip to content

Commit

Permalink
Merge branch 'City-of-Helsinki:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-dumi-ibm authored Feb 7, 2023
2 parents 0b7865a + 9705f55 commit 468754e
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 0 deletions.
17 changes: 17 additions & 0 deletions helfi_features/helfi_react_search/helfi_react_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ function helfi_react_search_event_list_allowed_values_function() : array {
5 => 5,
];
}

function helfi_react_search_preprocess_paragraph(array &$variables) {
$reactParagraphs = [
'school_search'
];

$type = $variables['paragraph']->getType();

$config = \Drupal::config('elastic_proxy.settings');
if (
isset($variables['paragraph']) &&
in_array($variables['paragraph']->getType(), $reactParagraphs) &&
$proxyUrl = $config->get('elastic_proxy_url')
) {
$variables['#attached']['drupalSettings']['helfi_react_search']['elastic_proxy_url'] = $proxyUrl;
}
}
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;
}

}
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']);
}

}
3 changes: 3 additions & 0 deletions modules/helfi_test_content/helfi_test_content.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ core_version_requirement: '^8 || ^9'
package: HELfi
dependencies:
- default_content:default_content
- helfi_charts:helfi_charts
- helfi_content:helfi_content
- helfi_media:helfi_media
- helfi_media_map_config:helfi_media_map_config
- helfi_tpr:helfi_tpr
default_content:
menu_link_content:
Expand Down

0 comments on commit 468754e

Please sign in to comment.