class DataExtension extends DataExtension (View source)

Class DataExtension

Provides a db-cached reference to the top-level page for improved read performance on projects with deeply nested block structures. Apply to BaseElement and ElementalArea.

Properties

protected T $owner

The object this extension is applied to.

from  Extension
int $TopPageID

Methods

public
__construct()

No description

public static 
add_to_class(string $class, string $extensionClass, mixed $args = null)

Called when this extension is added to a particular class

public
setOwner(object $owner)

Set the owner of this extension.

public
mixed
withOwner(mixed $owner, callable $callback, array $args = [])

Temporarily modify the owner. The original owner is ensured to be restored

public
clearOwner()

Clear the current owner, and restore extension to the state prior to the last setOwner()

public
T
getOwner()

Returns the owner of this extension.

public static 
string
get_classname_without_arguments(string $extensionStr)

Helper method to strip eval'ed arguments from a string that's passed to DataObject::$extensions or Object::add_extension().

public
mixed
invokeExtension(object $owner, string $method, array ...$arguments)

Invoke extension point. This will prefer explicit extend prefixed methods.

public
validate(ValidationResult $validationResult)

Hook for extension-specific validation.

public
augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)

Edit the given query object to support queries for this extension

public
augmentDatabase()

Update the database schema as required by this extension.

public
augmentWrite(array $manipulation)

Augment a write-record request.

public
onBeforeWrite()

Extend the owner's onBeforeWrite() logic

public
onAfterWrite()

Extension point in DataObject::onAfterWrite()

public
onBeforeDelete()

Extend the owner's onBeforeDelete() logic

public
onAfterDelete()

Extend the owner's onAfterDelete() logic

public
requireDefaultRecords()

Extend the owner's requireDefaultRecords() logic

public
populateDefaults()

Extend the owner's populateDefaults() logic

public
onAfterBuild()

Extend the owner's onAfterBuild() logic

public
bool|null
can(Member $member)

Influence the owner's can() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

public
bool|null
canEdit(Member $member)

Influence the owner's canEdit() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

public
bool|null
canDelete(Member $member)

Influence the owner's canDelete() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

public
bool|null
canCreate(Member $member)

Influence the owner's canCreate() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

public
array
extraStatics(string $class = null, string $extension = null)

Define extra database fields

public
updateCMSFields(FieldList $fields)

This function is used to provide modifications to the form in the CMS by the extension. By default, no changes are made. DataObject->getCMSFields().

public
void
updateCMSCompositeValidator(CompositeValidator $compositeValidator)

This function is used to provide modifications to the Validators used on a DataObject.

public
updateFrontEndFields(FieldList $fields)

This function is used to provide modifications to the form used for front end forms. DataObject->getFrontEndFields()

public
updateCMSActions(FieldList $actions)

This is used to provide modifications to the form actions used in the CMS. DataObject->getCMSActions().

public
updateSummaryFields(array $fields)

this function is used to provide modifications to the summary fields in CMS by the extension By default, the summaryField() of its owner will merge more fields defined in the extension's $extra_fields['summary_fields']

public
updateFieldLabels(array $labels)

this function is used to provide modifications to the fields labels in CMS by the extension By default, the fieldLabels() of its owner will merge more fields defined in the extension's $extra_fields['field_labels']

public
void
onBeforeDuplicate()

Extension point in DataObject::duplicate()

public
void
onAfterDuplicate()

Extension point in DataObject::duplicate()

public
SiteTree|null
getTopPage()

Finds the top-level SiteTree object for a Block / ElementalArea, using the cached TopPageID reference when possible.

public
void
setTopPage(SiteTree|null $page = null)

Set top page to an object If no page is provided as an argument nor as a fixed id via DataExtension::withFixedTopPage() automatic page determination will be attempted Note that this may not always succeed as your model may not be attached to parent object at the time of this call

public
mixed
withFixedTopPage(int $topPageID, callable $callback)

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 For example: model duplication where parent is assigned and saved only after the duplication is done It's not possible to determine top page in such case however it might be possible to know the top page even before the operation starts from the specific context Setting the page id to 0 disables this feature

protected
int
getFixedTopPageID()

Get the ID of a page which is currently set as the fixed top page

protected
void
updateTopPage()

Registers the object for a TopPage update. Ensures that this operation is deferred to a point when all required relations have been written.

protected
void
assignTopPage(SiteTree $page)

Assigns top page relation

protected
void
clearTopPage()

Clears top page relation, this is useful when duplicating object as the new object doesn't necessarily belong to the original page

