Skip to content

Commit

Permalink
Merge pull request #5 from lsst-epo/3-add-element-status-event
Browse files Browse the repository at this point in the history
Added `element-event-status` event
  • Loading branch information
ericdrosas87 authored Oct 24, 2024
2 parents 78892c1 + 589f9f3 commit e510cf9
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.0 - 2024-10-21
### Added
- New custom event for detecting entry status changes
- New event listener for custom event for triggering revalidates
- New command for running check to look for entries that have changed their status

## 1.2.1 - 2024-10-22
### Changed
- Changed Craft::info() to Craft::warning() so it gets picked up in non-devMode
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lsst-epo/craft-next-js-builds",
"description": "Invoke Next.js page builds from Craft.",
"type": "craft-plugin",
"version": "1.2.4",
"version": "2.0.0",
"keywords": [
"craft",
"cms",
Expand Down
69 changes: 61 additions & 8 deletions src/NextBuilds.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
use craft\events\PluginEvent;
use lsst\nextbuilds\services\Request as NextRequestService;
use craft\services\Structures;
use lsst\nextbuilds\services\ElementStatusEvents;
use lsst\nextbuilds\events\StatusChangeEvent;
use yii\base\Event;
use benf\neo\elements\Block;

use yii\caching\CacheInterface;
use craft\console\Application as CraftConsoleApp;
use lsst\nextbuilds\commands\ScheduledElements;
use craft\services\Elements;
/**
* Class NextBuilds
*
Expand All @@ -35,6 +41,7 @@
*/
class NextBuilds extends Plugin
{

// Static Properties
// =========================================================================

Expand All @@ -56,6 +63,8 @@ class NextBuilds extends Plugin
*/
public string $schemaVersion = '1.0.0';

private string $homeUri = "__home__";

/**
* @var bool
*/
Expand All @@ -76,6 +85,32 @@ public function init()
{
parent::init();
self::$plugin = $this;
$this->initializeElementStatusEvents();

Event::on(
ElementStatusEvents::class,
ElementStatusEvents::EVENT_STATUS_CHANGED,
function(StatusChangeEvent $event) {
$newStatus = $event->element->getStatus();
$entry = $event->element;
if($entry instanceof \craft\elements\Entry &&
!$entry->resaving &&
$this->settings->activeSections[$entry->section->handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$newStatus == Entry::STATUS_LIVE) {

if($entry->type->handle == "callout") {
$this->request->buildPagesFromEntry($this->homeUri, false);
} else if ($entry->uri != null) {
$revalidateMenu = ($entry->type->handle == "pages");
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
}

}
}
);

Event::on(
Plugins::class,
Expand Down Expand Up @@ -111,11 +146,12 @@ function (ModelEvent $event) {
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
!$entry->resaving
!$entry->resaving &&
$entry->uri != null
) {
$revalidateMenu = ($entry->type->handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand All @@ -130,11 +166,12 @@ function (Event $event) {
$this->settings->activeSections[$entry->section->handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$entry->uri != null
) {
$revalidateMenu = ($entry->type->handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand All @@ -161,11 +198,12 @@ function (MoveElementEvent $event) {
$this->settings->activeSections[$handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$entry->uri != null
) {
$revalidateMenu = ($handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand All @@ -180,17 +218,32 @@ function (Event $event) {
$this->settings->activeSections[$entry->section->handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$entry->uri != null
) {
$revalidateMenu = ($entry->type->handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
);
}

/**
* @return void
*/
protected function initializeElementStatusEvents(): void
{
Event::on(Elements::class, Elements::EVENT_BEFORE_SAVE_ELEMENT, [ElementStatusEvents::class, 'rememberPreviousStatus']);
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, [ElementStatusEvents::class, 'fireEventOnChange']);

if (Craft::$app instanceof CraftConsoleApp) {
Craft::$container->set(CacheInterface::class, Craft::$app->getCache());
Craft::$app->controllerMap['element-status-events'] = ScheduledElements::class;
}
}

// Protected Methods
// =========================================================================

Expand Down
71 changes: 71 additions & 0 deletions src/behaviors/ElementStatusBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace lsst\nextbuilds\behaviors;

use Craft;
use craft\base\Element;
use lsst\nextbuilds\services\ElementStatusEvents;
use lsst\nextbuilds\events\StatusChangeEvent;
use lsst\nextbuilds\commands\ScheduledElements;
use yii\base\Behavior;
use yii\base\Event;
use yii\caching\CacheInterface;

/**
*
*/
class ElementStatusBehavior extends Behavior
{

/**
* @var string
*/
public $statusBeforeSave = '';
public $previousStatus = "";

/**
* Saves the status of an element before it is saved
*/
public function rememberPreviousStatus()
{
/** @var Element $element */
$element = $this->owner;

$originalElement = Craft::$app->getElements()->getElementById(
$element->id,
get_class($element),
$element->siteId
);

$this->statusBeforeSave = $originalElement === null ?: $originalElement->getStatus();
}

/**
* Triggers an event if the status has changed
*/
public function fireEventOnChange()
{
/** @var Element $element */
$element = $this->owner;

if ($this->statusBeforeSave === $element->getStatus()) {
return;
}

if (Craft::$app->getRequest()->getIsConsoleRequest()) {
Craft::$container->set(CacheInterface::class, Craft::$app->getCache());
Craft::$app->controllerMap['element-status-events'] = ScheduledElements::class;
}

if (Event::hasHandlers(ElementStatusEvents::class, ElementStatusEvents::EVENT_STATUS_CHANGED)) {
Event::trigger(
ElementStatusEvents::class,
ElementStatusEvents::EVENT_STATUS_CHANGED,
new StatusChangeEvent([
'element' => $element,
'statusBeforeSave' => $this->statusBeforeSave
])
);
}
}
}
Loading

0 comments on commit e510cf9

Please sign in to comment.