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

DDST-922: General model fallback #28

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions dgi_image_discovery.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ services:
class: '\Drupal\dgi_image_discovery\EventSubscriber\DiscoverRepresentativeImageSubscriber'
tags:
- name: event_subscriber
dgi_image_discovery.model_fallback_subscriber:
class: '\Drupal\dgi_image_discovery\EventSubscriber\ModelDefaultFallbackSubscriber'
tags:
- name: event_subscriber
dgi_image_discovery.deferred_resolution_controller:
class: '\Drupal\dgi_image_discovery\Controller\DeferredResolutionController'
factory: [null, 'create']
Expand Down
64 changes: 64 additions & 0 deletions src/EventSubscriber/ModelDefaultFallbackSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Drupal\dgi_image_discovery\EventSubscriber;

use Drupal\dgi_image_discovery\ImageDiscoveryEvent;
use Drupal\node\NodeInterface;

/**
* Model default fallback image subscriber.
*/
class ModelDefaultFallbackSubscriber extends AbstractImageDiscoverySubscriber {

public const PRIORITY = 500;

/**
* {@inheritDoc}
*/
public function discoverImage(ImageDiscoveryEvent $event) : void {
$entity = $event->getEntity();
if (!$entity instanceof NodeInterface) {
return;
}
if (!$entity->hasField('field_model')) {
return;
}
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $term_item */
foreach ($entity->get('field_model') as $term_item) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $term_item?->get('entity')?->getTarget()?->getEntity();
$term_access = $term?->access('view', NULL, TRUE);

if (!$term_access?->isAllowed()) {
continue;
}

if (!$term?->hasField('field_default_image')) {
continue;
}

/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $candidate_item */
foreach ($term->get('field_default_image') as $candidate_item) {

/** @var \Drupal\media\Entity\Media $candidate */
if (!$candidate = $candidate_item?->get('entity')?->getTarget()?->getEntity()) {
continue;
}

$candidate_access = $candidate?->access('view', NULL, TRUE);
if (!$candidate_access->isAllowed()) {
continue;
}

$event->addCacheableDependency($term_access)
->addCacheableDependency($candidate_access)
->addCacheableDependency($term)
->addCacheableDependency($candidate)
->setMedia($candidate)
->stopPropagation();
return;
}
}
}

}
2 changes: 1 addition & 1 deletion src/Plugin/Field/FieldType/DIDImageItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DIDImageItem extends EntityReferenceItem implements RefinableCacheableDepe
public function __construct(
DataDefinitionInterface $definition,
$name = NULL,
TypedDataInterface $parent = NULL,
?TypedDataInterface $parent = NULL,
) {
parent::__construct($definition, $name, $parent);

Expand Down
49 changes: 1 addition & 48 deletions src/Plugin/search_api/processor/DgiImageDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
use Drupal\dgi_image_discovery\ImageDiscoveryInterface;
use Drupal\dgi_image_discovery\Plugin\search_api\processor\Property\DgiImageDiscoveryProperty;
use Drupal\dgi_image_discovery\UrlGeneratorPluginManagerInterface;
use Drupal\file\Entity\File;
use Drupal\image\ImageStyleInterface;
use Drupal\media\Entity\Media;
use Drupal\node\NodeInterface;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -81,7 +77,7 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*/
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
public function getPropertyDefinitions(?DatasourceInterface $datasource = NULL) {
$properties = [];

if (!$datasource) {
Expand Down Expand Up @@ -135,51 +131,8 @@ public function addFieldValues(ItemInterface $item) {
if ($generated_url) {
$field->addValue($generated_url->getGeneratedUrl());
}
else {
// Fallback to default image if URL generation fails.
$default_image_url = $this->getDefaultImageFromTaxonomy($entity, $image_style);
if ($default_image_url) {
$field->addValue($default_image_url);
}
}
}
}
}

/**
* Gets the default image URL from the taxonomy term.
*
* @param \Drupal\node\NodeInterface $node
* The node to get the default image from.
* @param \Drupal\image\ImageStyleInterface $image_style
* The image style to use.
*
* @return string|null
* The default image URL or null if not found.
*/
protected function getDefaultImageFromTaxonomy(NodeInterface $node, ImageStyleInterface $image_style) {
if (!$node->hasField('field_model')) {
return NULL;
}

$model_terms = $node->get('field_model')->referencedEntities();

foreach ($model_terms as $term) {
if ($term instanceof Term) {
// Load the media entity referenced by the field_default_image.
$media = $term->get('field_default_image')->entity;
if ($media instanceof Media) {
// Load the file entity from the media entity.
$file = $media->get('field_media_image')->entity;
if ($file instanceof File) {
// Use the provided image style.
return $image_style->buildUrl($file->getFileUri());
}
}
}
}

return NULL;
}

}
Loading