protected
void
assignFixedTopPage()

Assigns top page relation based on fixed id

protected
void
saveChanges(array $extraData = [])

Save top page changes without using write() Using raw query here because:

  • this is already called during write() and triggering more write() related extension points is undesirable
  • we don't want to create a new version if object is versioned
  • using writeWithoutVersion() produces some weird edge cases were data is not written because the fields are not recognised as changed (using forceChange() introduces a new set of issues)

protected
SiteTree|null
getTopPageFromCachedData(int $id)

Perform a page lookup based on cached data This function allows more extensibility as it can be fully overridden unlike an extension point Various projects may decide to alter this by injecting features like tracking, feature flags and even completely different data lookups This is a performance driven functionality so extension points are not great as they only allow adding features on top of existing ones not replacing them

protected
string
getTopPageTable()

Find table name which has the top page fields

TopPage()

No description

Details

__construct()

No description

static add_to_class(string $class, string $extensionClass, mixed $args = null)

Called when this extension is added to a particular class

Parameters

string $class
string $extensionClass
mixed $args

setOwner(object $owner)

Set the owner of this extension.

Parameters

object $owner

The owner object

mixed withOwner(mixed $owner, callable $callback, array $args = [])

Temporarily modify the owner. The original owner is ensured to be restored

Parameters

mixed $owner

Owner to set

callable $callback

Callback to invoke

array $args

Args to pass to callback

Return Value

mixed

clearOwner()

Clear the current owner, and restore extension to the state prior to the last setOwner()

T getOwner()

Returns the owner of this extension.

Return Value

T

static string get_classname_without_arguments(string $extensionStr)

Helper method to strip eval'ed arguments from a string that's passed to DataObject::$extensions or Object::add_extension().

Parameters

string $extensionStr

E.g. "Versioned('Stage','Live')"

Return Value

string

Extension classname, e.g. "Versioned"

mixed invokeExtension(object $owner, string $method, array ...$arguments)

Invoke extension point. This will prefer explicit extend prefixed methods.

Parameters

object $owner
string $method
array ...$arguments

Return Value

mixed

validate(ValidationResult $validationResult)

Hook for extension-specific validation.

Parameters

ValidationResult $validationResult

Local validation result

Exceptions

ValidationException

augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)

Edit the given query object to support queries for this extension

Parameters

SQLSelect $query

Query to augment.

DataQuery $dataQuery

Container DataQuery for this SQLSelect

augmentDatabase()

Update the database schema as required by this extension.

When duplicating a table's structure, remember to duplicate the create options as well. See Versioned->augmentDatabase for an example.

augmentWrite(array $manipulation)

Augment a write-record request.

Parameters

array $manipulation

Array of operations to augment.

onBeforeWrite()

Extend the owner's onBeforeWrite() logic

See DataObject::onBeforeWrite() for context.

onAfterWrite()

Extension point in DataObject::onAfterWrite()

Exceptions

ValidationException

onBeforeDelete()

Extend the owner's onBeforeDelete() logic

See DataObject::onBeforeDelete() for context.

onAfterDelete()

Extend the owner's onAfterDelete() logic

See DataObject::onAfterDelete() for context.

requireDefaultRecords()

Extend the owner's requireDefaultRecords() logic

See DataObject::requireDefaultRecords() for context.

populateDefaults()

Extend the owner's populateDefaults() logic

See DataObject::populateDefaults() for context.

onAfterBuild()

Extend the owner's onAfterBuild() logic

See DataObject::onAfterBuild() for context.

bool|null can(Member $member)

Influence the owner's can() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

