Skip to content

Commit

Permalink
API Apply TopPage extensions by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored and Sabina Talipova committed Nov 15, 2022
1 parent e20b46f commit 7db1c03
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 241 deletions.
8 changes: 4 additions & 4 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ SilverStripe\CMS\Controllers\CMSMain:
extensions:
- DNADesign\Elemental\Extensions\ElementalCMSMainExtension

SilverStripe\CMS\Model\SiteTree:
extensions:
topPageSiteTreeExtension: DNADesign\Elemental\TopPage\SiteTreeExtension

SilverStripe\Versioned\VersionedGridFieldItemRequest:
extensions:
- 'DNADesign\Elemental\Extensions\GridFieldDetailFormItemRequestExtension'
Expand All @@ -35,7 +39,3 @@ Symbiote\GridFieldExtensions\GridFieldAddNewMultiClassHandler:
SilverStripe\Core\Injector\Injector:
SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search:
class: DNADesign\Elemental\Controllers\ElementSiteTreeFilterSearch
SilverStripe\Dev\State\SapphireTestState:
properties:
States:
topPageTestState: '%$DNADesign\Elemental\TopPage\TestState'
17 changes: 2 additions & 15 deletions docs/en/advanced_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,21 +380,8 @@ belongs to.

When blocks are owned by a single ElementArea (and therefore a single Page), we can store a reference to the page
directly on the block as a performance enhancement for this query. A set of extensions to manage this are provided out
of the box, and can be enabled by applying the following configuration:

```
DNADesign\Elemental\Models\BaseElement:
extensions:
topPageDataExtension: DNADesign\Elemental\TopPage\DataExtension
DNADesign\Elemental\Models\ElementalArea:
extensions:
topPageDataExtension: DNADesign\Elemental\TopPage\DataExtension
Page:
extensions:
topPageSiteTreeExtension: DNADesign\Elemental\TopPage\SiteTreeExtension
```
of the box for elements on pages. You can use `$TopPage` in your template to get access to the page, which is more performant
than using `$Page`.

These extensions will be enabled by default in Elemental 5.

Expand Down
4 changes: 3 additions & 1 deletion src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DNADesign\Elemental\Controllers\ElementController;
use DNADesign\Elemental\Forms\TextCheckboxGroupField;
use DNADesign\Elemental\ORM\FieldType\DBObjectType;
use DNADesign\Elemental\TopPage\DataExtension;
use Exception;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\CMS\Model\SiteTree;
Expand Down Expand Up @@ -78,7 +79,8 @@ class BaseElement extends DataObject implements CMSPreviewable
];

private static $extensions = [
Versioned::class
Versioned::class,
DataExtension::class,
];

private static $casting = [
Expand Down
2 changes: 2 additions & 0 deletions src/Models/ElementalArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DNADesign\Elemental\Models;

use DNADesign\Elemental\Extensions\ElementalAreasExtension;
use DNADesign\Elemental\TopPage\DataExtension;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\TestOnly;
Expand Down Expand Up @@ -35,6 +36,7 @@ class ElementalArea extends DataObject

private static $extensions = [
Versioned::class,
DataExtension::class,
];

private static $owns = [
Expand Down
59 changes: 0 additions & 59 deletions src/TopPage/DataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ class DataExtension extends BaseDataExtension
'TopPageID' => true,
];

/**
* Global flag which indicates if this feature is enabled or not
*
* @see DataExtension::withTopPageUpdate()
* @var bool
*/
private $topPageUpdate = true;

/**
* Global flag which indicates that automatic page determination is enabled or not
* If this is set to a page ID it will be used instead of trying to determine the top page
Expand Down Expand Up @@ -155,10 +147,6 @@ public function getTopPage(): ?Page
*/
public function setTopPage(?Page $page = null): void
{
if (!$this->getTopPageUpdate()) {
return;
}

/** @var BaseElement|ElementalArea|Versioned|DataExtension $owner */
$owner = $this->owner;

Expand Down Expand Up @@ -188,49 +176,6 @@ public function setTopPage(?Page $page = null): void
$this->saveChanges();
}

public function getTopPageUpdate(): bool
{
return $this->topPageUpdate;
}

/**
* Global flag manipulation - enable automatic top page determination
* Useful for unit tests as you may want to enable / disable this feature based on need
*/
public function enableTopPageUpdate(): void
{
$this->topPageUpdate = true;
}

/**
* Global flag manipulation - disable automatic top page determination
* Useful for unit tests as you may want to enable / disable this feature based on need
*/
public function disableTopPageUpdate(): void
{
$this->topPageUpdate = false;
}

/**
* Use this to wrap any code which is supposed to run with desired top page update setting
* Useful for unit tests as you may want to enable / disable this feature based on need
*
* @param bool $update
* @param callable $callback
* @return mixed
*/
public function withTopPageUpdate(bool $update, callable $callback)
{
$original = $this->topPageUpdate;
$this->topPageUpdate = $update;

try {
return $callback();
} finally {
$this->topPageUpdate = $original;
}
}

/**
* Use this to wrap any code which is supposed to run with fixed top page
* Useful when top page is known upfront and doesn't need to be determined
Expand Down Expand Up @@ -271,10 +216,6 @@ protected function getFixedTopPageID(): int
*/
protected function updateTopPage(): void
{
if (!$this->getTopPageUpdate()) {
return;
}

/** @var SiteTreeExtension $extension */
$extension = singleton(SiteTreeExtension::class);
$extension->addDuplicatedObject($this->owner);
Expand Down
1 change: 0 additions & 1 deletion src/TopPage/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* Class FluentExtension
*
* Use in place of @see DataExtension if you use the Fluent module for page localisation.
* Be sure to also update @see TestState to use this extension
*
* @link https://github.com/tractorcow-farm/silverstripe-fluent
* @property string $TopPageLocale
Expand Down
29 changes: 1 addition & 28 deletions src/TopPage/SiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ protected function getDuplicatedPageKey(): ?string
*/
protected function initDuplication(Page $original): void
{
/** @var DataExtension $extension */
$extension = singleton(DataExtension::class);

if (!$extension->getTopPageUpdate()) {
return;
}

$key = $original->getDuplicationKey();

if ($key === null) {
Expand All @@ -165,13 +158,6 @@ protected function initDuplication(Page $original): void
*/
protected function processDuplication(Page $original, bool $written): void
{
/** @var DataExtension $extension */
$extension = singleton(DataExtension::class);

if (!$extension->getTopPageUpdate()) {
return;
}

if ($written) {
$this->writeDuplication($original);

Expand All @@ -191,13 +177,7 @@ protected function processDuplication(Page $original, bool $written): void
*/
protected function processDuplicationFromOriginal(): void
{
/** @var DataExtension $extension */
$extension = singleton(DataExtension::class);

if (!$extension->getTopPageUpdate()) {
return;
}

/** @var Page|ElementalPageExtension $owner */
$owner = $this->owner;

if (!isset($owner->duplicationOriginal)) {
Expand Down Expand Up @@ -250,13 +230,6 @@ protected function writeDuplication(Page $original): void
*/
protected function setTopPageForElementalArea(): void
{
/** @var DataExtension $extension */
$extension = singleton(DataExtension::class);

if (!$extension->getTopPageUpdate()) {
return;
}

/** @var Page|ElementalPageExtension $owner */
$owner = $this->owner;

Expand Down
40 changes: 0 additions & 40 deletions src/TopPage/TestState.php

This file was deleted.

Loading

0 comments on commit 7db1c03

Please sign in to comment.