diff --git a/composer.json b/composer.json index 142c73d1f..fb7d78663 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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": { diff --git a/modules/helfi_react_search/src/Plugin/search_api/processor/DistrictImageAbsoluteUrl.php b/modules/helfi_react_search/src/Plugin/search_api/processor/DistrictImageAbsoluteUrl.php index eb37f0bdd..d73698fc0 100644 --- a/modules/helfi_react_search/src/Plugin/search_api/processor/DistrictImageAbsoluteUrl.php +++ b/modules/helfi_react_search/src/Plugin/search_api/processor/DistrictImageAbsoluteUrl.php @@ -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'; } } diff --git a/modules/helfi_react_search/src/Plugin/search_api/processor/ProjectImageAbsoluteUrl.php b/modules/helfi_react_search/src/Plugin/search_api/processor/ProjectImageAbsoluteUrl.php index b063a72ad..bc949cdfd 100644 --- a/modules/helfi_react_search/src/Plugin/search_api/processor/ProjectImageAbsoluteUrl.php +++ b/modules/helfi_react_search/src/Plugin/search_api/processor/ProjectImageAbsoluteUrl.php @@ -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'; } } diff --git a/src/SearchAPI/Processor/MainImageProcessorProperties.php b/src/SearchAPI/Processor/MainImageProcessorProperties.php new file mode 100644 index 000000000..ea7a2029a --- /dev/null +++ b/src/SearchAPI/Processor/MainImageProcessorProperties.php @@ -0,0 +1,18 @@ +getFieldProperties(); + $properties[$fieldProperties->imageStyleField] = new ProcessorProperty([ + 'label' => $this->t('Main image: styles'), + 'description' => $this->t('Main image: an array of image styles'), + 'type' => 'string', + 'processor_id' => $this->getPluginId(), + ]); + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function addFieldValues(ItemInterface $item) { + $dataSourceId = $item->getDataSourceId(); + + if ($dataSourceId !== 'entity:node' || !$node = $item->getOriginalObject()->getValue()) { + return; + } + assert($node instanceof NodeInterface); + + if (!$this->isValid($node)) { + return; + } + $properties = $this->getFieldProperties(); + + $image = $node->get($properties->entityField)->entity; + + if (!$image || !$image->hasField('field_media_image') || !$file = $image->get('field_media_image')->entity) { + return; + } + assert($file instanceof FileInterface); + + $this->processFields($item, $file); + } + + /** + * Processes the image style field. + * + * @param \Drupal\search_api\Item\ItemInterface $item + * The item to process. + * @param \Drupal\file\FileInterface $file + * The file to process. + */ + protected function processFields(ItemInterface $item, FileInterface $file): void { + $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) { + if ($imageStyle = ImageStyle::load($styleName)) { + $urls[$breakpoint] = $imageStyle->buildUrl($file->getFileUri()); + } + } + $fields = $this + ->getFieldsHelper() + ->filterForPropertyPath($item->getFields(), 'entity:node', $this->getFieldProperties()->imageStyleField); + + foreach ($fields as $field) { + $field->addValue(json_encode($urls)); + } + } + +}