Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"drupal/diff": "^1.0",
"drupal/easy_breadcrumb": "^2.0",
"drupal/editoria11y": "^2.0",
"drupal/elasticsearch_connector": "^8.0@alpha",
"drupal/elasticsearch_connector": "^9.0@alpha",
"drupal/entity_browser": "^2.5",
"drupal/entity_usage": "^2.0@beta",
"drupal/external_entities": "^3.0",
Expand Down Expand Up @@ -64,10 +64,9 @@
"drupal/views_bulk_edit": "^3.0",
"drupal/views_bulk_operations": "^4.1",
"drupal/stage_file_proxy": "^3.1",
"ruflin/elastica": "^8.0",
"symfony/property-access": "^7.3",
"league/html-to-markdown": "^5.1",
"league/uri": "^6.0",
"league/uri": "^7.0",
"php": "^8.1"
},
"conflict": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,42 @@

namespace Drupal\helfi_react_search\Plugin\search_api\processor;

use Drupal\image\Entity\ImageStyle;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_platform_config\SearchAPI\Processor\MainImageUrlProcessorBase;
use Drupal\helfi_platform_config\SearchAPI\Processor\MainImageProcessorProperties;
use Drupal\node\NodeInterface;
use Drupal\search_api\Attribute\SearchApiProcessor;

