Skip to content

Commit

Permalink
PER-10: Addressed PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
axelerant-hardik committed Feb 22, 2024
1 parent 227fa2d commit 531e01b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
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 Drupal\search_api\Processor\ProcessorProperty;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Get the styled image url for the islandora node.
*
* @SearchApiProcessor(
* id = "islandora_object_image_discovery",
* label = @Translation("Islandora Object Image Discovery"),
* id = "dgi_image_discovery",
* label = @Translation("DGI Image Discovery"),
* description = @Translation("Get the styled image url for the islandora node."),
* stages = {
* "add_properties" = 0,
Expand All @@ -26,7 +26,7 @@
* hidden = true,
* )
*/
class IslandoraObjectImageDiscovery extends ProcessorPluginBase implements ContainerFactoryPluginInterface {
class DgiImageDiscovery extends ProcessorPluginBase implements ContainerFactoryPluginInterface {

/**
* The entity type manager service.
Expand Down Expand Up @@ -79,13 +79,13 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {

if (!$datasource) {
$definition = [
'label' => $this->t('Islandora Object Image Discovery'),
'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['islandora_object_image_discovery'] = new ProcessorProperty($definition);
$properties['dgi_image_discovery'] = new DgiImageDiscoveryProperty($definition);
}

return $properties;
Expand All @@ -101,20 +101,25 @@ public function addFieldValues(ItemInterface $item) {
// Get the image discovery URL.
if (!$entity->isNew() && $entity instanceof NodeInterface) {
$event = $this->imageDiscovery->getImage($entity);
if ($event->hasMedia()) {
$image = $event->getMedia()->field_media_image;
$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('solr_grid_thumbnail')
->buildUrl($image->entity->getFileUri());
$value = $this->entityTypeManager->getStorage('image_style')->load($image_style)
->buildUrl($image->getFileUri());
}
$field->addValue($value);
}
}

$fields = $item->getFields(FALSE);
$fields = $this->getFieldsHelper()->filterForPropertyPath($fields, NULL, 'islandora_object_image_discovery');
foreach ($fields as $field) {
$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;
}

}

0 comments on commit 531e01b

Please sign in to comment.