See DataObject::can()} and {@link DataObject::extendedCan() for context.

Parameters

Member $member

Return Value

bool|null

bool|null canEdit(Member $member)

Influence the owner's canEdit() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

See DataObject::canEdit()} and {@link DataObject::extendedCan() for context.

Parameters

Member $member

Return Value

bool|null

bool|null canDelete(Member $member)

Influence the owner's canDelete() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

See DataObject::canDelete()} and {@link DataObject::extendedCan() for context.

Parameters

Member $member

Return Value

bool|null

bool|null canCreate(Member $member)

Influence the owner's canCreate() permission check value to be disallowed (false), allowed (true) if no other processed results are to disallow, or open (null) to not affect the outcome.

See DataObject::canCreate()} and {@link DataObject::extendedCan() for context.

Parameters

Member $member

Return Value

bool|null

array extraStatics(string $class = null, string $extension = null)

Define extra database fields

Return a map where the keys are db, has_one, etc, and the values are additional fields/relations to be defined.

Parameters

string $class

since this method might be called on the class directly

string $extension

since this can help to extract parameters to help set indexes

Return Value

array

Returns a map where the keys are db, has_one, etc, and the values are additional fields/relations to be defined.

updateCMSFields(FieldList $fields)

This function is used to provide modifications to the form in the CMS by the extension. By default, no changes are made. DataObject->getCMSFields().

Please consider using updateFormFields() to globally add formfields to the record. The method updateCMSFields() should just be used to add or modify tabs, or fields which are specific to the CMS-context.

Caution: Use FieldList->addFieldToTab() to add fields.

Parameters

FieldList $fields

FieldList with a contained TabSet

void updateCMSCompositeValidator(CompositeValidator $compositeValidator)

This function is used to provide modifications to the Validators used on a DataObject.

Caution: Use CompositeValidator->addValidator() to add Validators.

Parameters

CompositeValidator $compositeValidator

Return Value

void

updateFrontEndFields(FieldList $fields)

This function is used to provide modifications to the form used for front end forms. DataObject->getFrontEndFields()

Caution: Use FieldList->push() to add fields.

Parameters

FieldList $fields

FieldList without TabSet nesting

updateCMSActions(FieldList $actions)

This is used to provide modifications to the form actions used in the CMS. DataObject->getCMSActions().

Parameters

FieldList $actions FieldList

updateSummaryFields(array $fields)

this function is used to provide modifications to the summary fields in CMS by the extension By default, the summaryField() of its owner will merge more fields defined in the extension's $extra_fields['summary_fields']

Parameters

array $fields

Array of field names

updateFieldLabels(array $labels)

this function is used to provide modifications to the fields labels in CMS by the extension By default, the fieldLabels() of its owner will merge more fields defined in the extension's $extra_fields['field_labels']

Parameters

array $labels

Array of field labels

void onBeforeDuplicate()

Extension point in DataObject::duplicate()

Return Value

void

void onAfterDuplicate()

Extension point in DataObject::duplicate()

Return Value

void

SiteTree|null getTopPage()

Finds the top-level SiteTree object for a Block / ElementalArea, using the cached TopPageID reference when possible.

Return Value

SiteTree|null

Exceptions

ValidationException

void setTopPage(SiteTree|null $page = null)

Set top page to an object If no page is provided as an argument nor as a fixed id via DataExtension::withFixedTopPage() automatic page determination will be attempted Note that this may not always succeed as your model may not be attached to parent object at the time of this call

Parameters

SiteTree|null $page

Return Value

void

Exceptions

ValidationException

mixed withFixedTopPage(int $topPageID, callable $callback)

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 For example: model duplication where parent is assigned and saved only after the duplication is done It's not possible to determine top page in such case however it might be possible to know the top page even before the operation starts from the specific context Setting the page id to 0 disables this feature

Parameters

int $topPageID
callable $callback

Return Value

mixed

protected int getFixedTopPageID()

Get the ID of a page which is currently set as the fixed top page

Return Value

int

protected void updateTopPage()

Registers the object for a TopPage update. Ensures that this operation is deferred to a point when all required relations have been written.

Return Value

void

protected void assignTopPage(SiteTree $page)

Assigns top page relation

Parameters

SiteTree $page

Return Value

void

protected void clearTopPage()

Clears top page relation, this is useful when duplicating object as the new object doesn't necessarily belong to the original page

Return Value

void

protected void assignFixedTopPage()

Assigns top page relation based on fixed id

Return Value

void

See also

DataExtension::withFixedTopPage

protected void saveChanges(array $extraData = [])

Save top page changes without using write() Using raw query here because:

  • this is already called during write() and triggering more write() related extension points is undesirable
  • we don't want to create a new version if object is versioned
  • using writeWithoutVersion() produces some weird edge cases were data is not written because the fields are not recognised as changed (using forceChange() introduces a new set of issues)

Parameters

array $extraData

Return Value

void

protected SiteTree|null getTopPageFromCachedData(int $id)

Perform a page lookup based on cached data This function allows more extensibility as it can be fully overridden unlike an extension point Various projects may decide to alter this by injecting features like tracking, feature flags and even completely different data lookups This is a performance driven functionality so extension points are not great as they only allow adding features on top of existing ones not replacing them

Parameters

int $id

Return Value

SiteTree|null

protected string getTopPageTable()

Find table name which has the top page fields

Return Value

string

SiteTree TopPage()

No description

Return Value

SiteTree