/**
* Get start and end date for daterange field.
*
* @SearchApiProcessor(
* id = "district_image_absolute_url",
* label = @Translation("Image absolute URL"),
* description = @Translation("Generate absolute URL for image"),
* stages = {
* "add_properties" = 0,
* },
* locked = true,
* hidden = true,
* )
* Indexes District image uri in correct image style.
*/
class DistrictImageAbsoluteUrl extends ProcessorPluginBase {
#[SearchApiProcessor(
id: 'district_image_absolute_url',
label: new TranslatableMarkup('Image absolute URL'),
description: new TranslatableMarkup('Generate absolute URL for image'),
stages: [
'add_properties' => 0,
],
locked: TRUE,
hidden: TRUE,
)]
final class DistrictImageAbsoluteUrl extends MainImageUrlProcessorBase {

/**
* {@inheritdoc}
*/
public function getPropertyDefinitions(?DatasourceInterface $datasource = NULL) {
$properties = [];

if ($datasource) {
$definition = [
'label' => $this->t('District image absolute URL'),
'description' => $this->t('Generate absolute URL for image'),
'type' => 'string',
'processor_id' => $this->getPluginId(),
];
$properties['district_image_absolute_url'] = new ProcessorProperty($definition);
}

return $properties;
protected function getFieldProperties(): MainImageProcessorProperties {
return new MainImageProcessorProperties(
imageStyleField: 'district_image_absolute_url',
entityField: 'field_district_image',
);
}

/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
$datasourceId = $item->getDataSourceId();

if ($datasourceId !== 'entity:node' || !$node = $item->getOriginalObject()->getValue()) {
return;
}

$type = $node->getType();

if ($type !== 'district') {
return;
}

$image = $node->get('field_district_image')->entity;

if (!$image || !$image->hasField('field_media_image') || !$file = $image->get('field_media_image')->entity) {
return;
}

$imagePath = $file->getFileUri();
$imageStyles = [
'1.5_304w_203h' => '1248',
'1.5_294w_196h' => '992',
'1.5_220w_147h' => '768',
'1.5_176w_118h' => '576',
'1.5_511w_341h' => '320',
'1.5_608w_406w_LQ' => '1248_2x',
'1.5_588w_392h_LQ' => '992_2x',
'1.5_440w_294h_LQ' => '768_2x',
'1.5_352w_236h_LQ' => '576_2x',
'1.5_1022w_682h_LQ' => '320_2x',
];

$urls = [];
foreach ($imageStyles as $styleName => $breakpoint) {
$imageStyle = ImageStyle::load($styleName);
if ($imageStyle) {
$urls[$breakpoint] = $imageStyle->buildUrl($imagePath);
}
}

$fields = $this->getFieldsHelper()
->filterForPropertyPath($item->getFields(), 'entity:node', 'district_image_absolute_url');
foreach ($fields as $field) {
$field->addValue(json_encode($urls));
}
protected function isValid(NodeInterface $node): bool {
return $node->getType() === 'district';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,42 @@

namespace Drupal\helfi_react_search\Plugin\search_api\processor;

use Drupal\image\Entity\ImageStyle;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_platform_config\SearchAPI\Processor\MainImageProcessorProperties;
use Drupal\helfi_platform_config\SearchAPI\Processor\MainImageUrlProcessorBase;
use Drupal\node\NodeInterface;
use Drupal\search_api\Attribute\SearchApiProcessor;

/**
* Get start and end date for daterange field.
*
* @SearchApiProcessor(
* id = "project_image_absolute_url",
* label = @Translation("Image absolute URL"),
* description = @Translation("Generate absolute URL for image"),
* stages = {
* "add_properties" = 0,
* },
* locked = true,
* hidden = true,
* )
* Indexes main image uri in correct image style.
*/
class ProjectImageAbsoluteUrl extends ProcessorPluginBase {
#[SearchApiProcessor(
id: 'project_image_absolute_url',
label: new TranslatableMarkup('Image absolute URL'),
description: new TranslatableMarkup('Generate absolute URL for image'),
stages: [
'add_properties' => 0,
],
locked: TRUE,
hidden: TRUE,
)]
final class ProjectImageAbsoluteUrl extends MainImageUrlProcessorBase {

/**
* {@inheritdoc}
*/
public function getPropertyDefinitions(?DatasourceInterface $datasource = NULL) {
$properties = [];

if ($datasource) {
$definition = [
'label' => $this->t('Project image absolute URL'),
'description' => $this->t('Generate absolute URL for image'),
'type' => 'string',
'processor_id' => $this->getPluginId(),
];
$properties['project_image_absolute_url'] = new ProcessorProperty($definition);
}

return $properties;
protected function getFieldProperties(): MainImageProcessorProperties {
return new MainImageProcessorProperties(
imageStyleField: 'project_image_absolute_url',
entityField: 'field_project_image',
);
}

/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
$datasourceId = $item->getDataSourceId();

if ($datasourceId !== 'entity:node' || !$node = $item->getOriginalObject()->getValue()) {
return;
}

$type = $node->getType();

if ($type !== 'project') {
return;
}

$image = $node->get('field_project_image')->entity;

if (!$image || !$image->hasField('field_media_image') || !$file = $image->get('field_media_image')->entity) {
return;
}

$imagePath = $file->getFileUri();
$imageStyles = [
'1.5_304w_203h' => '1248',
'1.5_294w_196h' => '992',
'1.5_220w_147h' => '768',
'1.5_176w_118h' => '576',
'1.5_511w_341h' => '320',
'1.5_608w_406w_LQ' => '1248_2x',
'1.5_588w_392h_LQ' => '992_2x',
'1.5_440w_294h_LQ' => '768_2x',
'1.5_352w_236h_LQ' => '576_2x',
'1.5_1022w_682h_LQ' => '320_2x',
];

$urls = [];
foreach ($imageStyles as $styleName => $breakpoint) {
$imageStyle = ImageStyle::load($styleName);
if ($imageStyle) {
$urls[$breakpoint] = $imageStyle->buildUrl($imagePath);
}
}

$fields = $this->getFieldsHelper()
->filterForPropertyPath($item->getFields(), 'entity:node', 'project_image_absolute_url');
foreach ($fields as $field) {
$field->addValue(json_encode($urls));
}
protected function isValid(NodeInterface $node): bool {
return $node->getType() === 'project';
}

}
18 changes: 18 additions & 0 deletions src/SearchAPI/Processor/MainImageProcessorProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_platform_config\SearchAPI\Processor;

/**
* A DTO to represent 'main image' Search API processor properties.
*/
final readonly class MainImageProcessorProperties {

public function __construct(
public string $imageStyleField,
public string $entityField,
) {
}

}
Loading
Loading