diff --git a/config/schema/search_api.processor.schema.yml b/config/schema/search_api.processor.schema.yml new file mode 100644 index 0000000..2a07257 --- /dev/null +++ b/config/schema/search_api.processor.schema.yml @@ -0,0 +1,7 @@ +search_api.property_configuration.dgi_image_discovery: + type: mapping + label: DGI Image Discovery + mapping: + image_style: + type: string + label: 'The image style to be used to create image url' diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php new file mode 100644 index 0000000..b0674f8 --- /dev/null +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -0,0 +1,128 @@ +imageDiscovery = $image_discovery; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('dgi_image_discovery.service'), + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { + $properties = []; + + if (!$datasource) { + $definition = [ + 'label' => $this->t('DGI Image Discovery'), + 'description' => $this->t('Styled Image Url which can then be passed to the image src.'), + 'type' => 'string', + 'is_list' => FALSE, + 'processor_id' => $this->getPluginId(), + ]; + $properties['dgi_image_discovery'] = new DgiImageDiscoveryProperty($definition); + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function addFieldValues(ItemInterface $item) { + $entity = $item->getOriginalObject()->getValue(); + $value = NULL; + + // Get the image discovery URL. + if (!$entity->isNew() && $entity instanceof NodeInterface) { + $event = $this->imageDiscovery->getImage($entity); + $media = $event->getMedia(); + if (empty($media)) { + return; + } + + $media_source = $media->getSource(); + $file_id = $media_source->getSourceFieldValue($media); + $image = $this->entityTypeManager->getStorage('file')->load($file_id); + if (empty($image)) { + return; + } + + $fields = $item->getFields(FALSE); + $fields = $this->getFieldsHelper()->filterForPropertyPath($fields, NULL, 'dgi_image_discovery'); + foreach ($fields as $field) { + $config = $field->getConfiguration(); + $image_style = $config['image_style']; + $value = $this->entityTypeManager->getStorage('image_style')->load($image_style) + ->buildUrl($image->getFileUri()); + $field->addValue($value); + } + } + } + +} diff --git a/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php b/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php new file mode 100644 index 0000000..3a22b30 --- /dev/null +++ b/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php @@ -0,0 +1,43 @@ + 'solr_grid_thumbnail', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) { + $configuration = $field->getConfiguration() + $this->defaultConfiguration(); + + $form['image_style'] = [ + '#type' => 'select', + '#options' => image_style_options(FALSE), + '#title' => $this->t('Image Style'), + '#description' => $this->t('Select the image style that should be applied to derive the DGI Image Discovery image url.'), + '#default_value' => $configuration['image_style'], + ]; + + return $form; + } + +}