Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PER-10: Added new search index field for image discovery #20

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
125 changes: 125 additions & 0 deletions src/Plugin/search_api/processor/DgiImageDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?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)) {
$media_source = $media->getSource();
$file_id = $media_source->getSourceFieldValue($media);
$image = $this->entityTypeManager->getStorage('file')->load($file_id);
}

$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'];
if (!empty($image)) {
$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,53 @@
<?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();

// Get an array of image styles.
$image_styles_array = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();
$image_styles = [];

if (!empty($image_styles_array)) {
foreach ($image_styles_array as $id => $style) {
$image_styles[$id] = $style->label();
}
}

$form['image_style'] = [
'#type' => 'select',
'#options' => $image_styles,
'#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'] ?? $this->defaultConfiguration()['image_style'],
];

return $form;
}

}
Loading