Skip to content

Commit

Permalink
Merge pull request #20 from axelerant-hardik/PER-10
Browse files Browse the repository at this point in the history
PER-10: Added new search index field for image discovery
  • Loading branch information
nchiasson-dgi authored Mar 6, 2024
2 parents bb3a011 + 7435720 commit ca4768f
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/schema/search_api.processor.schema.yml
Original file line number Diff line number Diff line change
@@ -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'
128 changes: 128 additions & 0 deletions src/Plugin/search_api/processor/DgiImageDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Drupal\dgi_image_discovery\Plugin\search_api\processor;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\dgi_image_discovery\ImageDiscoveryInterface;
use Drupal\dgi_image_discovery\Plugin\search_api\processor\Property\DgiImageDiscoveryProperty;
use Drupal\node\NodeInterface;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Get the styled image url for the islandora node.
*
* @SearchApiProcessor(
* id = "dgi_image_discovery",
* label = @Translation("DGI Image Discovery"),
* description = @Translation("Get the styled image url for the islandora node."),
* stages = {
* "add_properties" = 0,
* },
* locked = true,
* hidden = true,
* )
*/
class DgiImageDiscovery extends ProcessorPluginBase implements ContainerFactoryPluginInterface {

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The DGI Image Discovery service.
*
* @var \Drupal\dgi_image_discovery\ImageDiscoveryInterface
*/
protected $imageDiscovery;

/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ImageDiscoveryInterface $image_discovery,
EntityTypeManagerInterface $entity_type_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->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);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Drupal\dgi_image_discovery\Plugin\search_api\processor\Property;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\Processor\ConfigurablePropertyBase;

/**
* Defines a "DGI Image Discovery" property.
*/
class DgiImageDiscoveryProperty extends ConfigurablePropertyBase {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'image_style' => '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;
}

}

0 comments on commit ca4768f

Please sign in to comment.