Skip to content

Commit

Permalink
Fix preview targets issue on certain routes (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored May 28, 2024
1 parent 40d2ec6 commit 1faa51d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Subscribers/SocialImagesGeneratorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use Aerni\AdvancedSeo\Jobs\DeleteSocialImagesJob;
use Aerni\AdvancedSeo\Jobs\GenerateSocialImagesJob;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Str;
use Statamic\Events;
use Statamic\Events\Event;
use Statamic\Facades\CP\Toast;
use Statamic\Statamic;

class SocialImagesGeneratorSubscriber
{
Expand Down Expand Up @@ -50,13 +52,26 @@ public function generateSocialImages(Event $event): void

public function addPreviewTargets(Event $event): void
{
$data = $this->getDataFromEvent($event);

if (! SocialImagesGenerator::enabled($data)) {
if (! $this->shouldAddPreviewTargets($event)) {
return;
}

// TODO: This has to change when implementing for taxonomies.
$this->getProperty($event)?->collection()->addPreviewTargets(SocialImage::previewTargets($event->entry));
}

protected function shouldAddPreviewTargets(Event $event): bool
{
// Only add preview targets in the CP.
if (! Statamic::isCpRoute()) {
return false;
}

// Only add preview targets when editing an existing entry.
if (! Str::containsAll(request()->path(), ['collections', $event->entry?->id()])) {
return false;
}

// Only add preview targets when the generator is enabled.
return SocialImagesGenerator::enabled($this->getDataFromEvent($event));
}
}

0 comments on commit 1faa51d

Please sign in to comment.