mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
APICHANGE: add_extension() is now called directly on the class, instead of on Object
This commit is contained in:
parent
96a408029b
commit
fdea5321c7
@ -450,7 +450,15 @@ abstract class Object {
|
||||
* @param string $extension Subclass of {@link Extension} with optional parameters
|
||||
* as a string, e.g. "Versioned" or "Translatable('Param')"
|
||||
*/
|
||||
public static function add_extension($class, $extension) {
|
||||
public static function add_extension($extension) {
|
||||
$class = get_called_class();
|
||||
|
||||
if(func_num_args() > 1) {
|
||||
Deprecation::notice('3.1.0', "Object::add_extension() deprecated. Call add_extension() on the class");
|
||||
$class = func_get_arg(0);
|
||||
$extension = func_get_arg(1);
|
||||
}
|
||||
|
||||
if(!preg_match('/^([^(]*)/', $extension, $matches)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -291,7 +291,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
||||
if(!Object::has_extension($class, $extension)) {
|
||||
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
|
||||
$this->extensionsToRemove[$class][] = $extension;
|
||||
Object::add_extension($class, $extension);
|
||||
$class::add_extension($extension);
|
||||
$isAltered = true;
|
||||
}
|
||||
}
|
||||
@ -326,7 +326,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
||||
// Reapply ones removed
|
||||
foreach($this->extensionsToReapply as $class => $extensions) {
|
||||
foreach($extensions as $extension) {
|
||||
Object::add_extension($class, $extension);
|
||||
$class::add_extension($extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ Create a new file called `zzz_admin/code/BookmarkedPageExtension.php` and insert
|
||||
Enable the extension with the following line in `zzz_mysite/_config.php`:
|
||||
|
||||
:::php
|
||||
Object::add_extension('SiteTree', 'BookmarkedPageExtension');
|
||||
SiteTree::add_extension('BookmarkedPageExtension');
|
||||
|
||||
In order to add the field to the database, run a `dev/build/?flush=all`.
|
||||
Refresh the CMS, open a page for editing and you should see the new checkbox.
|
||||
@ -121,7 +121,7 @@ Add the following code to a new file `zzz_admin/code/BookmarkedLeftAndMainExtens
|
||||
Enable the extension with the following line in `zzz_mysite/_config.php`:
|
||||
|
||||
:::php
|
||||
Object::add_extension('LeftAndMain', 'BookmarkedPagesLeftAndMainExtension');
|
||||
LeftAndMain::add_extension('BookmarkedPagesLeftAndMainExtension');
|
||||
|
||||
As the last step, replace the hardcoded links with our list from the database.
|
||||
Find the `<ul>` you created earlier in `zzz_admin/admin/templates/LeftAndMain.ss`
|
||||
|
@ -31,14 +31,14 @@ ForumRole extension to the `[api:Member]` object.
|
||||
|
||||
|
||||
:::php
|
||||
Object::add_extension('Class You Want To Override', 'Your Class Name');
|
||||
ClassYouWantToOverride::add_extension('Your Class Name');
|
||||
|
||||
|
||||
For example above we want to override Member with a Custom Member so we would write the following
|
||||
|
||||
:::php
|
||||
// add to mysite/_config.php
|
||||
Object::add_extension('Member', 'CustomMember');
|
||||
Member::add_extension('CustomMember');
|
||||
|
||||
## Implementation
|
||||
|
||||
|
@ -91,7 +91,7 @@ Using inheritance to add extra behaviour or data fields to a member is limiting,
|
||||
class. A better way is to use role extensions to add this behaviour.
|
||||
|
||||
:::php
|
||||
Object::add_extension('Member', 'ForumRole');
|
||||
Member::add_extension('ForumRole');
|
||||
// OR
|
||||
Member::add_role('ForumRole');
|
||||
|
||||
|
@ -198,7 +198,7 @@ also another tool at your disposal: The `[api:Extension]` API.
|
||||
}
|
||||
|
||||
// mysite/_config.php
|
||||
Object::add_extension('MyAdmin', 'MyAdminExtension');
|
||||
MyAdmin::add_extension('MyAdminExtension');
|
||||
|
||||
The following extension points are available: `updateEditForm()`, `updateSearchContext()`,
|
||||
`updateSearchForm()`, `updateList()`, `updateImportForm`.
|
||||
|
@ -51,7 +51,7 @@ Create a mysite/code/CustomSiteConfig.php file.
|
||||
|
||||
Then add a link to your extension in the _config.php file like below.
|
||||
|
||||
Object::add_extension('SiteConfig', 'CustomSiteConfig');
|
||||
SiteConfig::add_extension('CustomSiteConfig');
|
||||
|
||||
|
||||
This tells SilverStripe to add the CustomSiteConfig extension to the `[api:SiteConfig]` class.
|
||||
|
@ -85,7 +85,7 @@ Example: Remove field for "image captions"
|
||||
|
||||
:::php
|
||||
// File: mysite/_config.php
|
||||
Object::add_extension('HtmlEditorField', 'MyToolbarExtension');
|
||||
HtmlEditorField::add_extension('MyToolbarExtension');
|
||||
|
||||
Adding functionality is a bit more advanced, you'll most likely
|
||||
need to add some fields to the PHP forms, as well as write some
|
||||
|
@ -24,7 +24,7 @@ which map to different database tables.
|
||||
|
||||
:::php
|
||||
// mysite/_config.php
|
||||
Object::add_extension('MyRecord', 'Versioned("Stage","Live")');
|
||||
MyRecord::add_extension('Versioned("Stage","Live")');
|
||||
|
||||
Note: The extension is automatically applied to `SiteTree` class.
|
||||
|
||||
|
@ -32,7 +32,7 @@ class FulltextSearchable extends DataExtension {
|
||||
* It can be used to limit the searched classes, but not to add your own classes.
|
||||
* For this purpose, please use {@link Object::add_extension()} directly:
|
||||
* <code>
|
||||
* Object::add_extension('MyObject', "FulltextSearchable('MySearchableField,'MyOtherField')");
|
||||
* MyObject::add_extension("FulltextSearchable('MySearchableField,'MyOtherField')");
|
||||
* </code>
|
||||
*
|
||||
* Caution: This is a wrapper method that should only be used in _config.php,
|
||||
@ -53,7 +53,7 @@ class FulltextSearchable extends DataExtension {
|
||||
|
||||
if(isset($defaultColumns[$class])) {
|
||||
Config::inst()->update($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
|
||||
Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')");
|
||||
$class::add_extension("FulltextSearchable('{$defaultColumns[$class]}')");
|
||||
} else {
|
||||
throw new Exception(
|
||||
"FulltextSearchable::enable() I don't know the default search columns for class '$class'");
|
||||
@ -61,7 +61,7 @@ class FulltextSearchable extends DataExtension {
|
||||
}
|
||||
self::$searchable_classes = $searchableClasses;
|
||||
if(class_exists("ContentController")){
|
||||
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
||||
ContentController::add_extension("ContentControllerSearchExtension");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -228,7 +228,7 @@ class ObjectTest extends SapphireTest {
|
||||
);
|
||||
|
||||
// ObjectTest_ExtendTest3 is added manually
|
||||
Object::add_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3("Param")');
|
||||
ObjectTest_ExtensionTest::add_extension('ObjectTest_ExtendTest3("Param")');
|
||||
$this->assertTrue(
|
||||
Object::has_extension('ObjectTest_ExtensionTest', 'ObjectTest_ExtendTest3'),
|
||||
"Extensions are detected with static has_extension() when added through add_extension()"
|
||||
@ -247,7 +247,7 @@ class ObjectTest extends SapphireTest {
|
||||
|
||||
public function testRemoveExtension() {
|
||||
// manually add ObjectTest_ExtendTest2
|
||||
Object::add_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2');
|
||||
ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2');
|
||||
$this->assertTrue(
|
||||
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'),
|
||||
"Extension added through \$add_extension() are added correctly"
|
||||
|
@ -147,4 +147,4 @@ class FormScaffolderTest_ArticleExtension extends DataExtension implements TestO
|
||||
|
||||
}
|
||||
|
||||
DataObject::add_extension('FormScaffolderTest_Article', 'FormScaffolderTest_ArticleExtension');
|
||||
FormScaffolderTest_Article::add_extension('FormScaffolderTest_ArticleExtension');
|
||||
|
@ -75,7 +75,7 @@ class DataExtensionTest extends SapphireTest {
|
||||
*/
|
||||
public function testAddExtensionLoadsStatics() {
|
||||
// Object::add_extension() will load DOD statics directly, so let's try adding a extension on the fly
|
||||
Object::add_extension('DataExtensionTest_Player', 'DataExtensionTest_PlayerExtension');
|
||||
DataExtensionTest_Player::add_extension('DataExtensionTest_PlayerExtension');
|
||||
|
||||
// Now that we've just added the extension, we need to rebuild the database
|
||||
$this->resetDBSchema(true);
|
||||
@ -226,7 +226,7 @@ class DataExtensionTest_RelatedObject extends DataObject implements TestOnly {
|
||||
|
||||
}
|
||||
|
||||
DataObject::add_extension('DataExtensionTest_Member', 'DataExtensionTest_ContactRole');
|
||||
DataExtensionTest_Member::add_extension('DataExtensionTest_ContactRole');
|
||||
|
||||
class DataExtensionTest_MyObject extends DataObject implements TestOnly {
|
||||
|
||||
@ -305,7 +305,7 @@ class DataExtensionTest_AppliedToDO extends DataExtension implements TestOnly {
|
||||
|
||||
}
|
||||
|
||||
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext1');
|
||||
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Ext2');
|
||||
DataObject::add_extension('DataExtensionTest_MyObject', 'DataExtensionTest_Faves');
|
||||
DataExtensionTest_MyObject::add_extension('DataExtensionTest_Ext1');
|
||||
DataExtensionTest_MyObject::add_extension('DataExtensionTest_Ext2');
|
||||
DataExtensionTest_MyObject::add_extension('DataExtensionTest_Faves');
|
||||
|
||||
|
@ -1313,5 +1313,5 @@ class DataObjectTest_TeamComment extends DataObject {
|
||||
);
|
||||
}
|
||||
|
||||
DataObject::add_extension('DataObjectTest_Team', 'DataObjectTest_Team_Extension');
|
||||
DataObjectTest_Team::add_extension('DataObjectTest_Team_Extension');
|
||||
|
||||
|
@ -18,7 +18,7 @@ class FulltextSearchableTest extends SapphireTest {
|
||||
public function tearDown() {
|
||||
// TODO This shouldn't need all arguments included
|
||||
if($this->orig['File_searchable']) {
|
||||
Object::add_extension('File', 'FulltextSearchable(\'"Filename","Title","Content"\')');
|
||||
File::add_extension('FulltextSearchable(\'"Filename","Title","Content"\')');
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
|
@ -470,7 +470,7 @@ class MemberTest extends FunctionalTest {
|
||||
$this->assertFalse($member->canEdit());
|
||||
|
||||
/* Apply a extension that allows viewing in any case (most likely the case for member profiles) */
|
||||
Object::add_extension('Member', 'MemberTest_ViewingAllowedExtension');
|
||||
Member::add_extension('MemberTest_ViewingAllowedExtension');
|
||||
$member2 = $this->objFromFixture('Member', 'staffmember');
|
||||
|
||||
$this->assertTrue($member2->canView());
|
||||
@ -479,7 +479,7 @@ class MemberTest extends FunctionalTest {
|
||||
|
||||
/* Apply a extension that denies viewing of the Member */
|
||||
Object::remove_extension('Member', 'MemberTest_ViewingAllowedExtension');
|
||||
Object::add_extension('Member', 'MemberTest_ViewingDeniedExtension');
|
||||
Member::add_extension('MemberTest_ViewingDeniedExtension');
|
||||
$member3 = $this->objFromFixture('Member', 'managementmember');
|
||||
|
||||
$this->assertFalse($member3->canView());
|
||||
@ -488,7 +488,7 @@ class MemberTest extends FunctionalTest {
|
||||
|
||||
/* Apply a extension that allows viewing and editing but denies deletion */
|
||||
Object::remove_extension('Member', 'MemberTest_ViewingDeniedExtension');
|
||||
Object::add_extension('Member', 'MemberTest_EditingAllowedDeletingDeniedExtension');
|
||||
Member::add_extension('MemberTest_EditingAllowedDeletingDeniedExtension');
|
||||
$member4 = $this->objFromFixture('Member', 'accountingmember');
|
||||
|
||||
$this->assertTrue($member4->canView());
|
||||
@ -603,7 +603,7 @@ class MemberTest extends FunctionalTest {
|
||||
*/
|
||||
protected function addExtensions($extensions) {
|
||||
if($extensions) foreach($extensions as $extension) {
|
||||
Object::add_extension('Member', $extension);
|
||||
Member::add_extension($extension);
|
||||
}
|
||||
return $extensions;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user