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-213 : switch user to anonymous/configured role in DGI Image discovery #22

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 1 addition & 2 deletions dgi_image_discovery.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* General hook implementations.
*/

use Drupal\dgi_image_discovery\DIDImageItemList;

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\dgi_image_discovery\DIDImageItemList;

/**
* Implements hook_entity_base_field_info().
Expand Down
2 changes: 1 addition & 1 deletion src/DIDImageItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Drupal\dgi_image_discovery;

use Drupal\Core\TypedData\ComputedItemListTrait;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;

/**
* Boiler-plate for our computed field.
Expand Down
5 changes: 2 additions & 3 deletions src/EventSubscriber/DiscoverChildThumbnailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Drupal\dgi_image_discovery\EventSubscriber;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\dgi_image_discovery\ImageDiscoveryEvent;
use Drupal\dgi_image_discovery\ImageDiscoveryInterface;
use Drupal\node\NodeInterface;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
* Discovery child thumbnails.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/EventSubscriber/DiscoverOwnedThumbnailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Drupal\dgi_image_discovery\EventSubscriber;

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

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\dgi_image_discovery\ImageDiscoveryEvent;
use Drupal\node\NodeInterface;

/**
* Discover thumbnails which are owned by a given object.
Expand Down
3 changes: 1 addition & 2 deletions src/EventSubscriber/ViewsFieldSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Drupal\dgi_image_discovery\EventSubscriber;

use Drupal\search_api\Event\SearchApiEvents;
use Drupal\search_api\Event\MappingViewsFieldHandlersEvent;

use Drupal\search_api\Event\SearchApiEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand Down
4 changes: 1 addition & 3 deletions src/ImageDiscoveryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Drupal\dgi_image_discovery;

use Drupal\media\MediaInterface;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Entity\ContentEntityInterface;

use Drupal\media\MediaInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Plugin/Field/FieldType/DIDImageItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Drupal\dgi_image_discovery\Plugin\Field\FieldType;

use Drupal\dgi_image_discovery\ImageDiscoveryInterface;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\dgi_image_discovery\ImageDiscoveryInterface;

/**
* Find image media related to the given node.
Expand Down
26 changes: 24 additions & 2 deletions src/Plugin/search_api/processor/DgiImageDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\dgi_image_discovery\ImageDiscoveryInterface;
use Drupal\dgi_image_discovery\Plugin\search_api\processor\Property\DgiImageDiscoveryProperty;
use Drupal\node\NodeInterface;
Expand Down Expand Up @@ -42,6 +44,13 @@ class DgiImageDiscovery extends ProcessorPluginBase implements ContainerFactoryP
*/
protected $imageDiscovery;

/**
* The account switcher service.
*
* @var \Drupal\Core\Session\AccountSwitcherInterface
*/
protected $accountSwitcher;

/**
* {@inheritdoc}
*/
Expand All @@ -50,12 +59,14 @@ public function __construct(
$plugin_id,
$plugin_definition,
ImageDiscoveryInterface $image_discovery,
EntityTypeManagerInterface $entity_type_manager
EntityTypeManagerInterface $entity_type_manager,
AccountSwitcherInterface $account_switcher
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->imageDiscovery = $image_discovery;
$this->entityTypeManager = $entity_type_manager;
$this->accountSwitcher = $account_switcher;
}

/**
Expand All @@ -67,7 +78,8 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_id,
$plugin_definition,
$container->get('dgi_image_discovery.service'),
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('account_switcher')
);
}

Expand Down Expand Up @@ -95,6 +107,9 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
// Switch to anonymous user.
$this->accountSwitcher->switchTo(new AnonymousUserSession());

$entity = $item->getOriginalObject()->getValue();
$value = NULL;

Expand All @@ -103,13 +118,17 @@ public function addFieldValues(ItemInterface $item) {
$event = $this->imageDiscovery->getImage($entity);
$media = $event->getMedia();
if (empty($media)) {
// Restore the original user before returning.
$this->accountSwitcher->switchBack();
return;
}

$media_source = $media->getSource();
$file_id = $media_source->getSourceFieldValue($media);
$image = $this->entityTypeManager->getStorage('file')->load($file_id);
if (empty($image)) {
// Restore the original user before returning.
$this->accountSwitcher->switchBack();
return;
}

Expand All @@ -123,6 +142,9 @@ public function addFieldValues(ItemInterface $item) {
$field->addValue($value);
}
}

// Restore the original user.
$this->accountSwitcher->switchBack();
}

}