Replace all legacy ::: syntax with GFMD tags

This commit is contained in:
Aaron Carlino 2017-08-03 12:51:32 +12:00
parent 16407b9185
commit eb1695c03d
97 changed files with 2522 additions and 1014 deletions

View File

@ -19,7 +19,8 @@ Let's look at a simple example:
**mysite/code/Player.php**
:::php
```php
<?php
class Player extends DataObject {
@ -31,7 +32,7 @@ Let's look at a simple example:
'Birthday' => 'Date'
);
}
```
This `Player` class definition will create a database table `Player` with columns for `PlayerNumber`, `FirstName` and
so on. After writing this class, we need to regenerate the database schema.
@ -76,7 +77,8 @@ system. Instead, it will generate a new `ID` by adding 1 to the current maximum
**mysite/code/Player.php**
:::php
```php
<?php
class Player extends DataObject {
@ -88,6 +90,7 @@ system. Instead, it will generate a new `ID` by adding 1 to the current maximum
'Birthday' => 'Date'
);
}
```
Generates the following `SQL`.
@ -109,13 +112,17 @@ Generates the following `SQL`.
A new instance of a [DataObject](api:SilverStripe\ORM\DataObject) can be created using the `new` syntax.
:::php
```php
$player = new Player();
```
Or, a better way is to use the `create` method.
:::php
```php
$player = Player::create();
```
<div class="notice" markdown='1'>
Using the `create()` method provides chainability, which can add elegance and brevity to your code, e.g. `Player::create()->write()`. More importantly, however, it will look up the class in the [Injector](../extending/injector) so that the class can be overriden by [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection).
@ -125,28 +132,35 @@ Using the `create()` method provides chainability, which can add elegance and br
Database columns and properties can be set as class properties on the object. The SilverStripe ORM handles the saving
of the values through a custom `__set()` method.
:::php
```php
$player->FirstName = "Sam";
$player->PlayerNumber = 07;
```
To save the `DataObject` to the database, use the `write()` method. The first time `write()` is called, an `ID` will be
set.
:::php
```php
$player->write();
```
For convenience, the `write()` method returns the record's ID. This is particularly useful when creating new records.
:::php
```php
$player = Player::create();
$id = $player->write();
```
## Querying Data
With the `Player` class defined we can query our data using the `ORM` or Object-Relational Model. The `ORM` provides
shortcuts and methods for fetching, sorting and filtering data from our database.
:::php
```php
$players = Player::get();
// returns a `DataList` containing all the `Player` objects.
@ -158,16 +172,19 @@ shortcuts and methods for fetching, sorting and filtering data from our database
echo $player->dbObject('LastEdited')->Ago();
// calls the `Ago` method on the `LastEdited` property.
```
The `ORM` uses a "fluent" syntax, where you specify a query by chaining together different methods. Two common methods
are `filter()` and `sort()`:
:::php
```php
$members = Player::get()->filter(array(
'FirstName' => 'Sam'
))->sort('Surname');
// returns a `DataList` containing all the `Player` records that have the `FirstName` of 'Sam'
```
<div class="info" markdown="1">
Provided `filter` values are automatically escaped and do not require any escaping.
@ -180,7 +197,8 @@ The `ORM` doesn't actually execute the [SQLSelect](api:SilverStripe\ORM\Queries\
It's smart enough to generate a single efficient query at the last moment in time without needing to post-process the
result set in PHP. In `MySQL` the query generated by the ORM may look something like this
:::php
```php
$players = Player::get()->filter(array(
'FirstName' => 'Sam'
));
@ -189,11 +207,12 @@ result set in PHP. In `MySQL` the query generated by the ORM may look something
// executes the following single query
// SELECT * FROM Player WHERE FirstName = 'Sam' ORDER BY Surname
```
This also means that getting the count of a list of objects will be done with a single, efficient query.
:::php
```php
$players = Player::get()->filter(array(
'FirstName' => 'Sam'
))->sort('Surname');
@ -201,26 +220,31 @@ This also means that getting the count of a list of objects will be done with a
// This will create an single SELECT COUNT query
// SELECT COUNT(*) FROM Player WHERE FirstName = 'Sam'
echo $players->Count();
```
## Looping over a list of objects
`get()` returns a `DataList` instance. You can loop over `DataList` instances in both PHP and templates.
:::php
```php
$players = Player::get();
foreach($players as $player) {
echo $player->FirstName;
}
```
Notice that we can step into the loop safely without having to check if `$players` exists. The `get()` call is robust, and will at worst return an empty `DataList` object. If you do want to check if the query returned any records, you can use the `exists()` method, e.g.
:::php
```php
$players = Player::get();
if($players->exists()) {
// do something here
}
```
See the [Lists](lists) documentation for more information on dealing with [SS_List](api:SilverStripe\ORM\SS_List) instances.
@ -229,60 +253,73 @@ See the [Lists](lists) documentation for more information on dealing with [SS_Li
There are a couple of ways of getting a single DataObject from the ORM. If you know the ID number of the object, you
can use `byID($id)`:
:::php
```php
$player = Player::get()->byID(5);
```
`get()` returns a [DataList](api:SilverStripe\ORM\DataList) instance. You can use operations on that to get back a single record.
:::php
```php
$players = Player::get();
$first = $players->first();
$last = $players->last();
```
## Sorting
If would like to sort the list by `FirstName` in a ascending way (from A to Z).
:::php
```php
// Sort can either be Ascending (ASC) or Descending (DESC)
$players = Player::get()->sort('FirstName', 'ASC');
// Ascending is implied
$players = Player::get()->sort('FirstName');
```
To reverse the sort
:::php
```php
$players = Player::get()->sort('FirstName', 'DESC');
// or..
$players = Player::get()->sort('FirstName', 'ASC')->reverse();
```
However you might have several entries with the same `FirstName` and would like to sort them by `FirstName` and
`LastName`
:::php
```php
$players = Players::get()->sort(array(
'FirstName' => 'ASC',
'LastName'=>'ASC'
));
```
You can also sort randomly. Using the `DB` class, you can get the random sort method per database type.
:::php
```php
$random = DB::get_conn()->random();
$players = Player::get()->sort($random)
```
## Filtering Results
The `filter()` method filters the list of objects that gets returned.
:::php
```php
$players = Player::get()->filter(array(
'FirstName' => 'Sam'
));
```
Each element of the array specifies a filter. You can specify as many filters as you like, and they **all** must be
true for the record to be included in the result.
@ -292,52 +329,63 @@ value that you want to filter to.
So, this would return only those players called "Sam Minnée".
:::php
```php
$players = Player::get()->filter(array(
'FirstName' => 'Sam',
'LastName' => 'Minnée',
));
// SELECT * FROM Player WHERE FirstName = 'Sam' AND LastName = 'Minnée'
```
There is also a shorthand way of getting Players with the FirstName of Sam.
:::php
```php
$players = Player::get()->filter('FirstName', 'Sam');
```
Or if you want to find both Sam and Sig.
:::php
```php
$players = Player::get()->filter(
'FirstName', array('Sam', 'Sig')
);
// SELECT * FROM Player WHERE FirstName IN ('Sam', 'Sig')
```
You can use [SearchFilters](searchfilters) to add additional behavior to your `filter` command rather than an
exact match.
:::php
```php
$players = Player::get()->filter(array(
'FirstName:StartsWith' => 'S'
'PlayerNumber:GreaterThan' => '10'
));
```
### filterAny
Use the `filterAny()` method to match multiple criteria non-exclusively (with an "OR" disjunctive),
:::php
```php
$players = Player::get()->filterAny(array(
'FirstName' => 'Sam',
'Age' => 17,
));
// SELECT * FROM Player WHERE ("FirstName" = 'Sam' OR "Age" = '17')
```
You can combine both conjunctive ("AND") and disjunctive ("OR") statements.
:::php
```php
$players = Player::get()
->filter(array(
'LastName' => 'Minnée'
@ -347,14 +395,17 @@ You can combine both conjunctive ("AND") and disjunctive ("OR") statements.
'Age' => 17,
));
// SELECT * FROM Player WHERE ("LastName" = 'Minnée' AND ("FirstName" = 'Sam' OR "Age" = '17'))
```
You can use [SearchFilters](searchfilters) to add additional behavior to your `filterAny` command.
:::php
```php
$players = Player::get()->filterAny(array(
'FirstName:StartsWith' => 'S'
'PlayerNumber:GreaterThan' => '10'
));
```
### Filtering by null values
@ -366,29 +417,33 @@ checks to ensure that exclusion filters behave predictably.
For instance, the below code will select only values that do not match the given value, including nulls.
:::php
```php
$players = Player::get()->filter('FirstName:not', 'Sam');
// ... WHERE "FirstName" != 'Sam' OR "FirstName" IS NULL
// Returns rows with any value (even null) other than Sam
```
If null values should be excluded, include the null in your check.
:::php
```php
$players = Player::get()->filter('FirstName:not', array('Sam', null));
// ... WHERE "FirstName" != 'Sam' AND "FirstName" IS NOT NULL
// Only returns non-null values for "FirstName" that aren't Sam.
// Strictly the IS NOT NULL isn't necessary, but is included for explicitness
```
It is also often useful to filter by all rows with either empty or null for a given field.
:::php
```php
$players = Player::get()->filter('FirstName', array(null, ''));
// ... WHERE "FirstName" == '' OR "FirstName" IS NULL
// Returns rows with FirstName which is either empty or null
```
### Filtering by aggregates
@ -424,84 +479,104 @@ for each record, if the callback returns true, this record will be added to the
The below example will get all `Players` aged over 10.
:::php
```php
$players = Player::get()->filterByCallback(function($item, $list) {
return ($item->Age() > 10);
});
```
### Exclude
The `exclude()` method is the opposite to the filter in that it removes entries from a list.
:::php
```php
$players = Player::get()->exclude('FirstName', 'Sam');
// SELECT * FROM Player WHERE FirstName != 'Sam'
```
Remove both Sam and Sig..
:::php
```php
$players = Player::get()->exclude(
'FirstName', array('Sam','Sig')
);
```
`Exclude` follows the same pattern as filter, so for removing only Sam Minnée from the list:
:::php
```php
$players = Player::get()->exclude(array(
'FirstName' => 'Sam',
'Surname' => 'Minnée',
));
```
And removing Sig and Sam with that are either age 17 or 43.
:::php
```php
$players = Player::get()->exclude(array(
'FirstName' => array('Sam', 'Sig'),
'Age' => array(17, 43)
));
// SELECT * FROM Player WHERE ("FirstName" NOT IN ('Sam','Sig) OR "Age" NOT IN ('17', '43'));
```
You can use [SearchFilters](searchfilters) to add additional behavior to your `exclude` command.
:::php
```php
$players = Player::get()->exclude(array(
'FirstName:EndsWith' => 'S'
'PlayerNumber:LessThanOrEqual' => '10'
));
```
### Subtract
You can subtract entries from a [DataList](api:SilverStripe\ORM\DataList) by passing in another DataList to `subtract()`
:::php
```php
$sam = Player::get()->filter('FirstName', 'Sam');
$players = Player::get();
$noSams = $players->subtract($sam);
```
Though for the above example it would probably be easier to use `filter()` and `exclude()`. A better use case could be
when you want to find all the members that does not exist in a Group.
:::php
```php
// ... Finding all members that does not belong to $group.
$otherMembers = Member::get()->subtract($group->Members());
```
### Limit
You can limit the amount of records returned in a DataList by using the `limit()` method.
:::php
```php
$members = Member::get()->limit(5);
```
`limit()` accepts two arguments, the first being the amount of results you want returned, with an optional second
parameter to specify the offset, which allows you to tell the system where to start getting the results from. The
offset, if not provided as an argument, will default to 0.
:::php
```php
// Return 10 members with an offset of 4 (starting from the 5th result).
$members = Member::get()->sort('Surname')->limit(10, 4);
```
<div class="alert">
Note that the `limit` argument order is different from a MySQL LIMIT clause.
@ -516,12 +591,13 @@ slashes in table names, it is necessary to provide an alternate mapping.
For instance, the below model will be stored in the table name `BannerImage`
:::php
```php
namespace SilverStripe\BannerManager;
class BannerImage extends \DataObject {
private static $table_name = 'BannerImage';
}
```
Note that any model class which does not explicitly declare a `table_name` config option will have a name
automatically generated for them. In the above case, the table name would have been
@ -550,7 +626,8 @@ For example, if running a query against a particular model, you will need to ens
table and column.
:::php
```php
public function countDuplicates($model, $fieldToCheck) {
$table = DataObject::getSchema()->tableForField($model, $field);
$query = new SQLSelect();
@ -558,7 +635,7 @@ table and column.
$query->setWhere(["\"{$table}\".\"{$field}\"" => $model->$fieldToCheck]);
return $query->count();
}
```
### Raw SQL
@ -576,8 +653,10 @@ you need it to, you may also consider extending the ORM with new data types or f
You can specify a WHERE clause fragment (that will be combined with other filters using AND) with the `where()` method:
:::php
```php
$members = Member::get()->where("\"FirstName\" = 'Sam'")
```
#### Joining Tables
@ -587,13 +666,15 @@ You can specify a join with the `innerJoin` and `leftJoin` methods. Both of the
* The filter clause for the join.
* An optional alias.
:::php
```php
// Without an alias
$members = Member::get()
->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
$members = Member::get()
->innerJoin("Group_Members", "\"Rel\".\"MemberID\" = \"Member\".\"ID\"", "Rel");
```
<div class="alert" markdown="1">
Passing a *$join* statement to will filter results further by the JOINs performed against the foreign table. It will
@ -605,7 +686,8 @@ Passing a *$join* statement to will filter results further by the JOINs performe
Define the default values for all the `$db` fields. This example sets the "Status"-column on Player to "Active"
whenever a new object is created.
:::php
```php
<?php
class Player extends DataObject {
@ -614,6 +696,7 @@ whenever a new object is created.
"Status" => 'Active',
);
}
```
<div class="notice" markdown='1'>
Note: Alternatively you can set defaults directly in the database-schema (rather than the object-model). See
@ -629,7 +712,8 @@ time.
For example, suppose we have the following set of classes:
:::php
```php
<?php
class Page extends SiteTree {
@ -642,10 +726,12 @@ For example, suppose we have the following set of classes:
'Summary' => 'Text'
);
}
```
The data for the following classes would be stored across the following tables:
:::yml
```yml
SiteTree:
- ID: Int
- ClassName: Enum('SiteTree', 'Page', 'NewsPage')
@ -656,15 +742,18 @@ The data for the following classes would be stored across the following tables:
NewsPage:
- ID: Int
- Summary: Text
```
Accessing the data is transparent to the developer.
:::php
```php
$news = NewsPage::get();
foreach($news as $article) {
echo $article->Title;
}
```
The way the ORM stores the data is this:

View File

@ -15,7 +15,8 @@ SilverStripe supports a number of relationship types and each relationship type
A 1-to-1 relation creates a database-column called "`<relationship-name>`ID", in the example below this would be
"TeamID" on the "Player"-table.
:::php
```php
<?php
class Team extends DataObject {
@ -35,13 +36,15 @@ A 1-to-1 relation creates a database-column called "`<relationship-name>`ID", in
"Team" => "Team",
);
}
```
This defines a relationship called `Team` which links to a `Team` class. The `ORM` handles navigating the relationship
and provides a short syntax for accessing the related object.
At the database level, the `has_one` creates a `TeamID` field on `Player`. A `has_many` field does not impose any database changes. It merely injects a new method into the class to access the related records (in this case, `Players()`)
:::php
```php
$player = Player::get()->byId(1);
$team = $player->Team();
@ -49,15 +52,18 @@ At the database level, the `has_one` creates a `TeamID` field on `Player`. A `ha
echo $player->Team()->Title;
// returns the 'Title' column on the 'Team' or `getTitle` if it exists.
```
The relationship can also be navigated in [templates](../templates).
:::ss
```ss
<% with $Player %>
<% if $Team %>
Plays for $Team.Title
<% end_if %>
<% end_with %>
```
## Polymorphic has_one
@ -70,7 +76,8 @@ with the ID column identifies the object.
To specify that a has_one relation is polymorphic set the type to 'DataObject'.
Ideally, the associated has_many (or belongs_to) should be specified with dot notation.
::php
```php
class Player extends DataObject {
private static $has_many = array(
@ -92,6 +99,7 @@ Ideally, the associated has_many (or belongs_to) should be specified with dot no
"FanOf" => "DataObject"
);
}
```
<div class="warning" markdown='1'>
Note: The use of polymorphic relationships can affect query performance, especially
@ -110,7 +118,8 @@ Please specify a $has_one-relationship on the related child-class as well, in or
available on both ends.
</div>
:::php
```php
<?php
class Team extends DataObject {
@ -130,11 +139,13 @@ available on both ends.
"Team" => "Team",
);
}
```
Much like the `has_one` relationship, `has_many` can be navigated through the `ORM` as well. The only difference being
you will get an instance of [HasManyList](api:SilverStripe\ORM\HasManyList) rather than the object.
:::php
```php
$team = Team::get()->first();
echo $team->Players();
@ -146,10 +157,12 @@ you will get an instance of [HasManyList](api:SilverStripe\ORM\HasManyList) rath
foreach($team->Players() as $player) {
echo $player->FirstName;
}
```
To specify multiple `$has_many` to the same object you can use dot notation to distinguish them like below:
:::php
```php
<?php
class Person extends DataObject {
@ -167,7 +180,7 @@ To specify multiple `$has_many` to the same object you can use dot notation to d
"Cleaner" => "Person"
);
}
```
Multiple `$has_one` relationships are okay if they aren't linking to the same object type. Otherwise, they have to be
named.
@ -193,7 +206,8 @@ declaring the `$belongs_to`.
Similarly with `$has_many`, dot notation can be used to explicitly specify the `$has_one` which refers to this relation.
This is not mandatory unless the relationship would be otherwise ambiguous.
:::php
```php
<?php
class Team extends DataObject {
@ -209,7 +223,7 @@ This is not mandatory unless the relationship would be otherwise ambiguous.
'Team' => 'Team.Coach'
);
}
```
## many_many
@ -226,12 +240,13 @@ Much like the `has_one` relationship, `many_many` can be navigated through the `
The only difference being you will get an instance of [ManyManyList](api:SilverStripe\ORM\ManyManyList) or
[ManyManyThroughList](api:SilverStripe\ORM\ManyManyThroughList) rather than the object.
:::php
```php
$team = Team::get()->byId(1);
$supporters = $team->Supporters();
// returns a 'ManyManyList' instance.
```
### Automatic many_many table
@ -243,7 +258,8 @@ Extra fields on the mapping table can be created by declaring a `many_many_extra
config to add extra columns.
:::php
```php
<?php
class Team extends DataObject {
@ -263,7 +279,7 @@ config to add extra columns.
"Supports" => "Team",
];
}
```
### many_many through relationship joined on a separate DataObject
@ -287,7 +303,8 @@ or child record.
The syntax for `belongs_many_many` is unchanged.
:::php
```php
<?php
class Team extends DataObject {
@ -316,15 +333,17 @@ The syntax for `belongs_many_many` is unchanged.
'Supporter' => 'Supporter'
];
}
```
In order to filter on the join table during queries, you can use the class name of the joining table
for any sql conditions.
:::php
```php
$team = Team::get()->byId(1);
$supporters = $team->Supporters()->where(['"TeamSupporter"."Ranking"' => 1]);
```
Note: ->filter() currently does not support joined fields natively due to the fact that the
query for the join table is isolated from the outer query controlled by DataList.
@ -335,13 +354,14 @@ query for the join table is isolated from the outer query controlled by DataList
The relationship can also be navigated in [templates](../templates).
The joined record can be accessed via `Join` or `TeamSupporter` property (many_many through only)
:::ss
```ss
<% with $Supporter %>
<% loop $Supports %>
Supports $Title <% if $TeamSupporter %>(rank $TeamSupporter.Ranking)<% end_if %>
<% end_if %>
<% end_with %>
```
You can also use `$Join` in place of the join class alias (`$TeamSupporter`), if your template
is class-agnostic and doesn't know the type of the join table.
@ -355,7 +375,8 @@ To specify multiple $many_manys between the same classes, specify use the dot no
distinguish them like below:
:::php
```php
<?php
class Category extends DataObject {
@ -373,7 +394,7 @@ distinguish them like below:
'FeaturedInCategories' => 'Category.FeaturedProducts'
);
}
```
If you're unsure about whether an object should take on `many_many` or `belongs_many_many`,
the best way to think about it is that the object where the relationship will be edited
@ -387,7 +408,8 @@ Adding new items to a relations works the same, regardless if you're editing a *
encapsulated by [HasManyList](api:SilverStripe\ORM\HasManyList) and [ManyManyList](api:SilverStripe\ORM\ManyManyList), both of which provide very similar APIs, e.g. an `add()`
and `remove()` method.
:::php
```php
$team = Team::get()->byId(1);
// create a new supporter
@ -397,7 +419,7 @@ and `remove()` method.
// add the supporter.
$team->Supporters()->add($supporter);
```
## Custom Relations
@ -406,7 +428,8 @@ You can use the ORM to get a filtered result list without writing any SQL. For e
See [DataObject::$has_many](api:SilverStripe\ORM\DataObject::$has_many) for more info on the described relations.
:::php
```php
<?php
class Team extends DataObject {
@ -419,6 +442,7 @@ See [DataObject::$has_many](api:SilverStripe\ORM\DataObject::$has_many) for more
return $this->Players()->filter('Status', 'Active');
}
}
```
<div class="notice" markdown="1">
Adding new records to a filtered `RelationList` like in the example above doesn't automatically set the filtered

View File

@ -11,23 +11,28 @@ modify.
[SS_List](api:SilverStripe\ORM\SS_List) implements `IteratorAggregate`, allowing you to loop over the instance.
:::php
```php
$members = Member::get();
foreach($members as $member) {
echo $member->Name;
}
```
Or in the template engine:
:::ss
```ss
<% loop $Members %>
<!-- -->
<% end_loop %>
```
## Finding an item by value.
:::php
```php
// $list->find($key, $value);
//
@ -35,13 +40,14 @@ Or in the template engine:
echo $members->find('ID', 4)->FirstName;
// returns 'Sam'
```
## Maps
A map is an array where the array indexes contain data as well as the values. You can build a map from any list
:::php
```php
$members = Member::get()->map('ID', 'FirstName');
// $members = array(
@ -49,16 +55,20 @@ A map is an array where the array indexes contain data as well as the values. Yo
// 2 => 'Sig'
// 3 => 'Will'
// );
```
This functionality is provided by the [Map](api:SilverStripe\ORM\Map) class, which can be used to build a map around any `SS_List`.
:::php
```php
$members = Member::get();
$map = new Map($members, 'ID', 'FirstName');
```
## Column
:::php
```php
$members = Member::get();
echo $members->column('Email');
@ -68,12 +78,14 @@ This functionality is provided by the [Map](api:SilverStripe\ORM\Map) class, whi
// 'sig@silverstripe.com',
// 'will@silverstripe.com'
// );
```
## ArrayList
[ArrayList](api:SilverStripe\ORM\ArrayList) exists to wrap a standard PHP array in the same API as a database backed list.
:::php
```php
$sam = Member::get()->byId(5);
$sig = Member::get()->byId(6);
@ -83,7 +95,7 @@ This functionality is provided by the [Map](api:SilverStripe\ORM\Map) class, whi
echo $list->Count();
// returns '2'
```
## API Documentation

View File

@ -13,7 +13,8 @@ In the `Player` example, we have four database columns each with a different dat
**mysite/code/Player.php**
:::php
```php
<?php
class Player extends DataObject {
@ -25,6 +26,7 @@ In the `Player` example, we have four database columns each with a different dat
'Birthday' => 'Date'
);
}
```
## Available Types
@ -51,7 +53,8 @@ See the [API documentation](api:SilverStripe\ORM\FieldType\DBField) for a full l
For complex default values for newly instantiated objects see [Dynamic Default Values](how_tos/dynamic_default_fields).
For simple values you can make use of the `$defaults` array. For example:
:::php
```php
<?php
class Car extends DataObject {
@ -66,6 +69,7 @@ For simple values you can make use of the `$defaults` array. For example:
'Condition' => 'New'
);
}
```
### Default values for new database columns
@ -81,7 +85,8 @@ For enum values, it's the second parameter.
For example:
:::php
```php
<?php
class Car extends DataObject {
@ -92,6 +97,7 @@ For example:
'Make' => 'Varchar(["default" => "Honda"]),
);
}
```
## Formatting Output
@ -103,7 +109,8 @@ object we can control the formatting and it allows us to call methods defined fr
**mysite/code/Player.php**
:::php
```php
<?php
class Player extends DataObject {
@ -114,10 +121,12 @@ object we can control the formatting and it allows us to call methods defined fr
return DBField::create_field('Varchar', $this->FirstName . ' '. $this->LastName);
}
}
```
Then we can refer to a new `Name` column on our `Player` instances. In templates we don't need to use the `get` prefix.
:::php
```php
$player = Player::get()->byId(1);
echo $player->Name;
@ -128,12 +137,14 @@ Then we can refer to a new `Name` column on our `Player` instances. In templates
echo $player->getName()->LimitCharacters(2);
// returns "Sa.."
```
## Casting
Rather than manually returning objects from your custom functions. You can use the `$casting` property.
:::php
```php
<?php
class Player extends DataObject {
@ -146,6 +157,7 @@ Rather than manually returning objects from your custom functions. You can use t
return $this->FirstName . ' '. $this->LastName;
}
}
```
The properties on any SilverStripe object can be type casted automatically, by transforming its scalar value into an
instance of the [DBField](api:SilverStripe\ORM\FieldType\DBField) class, providing additional helpers. For example, a string can be cast as a [DBText](api:SilverStripe\ORM\FieldType\DBText)
@ -154,21 +166,26 @@ type, which has a `FirstSentence()` method to retrieve the first sentence in a l
On the most basic level, the class can be used as simple conversion class from one value to another, e.g. to round a
number.
:::php
```php
DBField::create_field('Double', 1.23456)->Round(2); // results in 1.23
```
Of course that's much more verbose than the equivalent PHP call. The power of [DBField](api:SilverStripe\ORM\FieldType\DBField) comes with its more
sophisticated helpers, like showing the time difference to the current date:
:::php
```php
DBField::create_field('Date', '1982-01-01')->TimeDiff(); // shows "30 years ago"
```
## Casting ViewableData
Most objects in SilverStripe extend from [ViewableData](api:SilverStripe\View\ViewableData), which means they know how to present themselves in a view
context. Through a `$casting` array, arbitrary properties and getters can be casted:
:::php
```php
<?php
class MyObject extends ViewableData {
@ -187,7 +204,7 @@ context. Through a `$casting` array, arbitrary properties and getters can be cas
$obj->MyDate; // returns string
$obj->obj('MyDate'); // returns object
$obj->obj('MyDate')->InPast(); // returns boolean
```
## Casting HTML Text
@ -207,7 +224,8 @@ We can overload the default behavior by making a function called "get`<fieldname
The following example will use the result of `getStatus` instead of the 'Status' database column. We can refer to the
database column using `dbObject`.
:::php
```php
<?php
class Player extends DataObject {
@ -219,7 +237,7 @@ database column using `dbObject`.
public function getStatus() {
return (!$this->obj("Birthday")->InPast()) ? "Unborn" : $this->dbObject('Status')->Value();
}
```
## API Documentation

View File

@ -18,7 +18,8 @@ a `ModelAdmin` record.
Example: Disallow creation of new players if the currently logged-in player is not a team-manager.
:::php
```php
<?php
class Player extends DataObject {
@ -49,6 +50,7 @@ Example: Disallow creation of new players if the currently logged-in player is n
parent::onBeforeWrite();
}
}
```
## onBeforeDelete
@ -57,7 +59,8 @@ Triggered before executing *delete()* on an existing object.
Example: Checking for a specific [permission](permissions) to delete this type of object. It checks if a
member is logged in who belongs to a group containing the permission "PLAYER_DELETE".
:::php
```php
<?php
class Player extends DataObject {
@ -75,8 +78,7 @@ member is logged in who belongs to a group containing the permission "PLAYER_DEL
parent::onBeforeDelete();
}
}
```
<div class="notice" markdown='1'>
Note: There are no separate methods for *onBeforeCreate* and *onBeforeUpdate*. Please check `$this->isInDb()` to toggle

View File

@ -16,7 +16,8 @@ you can put on field names to change this behavior. These are represented as `Se
An example of a `SearchFilter` in use:
:::php
```php
// fetch any player that starts with a S
$players = Player::get()->filter(array(
'FirstName:StartsWith' => 'S',
@ -28,6 +29,7 @@ An example of a `SearchFilter` in use:
'FirstName:PartialMatch' => 'z',
'LastName:PartialMatch' => 'z'
));
```
Developers can define their own [SearchFilter](api:SilverStripe\ORM\Filters\SearchFilter) if needing to extend the ORM filter and exclude behaviors.
@ -41,15 +43,17 @@ within the `DataListFilter.` prefixed namespace. New filters can be registered u
config:
:::yaml
```yaml
SilverStripe\Core\Injector\Injector:
DataListFilter.CustomMatch:
class: MyVendor/Search/CustomMatchFilter
```
The following is a query which will return everyone whose first name starts with "S", either lowercase or uppercase:
:::php
```php
$players = Player::get()->filter(array(
'FirstName:StartsWith:nocase' => 'S'
));
@ -58,6 +62,7 @@ The following is a query which will return everyone whose first name starts with
$players = Player::get()->filter(array(
'FirstName:StartsWith:not' => 'W'
));
```
## API Documentation

View File

@ -16,7 +16,8 @@ By default, all `DataObject` subclasses can only be edited, created and viewed b
code.
</div>
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -37,6 +38,7 @@ code.
return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
}
}
```
<div class="alert" markdown="1">
These checks are not enforced on low-level ORM operations such as `write()` or `delete()`, but rather rely on being

View File

@ -18,7 +18,8 @@ such as counts or returning a single column.
For example, if you want to run a simple `COUNT` SQL statement,
the following three statements are functionally equivalent:
:::php
```php
// Through raw SQL.
$count = DB::query('SELECT COUNT(*) FROM "Member"')->value();
@ -28,6 +29,7 @@ the following three statements are functionally equivalent:
// Through the ORM.
$count = Member::get()->count();
```
If you do use raw SQL, you'll run the risk of breaking
various assumptions the ORM and code based on it have:
@ -56,7 +58,8 @@ conditional filters, grouping, limiting, and sorting.
E.g.
:::php
```php
<?php
$sqlQuery = new SQLSelect();
@ -81,6 +84,7 @@ E.g.
foreach($result as $row) {
echo $row['BirthYear'];
}
```
The result of `SQLSelect::execute()` is an array lightly wrapped in a database-specific subclass of [Query](api:SilverStripe\ORM\Connect\Query).
This class implements the *Iterator*-interface, and provides convenience-methods for accessing the data.
@ -93,17 +97,20 @@ object instead.
For example, creating a `SQLDelete` object
:::php
```php
<?php
$query = SQLDelete::create()
->setFrom('"SiteTree"')
->setWhere(array('"SiteTree"."ShowInMenus"' => 0));
$query->execute();
```
Alternatively, turning an existing `SQLQuery` into a delete
:::php
```php
<?php
$query = SQLQuery::create()
@ -111,13 +118,16 @@ Alternatively, turning an existing `SQLQuery` into a delete
->setWhere(array('"SiteTree"."ShowInMenus"' => 0))
->toDelete();
$query->execute();
```
Directly querying the database
:::php
```php
<?php
DB::prepared_query('DELETE FROM "SiteTree" WHERE "SiteTree"."ShowInMenus" = ?', array(0));
```
### INSERT/UPDATE
@ -163,7 +173,8 @@ SQLInsert also includes the following api methods:
E.g.
:::php
```php
<?php
$update = SQLUpdate::create('"SiteTree"')->addWhere(array('ID' => 3));
@ -188,6 +199,7 @@ E.g.
// Perform the update
$update->execute();
```
In addition to assigning values, the SQLInsert object also supports multi-row
inserts. For database connectors and API that don't have multi-row insert support
@ -195,7 +207,8 @@ these are translated internally as multiple single row inserts.
For example,
:::php
```php
<?php
$insert = SQLInsert::create('"SiteTree"');
@ -216,6 +229,7 @@ For example,
// $columns will be array('"Title"', '"Content"', '"ClassName"');
$insert->execute();
```
### Value Checks
@ -224,18 +238,22 @@ e.g. when you want a single column rather than a full-blown object representatio
Example: Get the count from a relationship.
:::php
```php
$sqlQuery = new SQLSelect();
$sqlQuery->setFrom('Player');
$sqlQuery->addSelect('COUNT("Player"."ID")');
$sqlQuery->addWhere(array('"Team"."ID"' => 99));
$sqlQuery->addLeftJoin('Team', '"Team"."ID" = "Player"."TeamID"');
$count = $sqlQuery->execute()->value();
```
Note that in the ORM, this call would be executed in an efficient manner as well:
:::php
```php
$count = $myTeam->Players()->count();
```
### Mapping
@ -244,19 +262,22 @@ This can be useful for creating dropdowns.
Example: Show player names with their birth year, but set their birth dates as values.
:::php
```php
$sqlQuery = new SQLSelect();
$sqlQuery->setFrom('Player');
$sqlQuery->setSelect('Birthdate');
$sqlQuery->selectField('CONCAT("Name", ' - ', YEAR("Birthdate")', 'NameWithBirthyear');
$map = $sqlQuery->execute()->map();
$field = new DropdownField('Birthdates', 'Birthdates', $map);
```
Note that going through SQLSelect is just necessary here
because of the custom SQL value transformation (`YEAR()`).
An alternative approach would be a custom getter in the object definition.
:::php
```php
class Player extends DataObject {
private static $db = array(
'Name' => 'Varchar',
@ -268,6 +289,7 @@ An alternative approach would be a custom getter in the object definition.
}
$players = Player::get();
$map = $players->map('Name', 'NameWithBirthyear');
```
## Related

View File

@ -21,7 +21,8 @@ write, and respond appropriately if it isn't.
The return value of `validate()` is a [ValidationResult](api:SilverStripe\ORM\ValidationResult) object.
:::php
```php
<?php
class MyObject extends DataObject {
@ -41,6 +42,7 @@ The return value of `validate()` is a [ValidationResult](api:SilverStripe\ORM\Va
return $result;
}
}
```
## API Documentation

View File

@ -20,25 +20,27 @@ By default, adding the `Versioned extension will create a "Stage" and "Live" sta
also track versioned history.
:::php
```php
class MyStagedModel extends DataObject {
private static $extensions = [
Versioned::class
];
}
```
Alternatively, staging can be disabled, so that only versioned changes are tracked for your model. This
can be specified by setting the constructor argument to "Versioned"
:::php
```php
class VersionedModel extends DataObject {
private static $extensions = [
"SilverStripe\\ORM\\Versioning\\Versioned('Versioned')"
];
}
```
<div class="notice" markdown="1">
The extension is automatically applied to `SiteTree` class. For more information on extensions see
@ -79,7 +81,8 @@ automatically joined as required:
By default, all records are retrieved from the "Draft" stage (so the `MyRecord` table in our example). You can
explicitly request a certain stage through various getters on the `Versioned` class.
:::php
```php
// Fetching multiple records
$stageRecords = Versioned::get_by_stage('MyRecord', Versioned::DRAFT);
$liveRecords = Versioned::get_by_stage('MyRecord', Versioned::LIVE);
@ -87,14 +90,17 @@ explicitly request a certain stage through various getters on the `Versioned` cl
// Fetching a single record
$stageRecord = Versioned::get_by_stage('MyRecord', Versioned::DRAFT)->byID(99);
$liveRecord = Versioned::get_by_stage('MyRecord', Versioned::LIVE)->byID(99);
```
### Historical Versions
The above commands will just retrieve the latest version of its respective stage for you, but not older versions stored
in the `<class>_versions` tables.
:::php
```php
$historicalRecord = Versioned::get_version('MyRecord', <record-id>, <version-id>);
```
<div class="alert" markdown="1">
The record is retrieved as a `DataObject`, but saving back modifications via `write()` will create a new version,
@ -105,10 +111,12 @@ In order to get a list of all versions for a specific record, we need to generat
objects, which expose the same database information as a `DataObject`, but also include information about when and how
a record was published.
:::php
```php
$record = MyRecord::get()->byID(99); // stage doesn't matter here
$versions = $record->allVersions();
echo $versions->First()->Version; // instance of Versioned_Version
```
### Writing Versions and Changing Stages
@ -130,7 +138,8 @@ done via one of several ways:
* `publishRecursive` Publishes this record, and any dependant objects this record may refer to.
See "DataObject ownership" for reference on dependant objects.
:::php
```php
$record = Versioned::get_by_stage('MyRecord', Versioned::DRAFT)->byID(99);
$record->MyField = 'changed';
// will update `MyRecord` table (assuming Versioned::current_stage() == 'Stage'),
@ -138,25 +147,30 @@ done via one of several ways:
$record->write();
// will copy the saved record information to the `MyRecord_Live` table
$record->publishRecursive();
```
Similarly, an "unpublish" operation does the reverse, and removes a record from a specific stage.
:::php
```php
$record = MyRecord::get()->byID(99); // stage doesn't matter here
// will remove the row from the `MyRecord_Live` table
$record->deleteFromStage(Versioned::LIVE);
```
### Forcing the Current Stage
The current stage is stored as global state on the object. It is usually modified by controllers, e.g. when a preview
is initialized. But it can also be set and reset temporarily to force a specific operation to run on a certain stage.
:::php
```php
$origMode = Versioned::get_reading_mode(); // save current mode
$obj = MyRecord::getComplexObjectRetrieval(); // returns 'Live' records
Versioned::set_reading_mode(Versioned::DRAFT); // temporarily overwrite mode
$obj = MyRecord::getComplexObjectRetrieval(); // returns 'Stage' records
Versioned::set_reading_mode($origMode); // reset current mode
```
### DataObject ownership
@ -177,7 +191,8 @@ When pages of type `MyPage` are published, any owned images and banners will be
without requiring any custom code.
:::php
```php
class MyPage extends Page {
private static $has_many = array(
'Banners' => Banner::class
@ -199,7 +214,7 @@ without requiring any custom code.
'Image'
);
}
```
Note that ownership cannot be used with polymorphic relations. E.g. has_one to non-type specific `DataObject`.
@ -216,7 +231,8 @@ that can be used to traverse between each, and then by ensuring you configure bo
E.g.
:::php
```php
class MyParent extends DataObject {
private static $extensions = array(
Versioned::class
@ -239,6 +255,7 @@ E.g.
return MyParent::get()->first();
}
}
```
#### DataObject Ownership in HTML Content
@ -257,8 +274,10 @@ smaller modifications of the generated `DataList` objects.
Example: Get the first 10 live records, filtered by creation date:
:::php
```php
$records = Versioned::get_by_stage('MyRecord', Versioned::LIVE)->limit(10)->sort('Created', 'ASC');
```
### Permissions
@ -280,7 +299,8 @@ Versioned object visibility can be customised in one of the following ways by ed
E.g.
:::php
```php
class MyObject extends DataObject {
private static $extensions = array(
Versioned::class,
@ -298,6 +318,7 @@ E.g.
return Permission::checkMember($member, 'ADMIN');
}
}
```
If you want to control permissions of an object in an extension, you can also use
one of the below extension points in your `DataExtension` subclass:
@ -310,25 +331,29 @@ only be invoked if the object is in a non-published state.
E.g.
:::php
```php
class MyObjectExtension extends DataExtension {
public function canViewNonLive($member = null) {
return Permission::check($member, 'DRAFT_STATUS');
}
}
```
If none of the above checks are overridden, visibility will be determined by the
permissions in the `TargetObject.non_live_permissions` config.
E.g.
:::php
```php
class MyObject extends DataObject {
private static $extensions = array(
Versioned::class,
);
private static $non_live_permissions = array('ADMIN');
}
```
Versioned applies no additional permissions to `canEdit` or `canCreate`, and such
these permissions should be implemented as per standard unversioned DataObjects.
@ -345,12 +370,13 @@ default, and only preview draft content if explicitly requested (e.g. by the "pr
to force a specific stage, we recommend the `Controller->init()` method for this purpose, for example:
**mysite/code/MyController.php**
:::php
```php
public function init() {
parent::init();
Versioned::set_stage(Versioned::DRAFT);
}
```
### Controllers

View File

@ -12,7 +12,8 @@ customise those fields as required.
An example is `DataObject`, SilverStripe will automatically create your CMS interface so you can modify what you need.
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -31,10 +32,12 @@ An example is `DataObject`, SilverStripe will automatically create your CMS inte
return $fields;
}
}
```
To fully customise your form fields, start with an empty FieldList.
:::php
```php
<?php
public function getCMSFields() {
@ -49,8 +52,7 @@ To fully customise your form fields, start with an empty FieldList.
return $fields;
}
```
You can also alter the fields of built-in and module `DataObject` classes through your own
[DataExtension](/developer_guides/extending/extensions), and a call to `DataExtension->updateCMSFields`.
@ -60,7 +62,8 @@ You can also alter the fields of built-in and module `DataObject` classes throug
The `$searchable_fields` property uses a mixed array format that can be used to further customise your generated admin
system. The default is a set of array values listing the fields.
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -70,13 +73,14 @@ system. The default is a set of array values listing the fields.
'ProductCode'
);
}
```
Searchable fields will be appear in the search interface with a default form field (usually a [TextField](api:SilverStripe\Forms\TextField)) and a
default search filter assigned (usually an [ExactMatchFilter](api:SilverStripe\ORM\Filters\ExactMatchFilter)). To override these defaults, you can specify
additional information on `$searchable_fields`:
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -86,11 +90,13 @@ additional information on `$searchable_fields`:
'ProductCode' => 'NumericField'
);
}
```
If you assign a single string value, you can set it to be either a [FormField](api:SilverStripe\Forms\FormField) or [SearchFilter](api:SilverStripe\ORM\Filters\SearchFilter). To specify
both, you can assign an array:
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -107,11 +113,12 @@ both, you can assign an array:
),
);
}
```
To include relations (`$has_one`, `$has_many` and `$many_many`) in your search, you can use a dot-notation.
:::php
```php
<?php
class Team extends DataObject {
@ -141,14 +148,15 @@ To include relations (`$has_one`, `$has_many` and `$many_many`) in your search,
'Teams' => 'Team'
);
}
```
### Summary Fields
Summary fields can be used to show a quick overview of the data for a specific [DataObject](api:SilverStripe\ORM\DataObject) record. The most common use
is their display as table columns, e.g. in the search results of a [ModelAdmin](api:SilverStripe\Admin\ModelAdmin) CMS interface.
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -164,11 +172,12 @@ is their display as table columns, e.g. in the search results of a [ModelAdmin](
'ProductCode'
);
}
```
To include relations or field manipulations in your summaries, you can use a dot-notation.
:::php
```php
<?php
class OtherObject extends DataObject {
@ -195,11 +204,12 @@ To include relations or field manipulations in your summaries, you can use a dot
'OtherObject.Title' => 'Other Object Title'
);
}
```
Non-textual elements (such as images and their manipulations) can also be used in summaries.
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -217,6 +227,7 @@ Non-textual elements (such as images and their manipulations) can also be used i
'HeroImage.CMSThumbnail' => 'Hero Image'
);
}
```
## Related Documentation

View File

@ -24,7 +24,8 @@ this index is present on the associative entity).
Indexes are represented on a `DataObject` through the `DataObject::$indexes` array which maps index names to a
descriptor. There are several supported notations:
:::php
```php
<?php
class MyObject extends DataObject {
@ -38,6 +39,7 @@ descriptor. There are several supported notations:
'<index-name>' => ['<column-name>', '<other-column-name>'],
];
}
```
The `<column-name>` is used to put a standard non-unique index on the column specified. For complex or large tables
we recommend building the index to suite the requirements of your data.
@ -52,7 +54,8 @@ support the following:
**mysite/code/MyTestObject.php**
:::php
```php
<?php
class MyTestObject extends DataObject {
@ -66,6 +69,7 @@ support the following:
'MyIndexName' => ['MyField', 'MyOtherField'],
];
}
```
## Complex/Composite Indexes
For complex queries it may be necessary to define a complex or composite index on the supporting object. To create a

View File

@ -11,7 +11,8 @@ An example of a SilverStripe template is below:
**mysite/templates/Page.ss**
:::ss
```ss
<html>
<head>
<% base_tag %>
@ -38,6 +39,7 @@ An example of a SilverStripe template is below:
<% include Footer %>
</body>
</html>
```
<div class="note">
Templates can be used for more than HTML output. You can use them to output your data as JSON, XML, CSV or any other
@ -66,17 +68,21 @@ Variables are placeholders that will be replaced with data from the [DataModel](
[Controller](../controllers). Variables are prefixed with a `$` character. Variable names must start with an
alphabetic character or underscore, with subsequent characters being alphanumeric or underscore:
:::ss
```ss
$Title
```
This inserts the value of the Title database field of the page being displayed in place of `$Title`.
Variables can be chained together, and include arguments.
:::ss
```ss
$Foo
$Foo(param)
$Foo.Bar
```
These variables will call a method / field on the object and insert the returned value as a string into the template.
@ -97,16 +103,20 @@ Variables can come from your database fields, or custom methods you define on yo
**mysite/code/Page.php**
:::php
```php
public function UsersIpAddress()
{
return $this->getRequest()->getIP();
}
```
**mysite/code/Page.ss**
:::html
```html
<p>You are coming from $UsersIpAddress.</p>
```
<div class="node" markdown="1">
Method names that begin with `get` will automatically be resolved when their prefix is excluded. For example, the above method call `$UsersIpAddress` would also invoke a method named `getUsersIpAddress()`.
@ -119,29 +129,34 @@ record and any subclasses of those two.
**mysite/code/Layout/Page.ss**
:::ss
```ss
$Title
// returns the page `Title` property
$Content
// returns the page `Content` property
```
## Conditional Logic
The simplest conditional block is to check for the presence of a value (does not equal 0, null, false).
:::ss
```ss
<% if $CurrentMember %>
<p>You are logged in as $CurrentMember.FirstName $CurrentMember.Surname.</p>
<% end_if %>
```
A conditional can also check for a value other than falsy.
:::ss
```ss
<% if $MyDinner == "kipper" %>
Yummy, kipper for tea.
<% end_if %>
```
<div class="notice" markdown="1">
When inside template tags variables should have a '$' prefix, and literals should have quotes.
@ -149,16 +164,19 @@ When inside template tags variables should have a '$' prefix, and literals shoul
Conditionals can also provide the `else` case.
:::ss
```ss
<% if $MyDinner == "kipper" %>
Yummy, kipper for tea
<% else %>
I wish I could have kipper :-(
<% end_if %>
```
`else_if` commands can be used to handle multiple `if` statements.
:::ss
```ss
<% if $MyDinner == "quiche" %>
Real men don't eat quiche
<% else_if $MyDinner == $YourDinner %>
@ -166,15 +184,18 @@ Conditionals can also provide the `else` case.
<% else %>
Can I have some of your chips?
<% end_if %>
```
### Negation
The inverse of `<% if %>` is `<% if not %>`.
:::ss
```ss
<% if not $DinnerInOven %>
I'm going out for dinner tonight.
<% end_if %>
```
### Boolean Logic
@ -182,27 +203,32 @@ Multiple checks can be done using `||`, `or`, `&&` or `and`.
If *either* of the conditions is true.
:::ss
```ss
<% if $MyDinner == "kipper" || $MyDinner == "salmon" %>
yummy, fish for tea
<% end_if %>
```
If *both* of the conditions are true.
:::ss
```ss
<% if $MyDinner == "quiche" && $YourDinner == "kipper" %>
Lets swap dinners
<% end_if %>
```
### Inequalities
You can use inequalities like `<`, `<=`, `>`, `>=` to compare numbers.
:::ss
```ss
<% if $Number >= "5" && $Number <= "10" %>
Number between 5 and 10
<% end_if %>
```
## Includes
@ -210,39 +236,47 @@ Within SilverStripe templates we have the ability to include other templates usi
will be searched for using the same filename look-up rules as a regular template. However in the case of the include tag
an additional `Includes` directory will be inserted into the resolved path just prior to the filename.
:::ss
```ss
<% include SideBar %> <!-- chooses templates/Includes/Sidebar.ss -->
<% include MyNamespace/SideBar %> <!-- chooses templates/MyNamespace/Includes/Sidebar.ss -->
```
When using subfolders in your template structure
(e.g. to fit with namespaces in your PHP class structure), the `Includes/` folder needs to be innermost.
:::ss
```ss
<% include MyNamespace/SideBar %> <!-- chooses templates/MyNamespace/Includes/Sidebar.ss -->
```
The `include` tag can be particularly helpful for nested functionality and breaking large templates up. In this example,
the include only happens if the user is logged in.
:::ss
```ss
<% if $CurrentMember %>
<% include MembersOnlyInclude %>
<% end_if %>
```
Includes can't directly access the parent scope when the include is included. However you can pass arguments to the
include.
:::ss
```ss
<% with $CurrentMember %>
<% include MemberDetails Top=$Top, Name=$Name %>
<% end_with %>
```
## Looping Over Lists
The `<% loop %>` tag is used to iterate or loop over a collection of items such as [DataList](api:SilverStripe\ORM\DataList) or a [ArrayList](api:SilverStripe\ORM\ArrayList)
collection.
:::ss
```ss
<h1>Children of $Title</h1>
<ul>
@ -250,6 +284,7 @@ collection.
<li>$Title</li>
<% end_loop %>
</ul>
```
This snippet loops over the children of a page, and generates an unordered list showing the `Title` property from each
page.
@ -269,48 +304,58 @@ templates can call [DataList](api:SilverStripe\ORM\DataList) methods.
Sorting the list by a given field.
:::ss
```ss
<ul>
<% loop $Children.Sort(Title, ASC) %>
<li>$Title</li>
<% end_loop %>
</ul>
```
Limiting the number of items displayed.
:::ss
```ss
<ul>
<% loop $Children.Limit(10) %>
<li>$Title</li>
<% end_loop %>
</ul>
```
Reversing the loop.
:::ss
```ss
<ul>
<% loop $Children.Reverse %>
<li>$Title</li>
<% end_loop %>
</ul>
```
Filtering the loop.
:::ss
```ss
<ul>
<% loop $Children.Filter('School', 'College') %>
<li>$Title</li>
<% end_loop %>
</ul>
```
Methods can also be chained.
:::ss
```ss
<ul>
<% loop $Children.Filter('School', 'College').Sort(Score, DESC) %>
<li>$Title</li>
<% end_loop %>
</ul>
```
### Position Indicators
@ -327,7 +372,8 @@ iteration.
Last item defaults to 1, but can be passed as a parameter.
* `$TotalItems`: Number of items in the list (integer).
:::ss
```ss
<ul>
<% loop $Children.Reverse %>
<% if First %>
@ -337,6 +383,7 @@ iteration.
<li class="$EvenOdd">Child $Pos of $TotalItems - $Title</li>
<% end_loop %>
</ul>
```
<div class="info" markdown="1">
A common task is to paginate your lists. See the [Pagination](how_tos/pagination) how to for a tutorial on adding
@ -347,7 +394,8 @@ pagination.
$Modulus and $MultipleOf can help to build column and grid layouts.
:::ss
```ss
// returns an int
$Modulus(value, offset)
@ -361,6 +409,7 @@ $Modulus and $MultipleOf can help to build column and grid layouts.
<% end_loop %>
// returns <div class="column-3">, <div class="column-2">,
```
<div class="hint" markdown="1">
`$Modulus` is useful for floated grid CSS layouts. If you want 3 rows across, put $Modulus(3) as a class and add a
@ -370,33 +419,40 @@ $Modulus and $MultipleOf can help to build column and grid layouts.
$MultipleOf(value, offset) can also be utilized to build column and grid layouts. In this case we want to add a `<br>`
after every 3rd item.
:::ss
```ss
<% loop $Children %>
<% if $MultipleOf(3) %>
<br>
<% end_if %>
<% end_loop %>
```
### Escaping
Sometimes you will have template tags which need to roll into one another. Use `{}` to contain variables.
:::ss
```ss
$Foopx // will returns "" (as it looks for a `Foopx` value)
{$Foo}px // returns "3px" (CORRECT)
```
Or when having a `$` sign in front of the variable such as displaying money.
:::ss
```ss
$$Foo // returns ""
${$Foo} // returns "$3"
```
You can also use a backslash to escape the name of the variable, such as:
:::ss
```ss
$Foo // returns "3"
\$Foo // returns "$Foo"
```
<div class="hint" markdown="1">
For more information on formatting and casting variables see [Formating, Modifying and Casting Variables](casting)
@ -424,7 +480,8 @@ classes of the current scope object, and any [Extension](api:SilverStripe\Core\E
When in a particular scope, `$Up` takes the scope back to the previous level.
:::ss
```ss
<h1>Children of '$Title'</h1>
<% loop $Children %>
@ -434,6 +491,7 @@ When in a particular scope, `$Up` takes the scope back to the previous level.
<p>Page '$Title' is a grandchild of '$Up.Up.Title'</p>
<% end_loop %>
<% end_loop %>
```
Given the following structure, it will output the text.
@ -455,19 +513,22 @@ Given the following structure, it will output the text.
Additional selectors implicitely change the scope so you need to put additional `$Up` to get what you expect.
</div>
:::ss
```ss
<h1>Children of '$Title'</h1>
<% loop $Children.Sort('Title').First %>
<%-- We have two additional selectors in the loop expression so... --%>
<p>Page '$Title' is a child of '$Up.Up.Up.Title'</p>
<% end_loop %>
```
#### Top
While `$Up` provides us a way to go up one level of scope, `$Top` is a shortcut to jump to the top most scope of the
page. The previous example could be rewritten to use the following syntax.
:::ss
```ss
<h1>Children of '$Title'</h1>
<% loop $Children %>
@ -477,20 +538,25 @@ page. The previous example could be rewritten to use the following syntax.
<p>Page '$Title' is a grandchild of '$Top.Title'</p>
<% end_loop %>
<% end_loop %>
```
### With
The `<% with %>` tag lets you change into a new scope. Consider the following example:
:::ss
```ss
<% with $CurrentMember %>
Hello, $FirstName, welcome back. Your current balance is $Balance.
<% end_with %>
```
This is functionalty the same as the following:
:::ss
```ss
Hello, $CurrentMember.FirstName, welcome back. Your current balance is $CurrentMember.Balance
```
Notice that the first example is much tidier, as it removes the repeated use of the `$CurrentMember` accessor.
@ -502,22 +568,27 @@ refer directly to properties and methods of the [Member](api:SilverStripe\Securi
`$Me` outputs the current object in scope. This will call the `forTemplate` of the object.
:::ss
```ss
$Me
```
## Comments
Using standard HTML comments is supported. These comments will be included in the published site.
:::ss
$EditForm <!-- Some public comment about the form -->
```ss
$EditForm <!-- Some public comment about the form -->
```
However you can also use special SilverStripe comments which will be stripped out of the published site. This is useful
for adding notes for other developers but for things you don't want published in the public html.
:::ss
```ss
$EditForm <%-- Some hidden comment about the form --%>
```
## Related

View File

@ -30,12 +30,14 @@ functionality may not be included.
## Base Tag
:::ss
```ss
<head>
<% base_tag %>
..
</head>
```
The `<% base_tag %>` placeholder is replaced with the HTML base element. Relative links within a document (such as <img
src="someimage.jpg" />) will become relative to the URI specified in the base tag. This ensures the browser knows where
@ -49,19 +51,20 @@ A `<% base_tag %>` is nearly always required or assumed by SilverStripe to exist
## CurrentMember
Returns the currently logged in [Member](api:SilverStripe\Security\Member) instance, if there is one logged in.
Returns the currently logged in [Member](api:SilverStripe\Security\Member) instance, if there is one logged in.```ss
:::ss
<% if $CurrentMember %>
Welcome Back, $CurrentMember.FirstName
<% end_if %>
```
## Title and Menu Title
:::ss
```ss
$Title
$MenuTitle
```
Most objects within SilverStripe will respond to `$Title` (i.e they should have a `Title` database field or at least a
`getTitle()` method).
@ -75,8 +78,10 @@ If `MenuTitle` is left blank by the CMS author, it'll just default to the value
## Page Content
:::ss
```ss
$Content
```
It returns the database content of the `Content` property. With the CMS Module, this is the value of the WYSIWYG editor
but it is also the standard for any object that has a body of content to output.
@ -97,8 +102,10 @@ more details).
web pages. You'll need to install it via `composer`.
</div>
:::ss
```ss
$SiteConfig.Title
```
The [SiteConfig](../configuration/siteconfig) object allows content authors to modify global data in the CMS, rather
than PHP code. By default, this includes a Website title and a Tagline.
@ -119,73 +126,86 @@ If you dont want to include the title tag use `$MetaTags(false)`.
By default `$MetaTags` renders:
:::ss
```ss
<title>Title of the Page</title>
<meta name="generator" http-equiv="generator" content="SilverStripe 3.0" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
```
`$MetaTags(false)` will render
`$MetaTags(false)` will render```ss
:::ss
<meta name="generator" http-equiv="generator" content="SilverStripe 3.0" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
```
If using `$MetaTags(false)` we can provide a more custom `title`.
:::ss
```ss
$MetaTags(false)
<title>$Title - Bob's Fantasy Football</title>
```
## Links
:::ss
```ss
<a href="$Link">..</a>
```
All objects that could be accessible in SilverStripe should define a `Link` method and an `AbsoluteLink` method. Link
returns the relative URL for the object and `AbsoluteLink` outputs your full website address along with the relative
link.
:::ss
```ss
$Link
<!-- returns /about-us/offices/ -->
$AbsoluteLink
<!-- returns http://yoursite.com/about-us/offices/ -->
```
### Linking Modes
:::ss
```ss
$isSection
$isCurrent
```
When looping over a list of `SiteTree` instances through a `<% loop $Menu %>` or `<% loop $Children %>`, `$isSection` and `$isCurrent`
will return true or false based on page being looped over relative to the currently viewed page.
For instance, to only show the menu item linked if it's the current one:
:::ss
```ss
<% if $isCurrent %>
$Title
<% else %>
<a href="$Link">$Title</a>
<% end_if %>
```
An example for checking for `current` or `section` is as follows:
:::ss
```ss
<a class="<% if $isCurrent %>current<% else_if $isSection %>section<% end_if %>" href="$Link">$MenuTitle</a>
```
**Additional Utility Method**
* `$InSection(page-url)`: This if block will pass if we're currently on the page-url page or one of its children.
:::ss
```ss
<% if $InSection(about-us) %>
<p>You are viewing the about us section</p>
<% end_if %>
```
### URLSegment
@ -193,12 +213,14 @@ This returns the part of the URL of the page you're currently on. For example on
`URLSegment` will be `offices`. `URLSegment` cannot be used to generate a link since it does not output the full path.
It can be used within templates to generate anchors or other CSS classes.
:::ss
```ss
<div id="section-$URLSegment">
</div>
<!-- returns <div id="section-offices"> -->
```
## ClassName
@ -206,17 +228,21 @@ Returns the class of the current object in [scope](syntax#scope) such as `Page`
handy for a number of uses. A common use case is to add to your `<body>` tag to influence CSS styles and JavaScript
behavior based on the page type used:
:::ss
```ss
<body class="$ClassName">
<!-- returns <body class="HomePage">, <body class="BlogPage"> -->
```
## Children Loops
:::ss
```ss
<% loop $Children %>
<% end_loop %>
```
Will loop over all Children records of the current object context. Children are pages that sit under the current page in
the `CMS` or a custom list of data. This originates in the `Versioned` extension's `getChildren` method.
@ -228,10 +254,12 @@ context.
### ChildrenOf
:::ss
```ss
<% loop $ChildrenOf(<my-page-url>) %>
<% end_loop %>
```
Will create a list of the children of the given page, as identified by its `URLSegment` value. This can come in handy
because it's not dependent on the context of the current page. For example, it would allow you to list all staff member
@ -244,18 +272,21 @@ Content authors have the ability to hide pages from menus by un-selecting the `S
This option will be honored by `<% loop $Children %>` and `<% loop $Menu %>` however if you want to ignore the user
preference, `AllChildren` does not filter by `ShowInMenus`.
:::ss
```ss
<% loop $AllChildren %>
...
<% end_loop %>
```
### Menu Loops
:::ss
```ss
<% loop $Menu(1) %>
...
<% end_loop %>
```
`$Menu(1)` returns the top-level menu of the website. You can also create a sub-menu using `$Menu(2)`, and so forth.
@ -265,10 +296,12 @@ Pages with the `ShowInMenus` property set to `false` will be filtered out.
## Access to a specific Page
:::ss
```ss
<% with $Page(my-page) %>
$Title
<% end_with %>
```
Page will return a single page from site, looking it up by URL.
@ -276,10 +309,12 @@ Page will return a single page from site, looking it up by URL.
### Level
:::ss
```ss
<% with $Level(1) %>
$Title
<% end_with %>
```
Will return a page in the current path, at the level specified by the numbers. It is based on the current page context,
looking back through its parent pages. `Level(1)` being the top most level.
@ -292,7 +327,8 @@ For example, imagine you're on the "bob marley" page, which is three levels in:
### Parent
:::ss
```ss
<!-- given we're on 'Bob Marley' in "about us > staff > bob marley" -->
$Parent.Title
@ -300,7 +336,7 @@ For example, imagine you're on the "bob marley" page, which is three levels in:
$Parent.Parent.Title
<!-- returns 'about us' -->
```
## Navigating Scope
@ -314,17 +350,21 @@ for website users.
While you can achieve breadcrumbs through the `$Level(<level>)` control manually, there's a nicer shortcut: The
`$Breadcrumbs` variable.
:::ss
```ss
$Breadcrumbs
```
By default, it uses the template defined in `cms/templates/BreadcrumbsTemplate.ss`
:::ss
```ss
<% if $Pages %>
<% loop $Pages %>
<% if $Last %>$Title.XML<% else %><a href="$Link">$MenuTitle.XML</a> &raquo;<% end_if %>
<% end_loop %>
<% end_if %>
```
<div class="info" markdown="1">
To customise the markup that the `$Breadcrumbs` generates, copy `cms/templates/BreadcrumbsTemplate.ss` to
@ -333,8 +373,10 @@ To customise the markup that the `$Breadcrumbs` generates, copy `cms/templates/B
## Forms
:::ss
```ss
$Form
```
A page will normally contain some content and potentially a form of some kind. For example, the log-in page has a the
SilverStripe log-in form. If you are on such a page, the `$Form` variable will contain the HTML content of the form.

View File

@ -15,10 +15,12 @@ The `Requirements` class can work with arbitrary file paths.
**<my-module-dir>/templates/SomeTemplate.ss**
:::ss
```ss
<% require css("<my-module-dir>/css/some_file.css") %>
<% require themedCSS("some_themed_file") %>
<% require javascript("<my-module-dir>/javascript/some_file.js") %>
```
<div class="alert" markdown="1">
Requiring assets from the template is restricted compared to the PHP API.

View File

@ -112,7 +112,7 @@ footer and navigation will remain the same and we don't want to replicate this w
`$Layout` function allows us to define the child template area which can be overridden.
**mysite/templates/Page.ss**
```ss
<html>
<head>
..
@ -126,19 +126,19 @@ footer and navigation will remain the same and we don't want to replicate this w
<% include Footer %>
</body>
``
**mysite/templates/Layout/Page.ss**
```ss
<p>You are on a $Title page</p>
$Content
```
**mysite/templates/Layout/HomePage.ss**
```ss
<h1>This is the homepage!</h1>
<blink>Hi!</blink>
```
If your classes have in a namespace, the Layout folder will be a found inside of the appropriate namespace folder.

View File

@ -8,7 +8,8 @@ summary: Reduce rendering time with cached templates and understand the limitati
All functions that provide data to templates must have no side effects, as the value is cached after first access. For
example, this controller method will not behave as you might imagine.
:::php
```php
private $counter = 0;
public function Counter() {
@ -16,12 +17,15 @@ example, this controller method will not behave as you might imagine.
return $this->counter;
}
```
:::ss
```ss
$Counter, $Counter, $Counter
// returns 1, 1, 1
```
When we render `$Counter` to the template we would expect the value to increase and output `1, 2, 3`. However, as
`$Counter` is cached at the first access, the value of `1` is saved.
@ -32,7 +36,11 @@ When we render `$Counter` to the template we would expect the value to increase
Partial caching is a feature that allows the caching of just a portion of a page. Instead of fetching the required data
from the database to display, the contents of the area are fetched from a [cache backend](../performance/caching).
:::ss
```ss
<% cached 'MyCachedContent', LastEdited %>
$Title
<% end_cached %>
```

View File

@ -5,11 +5,11 @@ summary: Definition of the syntax for writing i18n compatible templates.
Translations are easy to use with a template, and give access to SilverStripe's translation facilities. Here is an
example:
```ss
<%t Foo.BAR 'Bar' %>
<%t Member.WELCOME 'Welcome {name} to {site}' name=$Member.Name site="Foobar.com" %>
```
`Member.WELCOME` is an identifier in the translation system, for which different translations may be available. This
string may include named placeholders, in braces.

View File

@ -12,17 +12,20 @@ output the result of the [DBHtmlText::FirstParagraph()](api:SilverStripe\ORM\Fie
**mysite/code/Page.ss**
:::ss
```ss
$Content.FirstParagraph
<!-- returns the result of HtmlText::FirstParagragh() -->
$LastEdited.Format("d/m/Y")
<!-- returns the result of SS_Datetime::Format("d/m/Y") -->
```
Any public method from the object in scope can be called within the template. If that method returns another
`ViewableData` instance, you can chain the method calls.
:::ss
```ss
$Content.FirstParagraph.NoHTML
<!-- "First Paragraph" -->
@ -31,6 +34,7 @@ Any public method from the object in scope can be called within the template. If
<div class="$URLSegment.LowerCase">
<!-- <div class="about-us"> -->
```
<div class="notice" markdown="1">
See the API documentation for [DBHtmlText](api:SilverStripe\ORM\FieldType\DBHtmlText), [FieldType](api:SilverStripe\ORM\FieldType), [DBText](api:SilverStripe\ORM\FieldType\DBText) for all the methods you can use to format
@ -43,8 +47,8 @@ When rendering an object to the template such as `$Me` the `forTemplate` method
provide default template for an object.
**mysite/code/Page.php**
```php
:::php
<?php
class Page extends SiteTree {
@ -53,12 +57,14 @@ provide default template for an object.
return "Page: ". $this->Title;
}
}
```
**mysite/templates/Page.ss**
```ss
:::ss
$Me
<!-- returns Page: Home -->
```
## Casting
@ -66,7 +72,8 @@ Methods which return data to the template should either return an explicit objec
content that method sends back, or, provide a type in the `$casting` array for the object. When rendering that method
to a template, SilverStripe will ensure that the object is wrapped in the correct type and values are safely escaped.
:::php
```php
<?php
class Page extends SiteTree {
@ -79,6 +86,7 @@ to a template, SilverStripe will ensure that the object is wrapped in the correc
return "<h1>This is my header</h1>";
}
}
```
When calling `$MyCustomMethod` SilverStripe now has the context that this method will contain HTML and escape the data
accordingly.

View File

@ -8,7 +8,8 @@ subclass the base `Controller` class.
**mysite/code/controllers/TeamController.php**
:::php
```php
<?php
class TeamController extends Controller {
@ -26,6 +27,7 @@ subclass the base `Controller` class.
print_r($request->allParams());
}
}
```
## Routing
@ -44,7 +46,8 @@ Make sure that after you have modified the `routes.yml` file, that you clear you
**mysite/_config/routes.yml**
:::yml
```yml
---
Name: mysiteroutes
After: framework/routes#coreroutes
@ -52,7 +55,7 @@ Make sure that after you have modified the `routes.yml` file, that you clear you
SilverStripe\Control\Director:
rules:
'teams//$Action/$ID/$Name': 'TeamController'
```
For more information about creating custom routes, see the [Routing](routing) documentation.
@ -74,7 +77,8 @@ Action methods can return one of four main things:
**mysite/code/controllers/TeamController.php**
:::php
```php
/**
* Return some additional data to the current response that is waiting to go out, this makes $Title set to
* 'MyTeamName' and continues on with generating the response.
@ -126,6 +130,7 @@ Action methods can return one of four main things:
return $this->getResponse().
}
```
For more information on how a URL gets mapped to an action see the [Routing](routing) documentation.
@ -153,10 +158,12 @@ Each controller should define a `Link()` method. This should be used to avoid ha
**mysite/code/controllers/TeamController.php**
:::php
```php
public function Link($action = null) {
return Controller::join_links('teams', $action);
}
```
<div class="info" markdown="1">
The [Controller::join_links()](api:SilverStripe\Control\Controller::join_links()) is optional, but makes `Link()` more flexible by allowing an `$action` argument, and concatenates the path segments with slashes. The action should map to a method on your controller.

View File

@ -11,7 +11,8 @@ actions on the website they shouldn't be able to.
Any action you define on a controller must be defined in a `$allowed_actions` static array. This prevents users from
directly calling methods that they shouldn't.
:::php
```php
<?php
class MyController extends Controller {
@ -36,6 +37,7 @@ directly calling methods that they shouldn't.
'complexactioncheck' => '->canComplexAction("MyRestrictedAction", false, 42)',
);
}
```
<div class="info">
If the permission check fails, SilverStripe will return a `403` Forbidden HTTP status.
@ -44,7 +46,8 @@ If the permission check fails, SilverStripe will return a `403` Forbidden HTTP s
An action named "index" is white listed by default, unless `allowed_actions` is defined as an empty array, or the action
is specifically restricted.
:::php
```php
<?php
class MyController extends Controller {
@ -53,10 +56,12 @@ is specifically restricted.
// allowed without an $allowed_action defined
}
}
```
`$allowed_actions` can be defined on `Extension` classes applying to the controller.
:::php
```php
<?php
class MyExtension extends Extension {
@ -65,10 +70,12 @@ is specifically restricted.
'mycustomaction'
);
}
```
Only public methods can be made accessible.
:::php
```php
<?php
class MyController extends Controller {
@ -86,10 +93,11 @@ Only public methods can be made accessible.
// ..
}
}
```
If a method on a parent class is overwritten, access control for it has to be redefined as well.
```php
:::php
<?php
class MyController extends Controller {
@ -113,6 +121,7 @@ If a method on a parent class is overwritten, access control for it has to be re
}
}
```
<div class="notice" markdown="1">
Access checks on parent classes need to be overwritten via the [Configuration API](../configuration).
@ -122,8 +131,8 @@ Access checks on parent classes need to be overwritten via the [Configuration AP
Form action methods should **not** be included in `$allowed_actions`. However, the form method **should** be included
as an `allowed_action`.
```php
:::php
<?php
class MyController extends Controller {
@ -140,13 +149,15 @@ as an `allowed_action`.
// ..
}
}
```
## Action Level Checks
Each method responding to a URL can also implement custom permission checks, e.g. to handle responses conditionally on
the passed request data.
:::php
```php
<?php
class MyController extends Controller {
@ -163,6 +174,7 @@ the passed request data.
return 'valid';
}
}
```
<div class="notice" markdown="1">
This is recommended as an addition for `$allowed_actions`, in order to handle more complex checks, rather than a
@ -178,7 +190,8 @@ execution. This behavior can be used to implement permission checks.
<div class="info" markdown="1">
`init` is called for any possible action on the controller and before any specific method such as `index`.
</div>
:::php
```php
<?php
class MyController extends Controller {
@ -193,6 +206,7 @@ execution. This behavior can be used to implement permission checks.
}
}
}
```
## Related Documentation

View File

@ -8,7 +8,9 @@ HTTP header.
**mysite/code/Page.php**
:::php
```php
$this->redirect('goherenow');
// redirect to Page::goherenow(), i.e on the contact-us page this will redirect to /contact-us/goherenow/
@ -20,25 +22,30 @@ HTTP header.
$this->redirectBack();
// go back to the previous page.
```
## Status Codes
The `redirect()` method takes an optional HTTP status code, either `301` for permanent redirects, or `302` for
temporary redirects (default).
```php
:::php
$this->redirect('/', 302);
// go back to the homepage, don't cache that this page has moved
```
## Redirection in URL Handling
Controllers can specify redirections in the `$url_handlers` property rather than defining a method by using the '~'
operator.
:::php
```php
private static $url_handlers = array(
'players/john' => '~>coach'
);
```
For more information on `$url_handlers` see the [Routing](routing) documenation.

View File

@ -14,7 +14,9 @@ See the [Forms Tutorial](../../tutorials/forms/) for a step by step process of c
Creating a [Form](api:SilverStripe\Forms\Form) has the following signature.
:::php
```php
$form = new Form(
$controller, // the Controller to render this form on
$name, // name of the method that returns this form on the controller
@ -22,6 +24,7 @@ Creating a [Form](api:SilverStripe\Forms\Form) has the following signature.
FieldList $actions, // list of FormAction instances
$required // optional use of RequiredFields object
);
```
In practice, this looks like:

View File

@ -6,13 +6,17 @@ summary: Customize the generated HTML for a FormField or an entire Form.
Most markup generated in SilverStripe can be replaced by custom templates. Both [Form](api:SilverStripe\Forms\Form) and [FormField](api:SilverStripe\Forms\FormField) instances
can be rendered out using custom templates using `setTemplate`.
:::php
```php
$form = new Form(..);
$form->setTemplate('MyCustomFormTemplate');
// or, just a field
$field = new TextField(..);
$field->setTemplate('MyCustomTextField');
```
Both `MyCustomTemplate.ss` and `MyCustomTextField.ss` should be located in **mysite/templates/forms/** or the same directory as the core.
@ -33,7 +37,9 @@ the core template structure. It is recommended to use `setTemplate` and unique t
For [FormField](api:SilverStripe\Forms\FormField) instances, there are several other templates that are used on top of the main `setTemplate`.
:::php
```php
$field = new TextField();
$field->setTemplate('CustomTextField');
@ -55,6 +61,7 @@ For [FormField](api:SilverStripe\Forms\FormField) instances, there are several o
// The difference here is the small field holder template is used when the
// field is embedded within another field. For example, if the field is
// part of a `FieldGroup` or `CompositeField` alongside other fields.
```
All templates are rendered within the scope of the [FormField](api:SilverStripe\Forms\FormField). To understand more about Scope within Templates as
well as the available syntax, see the [Templates](../templates) documentation.

View File

@ -20,25 +20,32 @@ website.
The `SecurityToken` automatically added looks something like:
:::php
```php
$form = new Form(..);
echo $form->getSecurityToken()->getValue();
// 'c443076989a7f24cf6b35fe1360be8683a753e2c'
```
This token value is passed through the rendered Form HTML as a [HiddenField](api:SilverStripe\Forms\HiddenField).
```html
:::html
<input type="hidden" name="SecurityID" value="c443076989a7f24cf6b35fe1360be8683a753e2c" class="hidden" />
```
The token should be present whenever a operation has a side effect such as a `POST` operation.
It can be safely disabled for `GET` requests as long as it does not modify the database (i.e a search form does not
normally require a security token).
:::php
```php
$form = new Form(..);
$form->disableSecurityToken();
```
<div class="alert" markdown="1">
Do not disable the SecurityID for forms that perform some modification to the users session. This will open your
@ -51,7 +58,9 @@ To reduce attack exposure forms are limited, by default, to the intended HTTP ve
this check, forms that rely on `GET` can be submitted via `POST` or `PUT` or vice-versa potentially leading to
application errors or edge cases. If you need to disable this setting follow the below example:
:::php
```php
$form = new Form(..);
$form->setFormMethod('POST');
@ -59,6 +68,7 @@ application errors or edge cases. If you need to disable this setting follow the
// or alternative short notation..
$form->setFormMethod('POST', false);
```
## Spam and Bot Attacks

View File

@ -9,21 +9,27 @@ when certain fields cannot be edited due to permissions. Creating the form is do
To make an entire [Form](api:SilverStripe\Forms\Form) read-only.
:::php
```php
$form = new Form(..);
$form->makeReadonly();
```
To make all the fields within a [FieldList](api:SilverStripe\Forms\FieldList) read-only (i.e to make fields read-only but not buttons).
:::php
```php
$fields = new FieldList(..);
$fields = $fields->makeReadonly();
```
To make a [FormField](api:SilverStripe\Forms\FormField) read-only you need to know the name of the form field or call it direct on the object
:::php
```php
$field = new TextField(..);
$field = $field->performReadonlyTransformation();
@ -38,16 +44,19 @@ To make a [FormField](api:SilverStripe\Forms\FormField) read-only you need to kn
$fields = new FieldList(
$field
);
```
## Disabled FormFields
Disabling [FormField](api:SilverStripe\Forms\FormField) instances, sets the `disabled` property on the class. This will use the same HTML markup as
a normal form, but set the `disabled` attribute on the `input` tag.
:::php
```php
$field = new TextField(..);
$field->setDisabled(true);
echo $field->forTemplate();
// returns '<input type="text" class="text" .. disabled="disabled" />'
```

View File

@ -21,34 +21,49 @@ display up to two levels of tabs in the interface. If you want to group data fur
## Adding a field to a tab
:::php
```php
$fields->addFieldToTab('Root.Main', new TextField(..));
```
## Removing a field from a tab
:::php
```php
$fields->removeFieldFromTab('Root.Main', 'Content');
```
## Creating a new tab
:::php
```php
$fields->addFieldToTab('Root.MyNewTab', new TextField(..));
```
## Moving a field between tabs
:::php
```php
$content = $fields->dataFieldByName('Content');
$fields->removeFieldFromTab('Root.Main', 'Content');
$fields->addFieldToTab('Root.MyContent', $content);
```
## Add multiple fields at once
:::php
```php
$fields->addFieldsToTab('Root.Content', array(
TextField::create('Name'),
TextField::create('Email')
));
```
## API Documentation

View File

@ -14,7 +14,9 @@ The following example will add a simple DateField to your Page, allowing you to
**mysite/code/Page.php**
:::php
```php
<?php
class Page extends SiteTree {
@ -34,17 +36,21 @@ The following example will add a simple DateField to your Page, allowing you to
return $fields;
}
}
```
## Custom Date Format
A custom date format for a [DateField](api:SilverStripe\Forms\DateField) can be provided through `setDateFormat`.
This is only necessary if you want to opt-out of the built-in browser localisation via `type=date`.
:::php
```php
// will display a date in the following format: 31/06/2012
DateField::create('MyDate')
->setHTML5(false)
->setDateFormat('dd/MM/yyyy');
```
<div class="info" markdown="1">
The formats are based on [ICU format](http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details).
@ -56,10 +62,13 @@ The formats are based on [ICU format](http://www.icu-project.org/apiref/icu4c/cl
Sets the minimum and maximum allowed date values using the `min` and `max` configuration settings (in ISO format or
`strtotime()`).
:::php
```php
DateField::create('MyDate')
->setMinDate('-7 days')
->setMaxDate('2012-12-31')
```
## Formatting Hints
@ -67,7 +76,9 @@ It's often not immediate apparent which format a field accepts, and showing the
of limited use to the average user. An alternative is to show the current date in the desired format alongside the
field description as an example.
:::php
```php
$dateField = DateField::create('MyDate');
// Show long format as text below the field
@ -79,6 +90,7 @@ field description as an example.
// Alternatively, set short format as a placeholder in the field
$dateField->setAttribute('placeholder', $dateField->getDateFormat());
```
<div class="notice" markdown="1">
Fields scaffolded through [DataObject::scaffoldCMSFields()](api:SilverStripe\ORM\DataObject::scaffoldCMSFields()) automatically have a description attached to them.

View File

@ -15,7 +15,9 @@ functionality. It is usually added through the [DataObject::getCMSFields()](api:
**mysite/code/MyObject.php**
:::php
```php
<?php
class MyObject extends DataObject {
@ -30,6 +32,7 @@ functionality. It is usually added through the [DataObject::getCMSFields()](api:
);
}
}
```
### Specify which configuration to use
@ -42,7 +45,9 @@ will use the configuration with the name 'myConfig'.
You can also specify which [HtmlEditorConfig](api:SilverStripe\Forms\HTMLEditor\HtmlEditorConfig) to use on a per field basis via the construct argument.
This is particularly useful if you need different configurations for multiple [HTMLEditorField](api:SilverStripe\Forms\HTMLEditor\HTMLEditorField) on the same page or form.
:::php
```php
class MyObject extends DataObject {
private static $db = array(
'Content' => 'HTMLText',
@ -56,6 +61,7 @@ This is particularly useful if you need different configurations for multiple [H
));
}
}
```
In the above example, the 'Content' field will use the default 'cms' config while 'OtherContent' will be using 'myConfig'.
@ -85,8 +91,11 @@ You can add plugins to the editor using the Framework's [HtmlEditorConfig::enabl
transparently generate the relevant underlying TinyMCE code.
**mysite/_config.php**
:::php
```php
HtmlEditorConfig::get('cms')->enablePlugins('media');
```
<div class="notice" markdown="1">
This utilities the TinyMCE's `PluginManager::load` function under the hood (check the
@ -98,14 +107,20 @@ Plugins and advanced themes can provide additional buttons that can be added (or
configuration. Here is an example of adding a `ssmacron` button after the `charmap` button:
**mysite/_config.php**
:::php
```php
HtmlEditorConfig::get('cms')->insertButtonsAfter('charmap', 'ssmacron');
```
Buttons can also be removed:
**mysite/_config.php**
:::php
```php
HtmlEditorConfig::get('cms')->removeButtons('tablecontrols', 'blockquote', 'hr');
```
<div class="notice" markdown="1">
Internally [HtmlEditorConfig](api:SilverStripe\Forms\HTMLEditor\HtmlEditorConfig) uses the TinyMCE's `theme_advanced_buttons` option to configure these. See the
@ -123,7 +138,9 @@ tags](http://www.tinymce.com/wiki.php/Configuration:extended_valid_elements) - t
from the HTML source by the editor.
**mysite/_config.php**
:::php
```php
// Add start and type attributes for <ol>, add <object> and <embed> with all attributes.
HtmlEditorConfig::get('cms')->setOption(
'extended_valid_elements',
@ -136,6 +153,7 @@ from the HTML source by the editor.
'area[shape|coords|href|target|alt],' .
'ol[start|type]'
);
```
<div class="notice" markdown="1">
The default setting for the CMS's `extended_valid_elements` we are overriding here can be found in
@ -148,8 +166,11 @@ It is also possible to add custom plugins to TinyMCE, for example toolbar button
You can enable them through [HtmlEditorConfig::enablePlugins()](api:SilverStripe\Forms\HTMLEditor\HtmlEditorConfig::enablePlugins()):
**mysite/_config.php**
:::php
```php
HtmlEditorConfig::get('cms')->enablePlugins(array('myplugin' => '../../../mysite/javascript/myplugin/editor_plugin.js'));
```
You can learn how to [create a plugin](http://www.tinymce.com/wiki.php/Creating_a_plugin) from the TinyMCE documentation.
@ -200,8 +221,11 @@ defaults to the stricter 'xhtml' setting, for example rendering self closing tag
In case you want to adhere to HTML4 instead, use the following configuration:
:::php
```php
HtmlEditorConfig::get('cms')->setOption('element_format', 'html');
```
By default, TinyMCE and SilverStripe will generate valid HTML5 markup, but it will strip out HTML5 tags like
`<article>` or `<figure>`. If you plan to use those, add them to the
@ -223,17 +247,24 @@ back and forth between a content representation the editor can understand, prese
Example: Remove field for "image captions"
:::php
```php
// File: mysite/code/MyToolbarExtension.php
class MyToolbarExtension extends Extension {
public function updateFieldsForImage(&$fields, $url, $file) {
$fields->removeByName('CaptionText');
}
}
```
```php
:::php
// File: mysite/_config.php
ModalController::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
@ -258,22 +289,28 @@ encapsulated in the [ModalController](api:SilverStripe\Admin\ModalController) cl
In the CMS, those dialogs are automatically instantiate, but in your own interfaces outside
of the CMS you have to take care of instantiate yourself:
:::php
```php
// File: mysite/code/MyController.php
class MyObjectController extends Controller {
public function Modals() {
return ModalController::create($this, "Modals");
}
}
```
Note: The dialogs rely on CMS-access, e.g. for uploading and browsing files,
so this is considered advanced usage of the field.
:::php
```php
// File: mysite/_config.php
HtmlEditorConfig::get('cms')->disablePlugins('ssbuttons');
HtmlEditorConfig::get('cms')->removeButtons('sslink', 'ssmedia');
HtmlEditorConfig::get('cms')->addButtonsToLine(2, 'link', 'media');
```
### Developing a wrapper to use a different WYSIWYG editors with HTMLEditorField
@ -293,7 +330,9 @@ Most modern browsers support it, although Internet Explorer only has limited
support in IE10. Alternatively, you can use the PSpell PHP module for server side checks.
Assuming you have the module installed, here's how you enable its use in `mysite/_config.php`:
:::php
```php
HtmlEditorConfig::get('cms')->enablePlugins('spellchecker', 'contextmenu');
HtmlEditorConfig::get('cms')->addButtonsToLine(2, 'spellchecker');
HtmlEditorConfig::get('cms')->setOption(
@ -301,9 +340,16 @@ Assuming you have the module installed, here's how you enable its use in `mysite
THIRDPARTY_DIR . '/tinymce-spellchecker/rpc.php'
);
HtmlEditorConfig::get('cms')->setOption('browser_spellcheck', false);
```
Now change the default spellchecker in `framework/thirdparty/tinymce-spellchecker/config.php`:
:::php
```php
// ...
$config['general.engine'] = 'PSpell';
```

View File

@ -6,10 +6,11 @@ summary: How to use the GridField class for managing tabular data.
[GridField](api:SilverStripe\Forms\GridField\GridField) is SilverStripe's implementation of data grids. The main purpose of the `FormField` is to display
tabular data in a format that is easy to view and modify. It can be thought of as a HTML table with some tricks.
:::php
```php
$field = new GridField($name, $title, $list);
```
<div class="hint" markdown='1'>
GridField can only be used with `$list` data sets that are of the type `SS_List` such as `DataList` or `ArrayList`.
@ -27,7 +28,9 @@ actions such as deleting records.
**mysite/code/Page.php**
:::php
```php
<?php
class Page extends SiteTree {
@ -42,6 +45,7 @@ actions such as deleting records.
return $fields;
}
}
```
This will display a bare bones `GridField` instance under `Pages` tab in the CMS. As we have not specified the
`GridField` configuration, the default configuration is an instance of [GridFieldConfig_Base](api:SilverStripe\Forms\GridField\GridFieldConfig_Base) which provides:
@ -58,7 +62,9 @@ the `getConfig()` method on `GridField`.
**mysite/code/Page.php**
:::php
```php
<?php
class Page extends SiteTree {
@ -89,11 +95,11 @@ the `getConfig()` method on `GridField`.
return $fields;
}
}
```
With the `GridFieldConfig` instance, we can modify the behavior of the `GridField`.
```php
:::php
// `GridFieldConfig::create()` will create an empty configuration (no components).
$config = GridFieldConfig::create();
@ -102,31 +108,41 @@ With the `GridFieldConfig` instance, we can modify the behavior of the `GridFiel
// Update the GridField with our custom configuration
$gridField->setConfig($config);
```
`GridFieldConfig` provides a number of methods to make setting the configuration easier. We can insert a component
before another component by passing the second parameter.
```php
:::php
$config->addComponent(new GridFieldFilterHeader(), 'GridFieldDataColumns');
```
We can add multiple components in one call.
:::php
```php
$config->addComponents(
new GridFieldDataColumns(),
new GridFieldToolbarHeader()
);
```
Or, remove a component.
:::php
```php
$config->removeComponentsByType('GridFieldDeleteAction');
```
Fetch a component to modify it later on.
:::php
$component = $config->getComponentByType('GridFieldFilterHeader')
```php
$component = $config->getComponentByType('GridFieldFilterHeader')
```
Here is a list of components for use bundled with the core framework. Many more components are provided by third-party
modules and extensions.
@ -152,7 +168,9 @@ developers manually adding each component.
A simple read-only and paginated view of records with sortable and searchable headers.
:::php
```php
$config = GridFieldConfig_Base::create();
$gridField->setConfig($config);
@ -164,6 +182,7 @@ A simple read-only and paginated view of records with sortable and searchable he
// .. new GridFieldDataColumns()
// .. new GridFieldPageCount('toolbar-header-right')
// .. new GridFieldPaginator($itemsPerPage)
```
### GridFieldConfig_RecordViewer
@ -180,7 +199,9 @@ The `DataObject` class displayed must define a `canView()` method that returns a
this record.
</div>
:::php
```php
$config = GridFieldConfig_RecordViewer::create();
$gridField->setConfig($config);
@ -188,6 +209,7 @@ this record.
// Same as GridFieldConfig_Base with the addition of
// .. new GridFieldViewButton(),
// .. new GridFieldDetailForm()
```
### GridFieldConfig_RecordEditor
@ -203,7 +225,9 @@ Permission control for editing and deleting the record uses the `canEdit()` and
`DataObject` object.
</div>
:::php
```php
$config = GridFieldConfig_RecordEditor::create();
$gridField->setConfig($config);
@ -212,16 +236,18 @@ Permission control for editing and deleting the record uses the `canEdit()` and
// .. new GridFieldAddNewButton(),
// .. new GridFieldEditButton(),
// .. new GridFieldDeleteAction()
```
### GridFieldConfig_RelationEditor
Similar to `GridFieldConfig_RecordEditor`, but adds features to work on a record's has-many or many-many relationships.
As such, it expects the list used with the `GridField` to be a instance of `RelationList`.
```php
:::php
$config = GridFieldConfig_RelationEditor::create();
$gridField->setConfig($config);
```
This configuration adds the ability to searched for existing records and add a relationship
(`GridFieldAddExistingAutocompleter`).
@ -235,11 +261,14 @@ The `GridFieldDetailForm` component drives the record viewing and editing form.
`DataObject->getCMSFields()` method but can be customised to accept different fields via the
[GridFieldDetailForm::setFields()](api:SilverStripe\Forms\GridField\GridFieldDetailForm::setFields()) method.
:::php
```php
$form = $gridField->getConfig()->getComponentByType('GridFieldDetailForm');
$form->setFields(new FieldList(
new TextField('Title')
));
```
### many_many_extraFields
@ -252,7 +281,9 @@ them as fields for relation extra data, and to avoid clashes with the other form
The namespace notation is `ManyMany[<extradata-field-name>]`, so for example `ManyMany[MyExtraField]`.
:::php
```php
<?php
class Team extends DataObject {
@ -303,7 +334,7 @@ The namespace notation is `ManyMany[<extradata-field-name>]`, so for example `Ma
return $fields;
}
}
```
## Flexible Area Assignment through Fragments
@ -322,10 +353,12 @@ These built-ins can be used by passing the fragment names into the constructor o
[GridFieldConfig](api:SilverStripe\Forms\GridField\GridFieldConfig) classes will already have rows added to them. The following example will add a print button at the
bottom right of the table.
:::php
```php
$config->addComponent(new GridFieldButtonRow('after'));
$config->addComponent(new GridFieldPrintButton('buttons-after-right'));
```
### Creating your own Fragments
@ -333,7 +366,9 @@ Fragments are designated areas within a `GridField` which can be shared between
your own fragments by using a `\$DefineFragment' placeholder in your components' template. This example will simply
create an area rendered before the table wrapped in a simple `<div>`.
:::php
```php
<?php
class MyAreaComponent implements GridField_HTMLProvider {
@ -344,6 +379,7 @@ create an area rendered before the table wrapped in a simple `<div>`.
);
}
}
```
<div class="notice" markdown="1">
Please note that in templates, you'll need to escape the dollar sign on `\$DefineFragment`. These are specially
@ -353,7 +389,9 @@ processed placeholders as opposed to native template syntax.
Now you can add other components into this area by returning them as an array from your
[GridFieldComponent::getHTMLFragments()](api:SilverStripe\Forms\GridField\GridFieldComponent::getHTMLFragments()) implementation:
:::php
```php
<?php
class MyShareLinkComponent implements GridField_HTMLProvider {
@ -364,11 +402,15 @@ Now you can add other components into this area by returning them as an array fr
);
}
}
```
Your new area can also be used by existing components, e.g. the [GridFieldPrintButton](api:SilverStripe\Forms\GridField\GridFieldPrintButton)
:::php
```php
new GridFieldPrintButton('my-component-area');
```
## Creating a Custom GridFieldComponent

View File

@ -9,7 +9,9 @@ code for a `Form` is to create it as a subclass to `Form`. Let's look at a examp
**mysite/code/Page.php**
:::php
```php
<?php
class PageController extends ContentController {
@ -59,13 +61,16 @@ code for a `Form` is to create it as a subclass to `Form`. Let's look at a examp
..
}
```
Now that is a bit of code to include on our controller and generally makes the file look much more complex than it
should be. Good practice would be to move this to a subclass and create a new instance for your particular controller.
**mysite/code/forms/SearchForm.php**
:::php
```php
<?php
class SearchForm extends Form {
@ -120,12 +125,15 @@ should be. Good practice would be to move this to a subclass and create a new in
$this->loadDataFrom($_REQUEST);
}
}
```
Our controller will now just have to create a new instance of this form object. Keeping the file light and easy to read.
**mysite/code/Page.php**
:::php
```php
<?php
class PageController extends ContentController {
@ -138,6 +146,7 @@ Our controller will now just have to create a new instance of this form object.
return new SearchForm($this, 'SearchForm');
}
}
```
Form actions can also be defined within your `Form` subclass to keep the entire form logic encapsulated.

View File

@ -11,7 +11,9 @@ totally custom template to meet our needs. To do this, we'll provide the class w
**mysite/code/Page.php**
:::php
```php
<?php
public function SearchForm() {
@ -28,10 +30,13 @@ totally custom template to meet our needs. To do this, we'll provide the class w
return $form;
}
```
**mysite/templates/Includes/SearchForm.ss**
:::ss
```ss
<form $FormAttributes>
<fieldset>
$Fields.dataFieldByName(q)
@ -41,6 +46,7 @@ totally custom template to meet our needs. To do this, we'll provide the class w
<% loop $Actions %>$Field<% end_loop %>
</div>
</form>
```
`SearchForm.ss` will be executed within the scope of the `Form` object so has access to any of the methods and
properties on [Form](api:SilverStripe\Forms\Form) such as `$Fields` and `$Actions`.

View File

@ -17,7 +17,9 @@ perform custom operations on a row.
A basic outline of our new `GridFieldCustomAction.php` will look like something
below:
:::php
```php
<?php
class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider {
@ -32,7 +34,6 @@ below:
return array('class' => 'grid-field__col-compact');
}
public function getColumnMetadata($gridField, $columnName) {
if($columnName == 'Actions') {
return array('title' => '');
@ -54,7 +55,6 @@ below:
array('RecordID' => $record->ID)
);
return $field->Field();
}
@ -74,6 +74,7 @@ below:
}
}
}
```
## Add the GridFieldCustomAction to the current `GridFieldConfig`
@ -82,7 +83,9 @@ a new instance of the class to the [GridFieldConfig](api:SilverStripe\Forms\Grid
[Reference](/developer_guides/forms/field_types/gridfield) documentation has more information about
manipulating the `GridFieldConfig` instance if required.
:::php
```php
// option 1: creating a new GridField with the CustomAction
$config = GridFieldConfig::create();
$config->addComponent(new GridFieldCustomAction());
@ -91,6 +94,7 @@ manipulating the `GridFieldConfig` instance if required.
// option 2: adding the CustomAction to an exisitng GridField
$gridField->getConfig()->addComponent(new GridFieldCustomAction());
```
For documentation on adding a Component to a `GridField` created by `ModelAdmin`
please view the [GridField Customization](/developer_guides/forms/how_tos/create_a_gridfield_actionprovider) section.

View File

@ -4,7 +4,9 @@ In this how-to, we'll explain how to set up a specific page type
holding a contact form, which submits a message via email.
Let's start by defining a new `ContactPage` page type:
:::php
```php
<?php
class ContactPage extends Page {
}
@ -22,29 +24,39 @@ Let's start by defining a new `ContactPage` page type:
return new Form($this, 'Form', $fields, $actions);
}
}
```
To create a form, we instanciate a `Form` object on a function on our page controller. We'll call this function `Form()`. You're free to choose this name, but it's standard practice to name the function `Form()` if there's only a single form on the page.
There's quite a bit in this function, so we'll step through one piece at a time.
:::php
```php
$fields = new FieldList(
new TextField('Name'),
new EmailField('Email'),
new TextareaField('Message')
);
```
First we create all the fields we want in the contact form, and put them inside a FieldList. You can find a list of form fields available on the [FormField](api:SilverStripe\Forms\FormField) page.
:::php
```php
$actions = FieldList(
new FormAction('submit', 'Submit')
);
```
We then create a [FieldList](api:SilverStripe\Forms\FieldList) of the form actions, or the buttons that submit the form. Here we add a single form action, with the name 'submit', and the label 'Submit'. We'll use the name of the form action later.
:::php
```php
return new Form($this, 'Form', $fields, $actions);
```
Finally we create the `Form` object and return it. The first argument is the controller that the form is on this is almost always $this. The second argument is the name of the form this has to be the same as the name of the function that creates the form, so we've used 'Form'. The third and fourth arguments are the fields and actions we created earlier.
@ -60,7 +72,9 @@ If you now create a ContactPage in the CMS (making sure you have rebuilt the dat
Now that we have a contact form, we need some way of collecting the data submitted. We do this by creating a function on the controller with the same name as the form action. In this case, we create the function 'submit' on the ContactPage_Controller class.
:::php
```php
class ContactPageController extends PageController {
private static $allowed_actions = array('Form');
public function Form() {
@ -85,6 +99,7 @@ Now that we have a contact form, we need some way of collecting the data submitt
);
}
}
```
<div class="hint" markdown="1">
Caution: This form is prone to abuse by spammers,
@ -106,12 +121,15 @@ All forms have some basic validation built in email fields will only let the
The framework comes with a predefined validator called [RequiredFields](api:SilverStripe\Forms\RequiredFields), which performs the common task of making sure particular fields are filled out. Below is the code to add validation to a contact form:
:::php
```php
public function Form() {
// ...
$validator = new RequiredFields('Name', 'Message');
return new Form($this, 'Form', $fields, $actions, $validator);
}
```
We've created a RequiredFields object, passing the name of the fields we want to be required. The validator we have created is then passed as the fifth argument of the form constructor. If we now try to submit the form without filling out the required fields, JavaScript validation will kick in, and the user will be presented with a message about the missing fields. If the user has JavaScript disabled, PHP validation will kick in when the form is submitted, and the user will be redirected back to the Form with messages about their missing fields.

View File

@ -27,7 +27,9 @@ be marked `private static` and follow the `lower_case_with_underscores` structur
**mysite/code/MyClass.php**
:::php
```php
<?php
class MyClass extends Page {
@ -44,17 +46,22 @@ be marked `private static` and follow the `lower_case_with_underscores` structur
// ..
}
```
## Accessing and Setting Configuration Properties
This can be done by calling the static method [Config::inst()](api:SilverStripe\Core\Config\Config::inst()), like so:
:::php
```php
$config = Config::inst()->get('MyClass', 'property');
```
Or through the `config()` object on the class.
```php
$config = $this->config()->get('property')';
```
Note that by default `Config::inst()` returns only an immutable version of config. Use `Config::modify()`
if it's necessary to alter class config. This is generally undesirable in most applications, as modification
@ -77,17 +84,22 @@ To set those configuration options on our previously defined class we can define
**mysite/_config/app.yml**
:::yml
```yml
MyClass:
option_one: false
option_two:
- Foo
- Bar
- Baz
```
To use those variables in your application code:
:::php
```php
$me = new MyClass();
echo $me->config()->option_one;
@ -114,6 +126,7 @@ To use those variables in your application code:
echo implode(', ', MyClass::config()->option_one);
// returns 'Qux'
```
<div class="notice" markdown="1">
There is no way currently to restrict read or write access to any configuration property, or influence/check the values
@ -189,8 +202,8 @@ The name of the files within the applications `_config` directly are arbitrary.
</div>
The structure of each YAML file is a series of headers and values separated by YAML document separators.
```yml
:::yml
---
Name: adminroutes
After:
@ -201,6 +214,7 @@ The structure of each YAML file is a series of headers and values separated by Y
rules:
'admin': 'AdminRootController'
---
```
<div class="info">
If there is only one set of values the header can be omitted.
@ -237,7 +251,9 @@ before (lower priority than) or after (higher priority than) some other value se
To specify these rules you add an "After" and/or "Before" key to the relevant header section. The value for these
keys is a list of reference paths to other value sections. A basic example:
:::yml
```yml
---
Name: adminroutes
After:
@ -248,6 +264,7 @@ keys is a list of reference paths to other value sections. A basic example:
rules:
'admin': 'AdminRootController'
---
```
You do not have to specify all portions of a reference path. Any portion may be replaced with a wildcard "\*", or left
out all together. Either has the same affect - that portion will be ignored when checking a value section's reference
@ -300,7 +317,9 @@ You then list any of the following rules as sub-keys, with informational values
For instance, to add a property to "foo" when a module exists, and "bar" otherwise, you could do this:
:::yml
```yml
---
Only:
moduleexists: 'MyFineModule'
@ -314,17 +333,20 @@ For instance, to add a property to "foo" when a module exists, and "bar" otherwi
MyClass:
property: 'bar'
---
```
Multiple conditions of the same type can be declared via array format
:::yaml
```yaml
---
Only:
moduleexists:
- 'silverstripe/blog'
- 'silverstripe/lumberjack'
```
<div class="alert" markdown="1">
When you have more than one rule for a nested fragment, they're joined like

View File

@ -10,23 +10,28 @@ throughout the site. Out of the box this includes selecting the current site the
`SiteConfig` options can be accessed from any template by using the $SiteConfig variable.
:::ss
```ss
$SiteConfig.Title
$SiteConfig.Tagline
<% with $SiteConfig %>
$Title $AnotherField
<% end_with %>
```
To access variables in the PHP:
:::php
```php
$config = SiteConfig::current_site_config();
echo $config->Title;
// returns "Website Name"
```
## Extending SiteConfig
@ -34,7 +39,9 @@ To extend the options available in the panel, define your own fields via a [Data
**mysite/code/extensions/CustomSiteConfig.php**
:::php
```php
<?php
class CustomSiteConfig extends DataExtension {
@ -49,15 +56,19 @@ To extend the options available in the panel, define your own fields via a [Data
);
}
}
```
Then activate the extension.
**mysite/_config/app.yml**
:::yml
```yml
Silverstripe\SiteConfig\SiteConfig:
extensions:
- CustomSiteConfig
```
<div class="notice" markdown="1">
After adding the class and the YAML change, make sure to rebuild your database by visiting http://yoursite.com/dev/build.

View File

@ -17,7 +17,9 @@ and `RequestHandler`. You can still apply extensions to descendants of these cla
**mysite/code/extensions/MyMemberExtension.php**
:::php
```php
<?php
class MyMemberExtension extends DataExtension {
@ -31,6 +33,7 @@ and `RequestHandler`. You can still apply extensions to descendants of these cla
return "Hi " . $this->owner->Name;
}
}
```
<div class="info" markdown="1">
Convention is for extension class names to end in `Extension`. This isn't a requirement but makes it clearer
@ -41,15 +44,21 @@ we want to add the `MyMemberExtension` too. To activate this extension, add the
**mysite/_config/app.yml**
:::yml
```yml
Member:
extensions:
- MyMemberExtension
```
Alternatively, we can add extensions through PHP code (in the `_config.php` file).
:::php
```php
Member::add_extension('MyMemberExtension');
```
This class now defines a `MyMemberExtension` that applies to all `Member` instances on the website. It will have
transformed the original `Member` class in two ways:
@ -70,7 +79,9 @@ $has_one etc.
**mysite/code/extensions/MyMemberExtension.php**
:::php
```php
<?php
class MyMemberExtension extends DataExtension {
@ -88,13 +99,16 @@ $has_one etc.
return "Hi " . $this->owner->Name;
}
}
```
**mysite/templates/Page.ss**
:::ss
```ss
$CurrentMember.Position
$CurrentMember.Image
```
## Adding Methods
@ -102,18 +116,23 @@ Methods that have a unique name will be called as part of the `__call` method on
we added a `SayHi` method which is unique to our extension.
**mysite/templates/Page.ss**
:::ss
```ss
<p>$CurrentMember.SayHi</p>
// "Hi Sam"
```
**mysite/code/Page.php**
:::php
```php
$member = Security::getCurrentUser();
echo $member->SayHi;
// "Hi Sam"
```
## Modifying Existing Methods
@ -123,7 +142,9 @@ through the [Object::extend()](api:Object::extend()) method.
**framework/security/Member.php**
:::php
```php
public function getValidator() {
// ..
@ -131,6 +152,7 @@ through the [Object::extend()](api:Object::extend()) method.
// ..
}
```
Extension Hooks can be located anywhere in the method and provide a point for any `Extension` instances to modify the
variables at that given point. In this case, the core function `getValidator` on the `Member` class provides an
@ -139,7 +161,9 @@ validator by defining the `updateValidator` method.
**mysite/code/extensions/MyMemberExtension.php**
:::php
```php
<?php
class MyMemberExtension extends DataExtension {
@ -151,6 +175,7 @@ validator by defining the `updateValidator` method.
$validator->addRequiredField('DateOfBirth');
}
}
```
<div class="info" markdown="1">
The `$validator` parameter is passed by reference, as it is an object.
@ -159,7 +184,9 @@ The `$validator` parameter is passed by reference, as it is an object.
Another common example of when you will want to modify a method is to update the default CMS fields for an object in an
extension. The `CMS` provides a `updateCMSFields` Extension Hook to tie into.
:::php
```php
<?php
class MyMemberExtension extends DataExtension {
@ -178,14 +205,16 @@ extension. The `CMS` provides a `updateCMSFields` Extension Hook to tie into.
$upload->setAllowedFileCategories('image/supported');
}
}
```
<div class="notice" markdown="1">
If you're providing a module or working on code that may need to be extended by other code, it should provide a *hook*
which allows an Extension to modify the results.
</div>
:::php
```php
public function Foo() {
$foo = // ..
@ -193,6 +222,7 @@ which allows an Extension to modify the results.
return $foo;
}
```
The convention for extension hooks is to provide an `update{$Function}` hook at the end before you return the result. If
you need to provide extension hooks at the beginning of the method use `before{..}`.
@ -202,7 +232,9 @@ you need to provide extension hooks at the beginning of the method use `before{.
In your [Extension](api:SilverStripe\Core\Extension) class you can only refer to the source object through the `owner` property on the class as
`$this` will refer to your `Extension` instance.
:::php
```php
<?php
class MyMemberExtension extends DataExtension {
@ -212,14 +244,14 @@ In your [Extension](api:SilverStripe\Core\Extension) class you can only refer to
var_dump($this->owner);
}
}
```
## Checking to see if an Object has an Extension
To see what extensions are currently enabled on an object, use [Object::getExtensionInstances()](api:Object::getExtensionInstances()) and
[Object::hasExtension()](api:Object::hasExtension())
```php
:::php
$member = Security::getCurrentUser();
print_r($member->getExtensionInstances());
@ -227,7 +259,7 @@ To see what extensions are currently enabled on an object, use [Object::getExten
if($member->hasExtension('MyCustomMemberExtension')) {
// ..
}
```
## Object extension injection points
@ -245,7 +277,9 @@ require that a callback is registered each time, if necessary.
Example: A class that wants to control default values during object initialization. The code needs to assign a value
if not specified in `self::$defaults`, but before extensions have been called:
:::php
```php
function __construct() {
$self = $this;
@ -257,6 +291,7 @@ if not specified in `self::$defaults`, but before extensions have been called:
parent::__construct();
}
```
Example 2: User code can intervene in the process of extending cms fields.
@ -264,7 +299,9 @@ Example 2: User code can intervene in the process of extending cms fields.
This method is preferred to disabling, enabling, and calling field extensions manually.
</div>
:::php
```php
public function getCMSFields() {
$this->beforeUpdateCMSFields(function($fields) {
@ -276,7 +313,7 @@ This method is preferred to disabling, enabling, and calling field extensions ma
// ... additional fields here
return $fields;
}
```
## Related Documentaion

View File

@ -11,18 +11,18 @@ In the CMS, authors often want to insert content elements which go beyond standa
in their WYSIWYG editor. Shortcodes are a semi-technical solution for this. A good example would be embedding a 3D file
viewer or a Google Map at a certain location.
```php
:::php
$text = "<h1>My Map</h1>[map]"
// Will output
// <h1>My Map</h1><iframe ..></iframe>
```
Here's some syntax variations:
```php
:::php
[my_shortcode]
#
[my_shortcode /]
@ -30,6 +30,7 @@ Here's some syntax variations:
[my_shortcode,myparameter="value"]
#
[my_shortcode,myparameter="value"]Enclosed Content[/my_shortcode]
```
Shortcodes are automatically parsed on any database field which is declared as [HTMLValue](api:SilverStripe\View\Parsers\HTMLValue) or [DBHTMLText](api:SilverStripe\ORM\FieldType\DBHTMLText),
when rendered into a template. This means you can use shortcodes on common fields like `SiteTree.Content`, and any
@ -37,9 +38,12 @@ other [DataObject::$db](api:SilverStripe\ORM\DataObject::$db) definitions of the
Other fields can be manually parsed with shortcodes through the `parse` method.
:::php
```php
$text = "My awesome [my_shortcode] is here.";
ShortcodeParser::get_active()->parse($text);
```
## Defining Custom Shortcodes
@ -47,7 +51,9 @@ First we need to define a callback for the shortcode.
**mysite/code/Page.php**
:::php
```php
<?php
class Page extends SiteTree {
@ -60,6 +66,7 @@ First we need to define a callback for the shortcode.
return "<em>" . $tagName . "</em> " . $content . "; " . count($arguments) . " arguments.";
}
}
```
These parameters are passed to the `MyShortCodeMethod` callback:
@ -77,11 +84,13 @@ To register a shortcode you call the following.
**mysite/_config.php**
:::php
```php
// ShortcodeParser::get('default')->register($shortcode, $callback);
ShortcodeParser::get('default')->register('my_shortcode', array('Page', 'MyShortCodeMethod'));
```
## Built-in Shortcodes
@ -93,13 +102,19 @@ Internal page links keep references to their database IDs rather than the URL, i
against moving the target page to a different location in the page tree. This is done through the `[sitetree_link]`
shortcode, which takes an `id` parameter.
:::php
```php
<a href="[sitetree_link,id=99]">
```
Links to internal `File` database records work exactly the same, but with the `[file_link]` shortcode.
:::php
```php
<a href="[file_link,id=99]">
```
### Images
@ -135,15 +150,16 @@ The first is called "element scope" use, the second "attribute scope"
You may not use shortcodes in any other location. Specifically, you can not use shortcodes to generate attributes or
change the name of a tag. These usages are forbidden:
```ss
<[paragraph]>Some test</[paragraph]>
<a [titleattribute]>link</a>
```
You may need to escape text inside attributes `>` becomes `&gt;`, You can include HTML tags inside a shortcode tag, but
you need to be careful of nesting to ensure you don't break the output.
:::ss
```ss
<!-- Good -->
<div>
[shortcode]
@ -159,34 +175,35 @@ you need to be careful of nesting to ensure you don't break the output.
<p>
[/shortcode]
</p>
```
### Location
Element scoped shortcodes have a special ability to move the location they are inserted at to comply with HTML lexical
rules. Take for example this basic paragraph tag:
```ss
<p><a href="#">Head [figure,src="assets/a.jpg",caption="caption"] Tail</a></p>
```
When converted naively would become:
```ss
<p><a href="#">Head <figure><img src="assets/a.jpg" /><figcaption>caption</figcaption></figure> Tail</a></p>
```
However this is not valid HTML - P elements can not contain other block level elements.
To fix this you can specify a "location" attribute on a shortcode. When the location attribute is "left" or "right"
the inserted content will be moved to immediately before the block tag. The result is this:
```ss
<figure><img src="assets/a.jpg" /><figcaption>caption</figcaption></figure><p><a href="#">Head Tail</a></p>
```
When the location attribute is "leftAlone" or "center" then the DOM is split around the element. The result is this:
```ss
<p><a href="#">Head </a></p><figure><img src="assets/a.jpg" /><figcaption>caption</figcaption></figure><p><a href="#"> Tail</a></p>
```
### Parameter values
Here is a summary of the callback parameter values based on some example shortcodes.
```php
:::php
public function MyCustomShortCode($arguments, $content = null, $parser = null, $tagName) {
// ..
}
@ -210,6 +227,7 @@ Here is a summary of the callback parameter values based on some example shortco
$enclosedContent => 'content'
$parser => ShortcodeParser instance
$tagName => 'my_shortcode'
```
## Limitations

View File

@ -18,35 +18,45 @@ Some of the goals of dependency injection are:
The following sums up the simplest usage of the `Injector` it creates a new object of type `MyClassName` through `create`
:::php
```php
$object = Injector::inst()->create('MyClassName');
```
The benefit of constructing objects through this syntax is `ClassName` can be swapped out using the
[Configuration API](../configuration) by developers.
**mysite/_config/app.yml**
:::yml
```yml
Injector:
MyClassName:
class: MyBetterClassName
```
Repeated calls to `create()` create a new object each time.
:::php
```php
$object = Injector::inst()->create('MyClassName');
$object2 = Injector::inst()->create('MyClassName');
echo $object !== $object2;
// returns true;
```
## Singleton Pattern
The `Injector` API can be used for the singleton pattern through `get()`. Subsequent calls to `get` return the same
object instance as the first call.
:::php
```php
// sets up MyClassName as a singleton
$object = Injector::inst()->get('MyClassName');
$object2 = Injector::inst()->get('MyClassName');
@ -54,12 +64,15 @@ object instance as the first call.
echo ($object === $object2);
// returns true;
```
## Dependencies
The `Injector` API can be used to define the types of `$dependencies` that an object requires.
:::php
```php
<?php
class MyController extends Controller {
@ -76,10 +89,13 @@ The `Injector` API can be used to define the types of `$dependencies` that an ob
'permissions' => '%$PermissionService',
);
}
```
When creating a new instance of `MyController` the dependencies on that class will be met.
:::php
```php
$object = Injector::inst()->get('MyController');
echo ($object->permissions instanceof PermissionService);
@ -87,22 +103,27 @@ When creating a new instance of `MyController` the dependencies on that class wi
echo (is_string($object->textProperty));
// returns true;
```
The [Configuration YAML](../configuration) does the hard work of configuring those `$dependencies` for us.
**mysite/_config/app.yml**
:::yml
```yml
Injector:
PermissionService:
class: MyCustomPermissionService
MyController
properties:
textProperty: 'My Text Value'
```
Now the dependencies will be replaced with our configuration.
:::php
```php
$object = Injector::inst()->get('MyController');
echo ($object->permissions instanceof MyCustomPermissionService);
@ -110,16 +131,19 @@ Now the dependencies will be replaced with our configuration.
echo ($object->textProperty == 'My Text Value');
// returns true;
```
As well as properties, method calls can also be specified:
:::yml
```yml
Injector:
Logger:
class: Monolog\Logger
calls:
- [ pushHandler, [ %$DefaultHandler ] ]
```
## Using constants as variables
@ -146,14 +170,19 @@ An example using the `MyFactory` service to create instances of the `MyService`
**mysite/_config/app.yml**
:::yml
```yml
Injector:
MyService:
factory: MyFactory
```
**mysite/code/MyFactory.php**
:::php
```php
<?php
class MyFactory implements SilverStripe\Core\Injector\Factory {
@ -165,6 +194,7 @@ An example using the `MyFactory` service to create instances of the `MyService`
// Will use MyFactoryImplementation::create() to create the service instance.
$instance = Injector::inst()->get('MyService');
```
## Dependency overrides
@ -187,7 +217,9 @@ runtime.
Assuming a class structure such as
:::php
```php
<?php
class RestrictivePermissionService {
@ -207,10 +239,13 @@ Assuming a class structure such as
$this->password = $password;
}
}
```
And the following configuration..
:::yml
```yml
name: MyController
---
MyController:
@ -225,12 +260,16 @@ And the following configuration..
constructor:
0: 'dbusername'
1: 'dbpassword'
```
Calling..
:::php
```php
// sets up ClassName as a singleton
$controller = Injector::inst()->get('MyController');
```
Would setup the following
@ -247,16 +286,15 @@ named services, which may not be actual classes, and thus should not behave as t
Thus if you want an object to have the injected dependencies of a service of another name, you must
assign a reference to that service.
```yaml
:::yaml
Injector:
JSONServiceDefinition:
class: JSONServiceImplementor
properties:
Serialiser: JSONSerialiser
GZIPJSONProvider: %$JSONServiceDefinition
```
`Injector::inst()->get('GZIPJSONProvider')` will then be an instance of `JSONServiceImplementor` with the injected
properties.
@ -284,7 +322,9 @@ which may be later discarded, reverting the application to the original state. T
This is useful when writing test cases, as certain services may be necessary to override for a single method call.
:::php
```php
// Setup default service
Injector::inst()->registerService(new LiveService(), 'ServiceName');
@ -297,7 +337,7 @@ This is useful when writing test cases, as certain services may be necessary to
// revert changes
Injector::unnest();
```
## API Documentation

View File

@ -45,7 +45,9 @@ used.
**mysite/code/MySQLWriteDbAspect.php**
:::php
```php
<?php
class MySQLWriteDbAspect implements BeforeCallAspect {
@ -59,7 +61,6 @@ used.
'insert','update','delete','replace'
);
public function beforeCall($proxied, $method, $args, &$alternateReturn) {
if (isset($args[0])) {
$sql = $args[0];
@ -72,13 +73,16 @@ used.
}
}
}
```
To actually make use of this class, a few different objects need to be configured. First up, define the `writeDb`
object that's made use of above.
**mysite/_config/app.yml**
:::yml
```yml
WriteMySQLDatabase:
class: MySQLDatabase
constructor:
@ -87,6 +91,7 @@ object that's made use of above.
username: user
password: pass
database: write_database
```
This means that whenever something asks the [Injector](api:SilverStripe\Core\Injector\Injector) for the `WriteMySQLDatabase` object, it'll receive an object
of type `MySQLDatabase`, configured to point at the 'write_database'.
@ -95,17 +100,19 @@ Next, this should be bound into an instance of the `Aspect` class
**mysite/_config/app.yml**
:::yml
```yml
MySQLWriteDbAspect:
properties:
writeDb: %$WriteMySQLDatabase
```
Next, we need to define the database connection that will be used for all non-write queries
**mysite/_config/app.yml**
```yml
:::yml
ReadMySQLDatabase:
class: MySQLDatabase
constructor:
@ -114,13 +121,14 @@ Next, we need to define the database connection that will be used for all non-wr
username: user
password: pass
database: read_database
```
The final piece that ties everything together is the [AopProxyService](api:SilverStripe\Core\Injector\AopProxyService) instance that will be used as the replacement
object when the framework creates the database connection.
**mysite/_config/app.yml**
```yml
:::yml
MySQLDatabase:
class: AopProxyService
properties:
@ -128,6 +136,7 @@ object when the framework creates the database connection.
beforeCall:
query:
- %$MySQLWriteDbAspect
```
The two important parts here are in the `properties` declared for the object.
@ -138,8 +147,8 @@ defined method\_name
Overall configuration for this would look as follows
**mysite/_config/app.yml**
```yml
:::yml
Injector:
ReadMySQLDatabase:
class: MySQLDatabase
@ -167,18 +176,19 @@ Overall configuration for this would look as follows
beforeCall:
query:
- %$MySQLWriteDbAspect
```
## Changing what a method returns
One major feature of an `Aspect` is the ability to modify what is returned from the client's call to the proxied method.
As seen in the above example, the `beforeCall` method modifies the `&$alternateReturn` variable, and returns `false`
after doing so.
```php
:::php
$alternateReturn = $this->writeDb->query($sql, $code);
return false;
```
By returning `false` from the `beforeCall()` method, the wrapping proxy class will_not_ call any additional `beforeCall`
handlers defined for the called method. Assigning the `$alternateReturn` variable also indicates to return that value

View File

@ -16,7 +16,9 @@ A basic usage of a module for 3.1 that requires the CMS would look similar to
this:
**mycustommodule/composer.json**
:::js
```js
{
"name": "your-vendor-name/module-name",
"description": "One-liner describing your module",
@ -42,7 +44,7 @@ this:
]
}
}
```
Once your module is published online with a service like Github.com or Bitbucket.com, submit the repository to
[Packagist](https://packagist.org/) to have the module accessible to developers. It'll automatically get picked

View File

@ -5,15 +5,18 @@ title: How to Create a Google Maps Shortcode
To demonstrate how easy it is to build custom shortcodes, we'll build one to display a Google Map based on a provided
address. We want our CMS authors to be able to embed the map using the following code:
:::php
```php
[googlemap,width=500,height=300]97-99 Courtenay Place, Wellington, New Zealand[/googlemap]
```
So we've got the address as "content" of our new `googlemap` shortcode tags, plus some `width` and `height` arguments.
We'll add defaults to those in our shortcode parser so they're optional.
**mysite/_config.php**
:::php
```php
ShortcodeParser::get('default')->register('googlemap', function($arguments, $address, $parser, $shortcode) {
$iframeUrl = sprintf(
'http://maps.google.com/maps?q=%s&amp;hnear=%s&amp;ie=UTF8&hq=&amp;t=m&amp;z=14&amp;output=embed',
@ -31,3 +34,4 @@ We'll add defaults to those in our shortcode parser so they're optional.
$iframeUrl
);
});
```

View File

@ -9,7 +9,9 @@ often the member has visited. Or more specifically,
how often he has started a browser session, either through
explicitly logging in or by invoking the "remember me" functionality.
:::php
```php
<?php
class MyMemberExtension extends DataExtension {
private static $db = array(
@ -42,11 +44,13 @@ explicitly logging in or by invoking the "remember me" functionality.
));
}
}
```
Now you just need to apply this extension through your config:
:::yml
```yml
Member:
extensions:
- MyMemberExtension
```

View File

@ -8,7 +8,9 @@ to ensure that it works as it should. A simple example would be to test the resu
**mysite/code/Page.php**
:::php
```php
<?php
use SilverStripe\CMS\Model\SiteTree;
@ -20,10 +22,13 @@ to ensure that it works as it should. A simple example would be to test the resu
return (1 + 1);
}
}
```
**mysite/tests/PageTest.php**
:::php
```php
<?php
use Page;
@ -36,6 +41,7 @@ to ensure that it works as it should. A simple example would be to test the resu
$this->assertEquals(2, Page::MyMethod());
}
}
```
<div class="info" markdown="1">
Tests for your application should be stored in the `mysite/tests` directory. Test cases for add-ons should be stored in
@ -84,7 +90,9 @@ needs.
**phpunit.xml**
:::xml
```xml
<phpunit bootstrap="framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>mysite/tests</directory>
@ -102,6 +110,7 @@ needs.
</exclude>
</groups>
</phpunit>
```
### setUp() and tearDown()
@ -109,7 +118,9 @@ In addition to loading data through a [Fixture File](fixtures), a test case may
run before each test method. For this, use the PHPUnit `setUp` and `tearDown` methods. These are run at the start and
end of each test.
:::php
```php
<?php
use SilverStripe\Core\Config\Config;
@ -142,12 +153,15 @@ end of each test.
// ..
}
}
```
`tearDownAfterClass` and `setUpBeforeClass` can be used to run code just once for the file rather than before and after
each individual test case. Remember to class the parent method in each method to ensure the core boot-strapping of tests
takes place.
:::php
```php
<?php
use SilverStripe\Dev\SapphireTest;
@ -168,6 +182,7 @@ takes place.
// ..
}
}
```
### Config and Injector Nesting
@ -179,7 +194,9 @@ If you need to make changes to `Config` (or `Injector`) for each test (or the wh
It's important to remember that the `parent::setUp();` functions will need to be called first to ensure the nesting feature works as expected.
:::php
```php
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
@ -197,6 +214,7 @@ It's important to remember that the `parent::setUp();` functions will need to be
{
Config::inst()->get('ClassName', 'var_name'); // this will be 'var_value'
}
```
## Related Documentation

View File

@ -8,37 +8,48 @@ core idea of these tests is the same as `SapphireTest` unit tests but `Functiona
creating [HTTPRequest](api:SilverStripe\Control\HTTPRequest), receiving [HTTPResponse](api:SilverStripe\Control\HTTPResponse) objects and modifying the current user session.
## Get
```php
:::php
$page = $this->get($url);
```
Performs a GET request on $url and retrieves the [HTTPResponse](api:SilverStripe\Control\HTTPResponse). This also changes the current page to the value
of the response.
## Post
```php
:::php
$page = $this->post($url);
```
Performs a POST request on $url and retrieves the [HTTPResponse](api:SilverStripe\Control\HTTPResponse). This also changes the current page to the value
of the response.
## Submit
:::php
```php
$submit = $this->submitForm($formID, $button = null, $data = array());
```
Submits the given form (`#ContactForm`) on the current page and returns the [HTTPResponse](api:SilverStripe\Control\HTTPResponse).
## LogInAs
:::php
```php
$this->logInAs($member);
```
Logs a given user in, sets the current session. To log all users out pass `null` to the method.
:::php
```php
$this->logInAs(null);
```
## Assertions
@ -46,10 +57,13 @@ The `FunctionalTest` class also provides additional asserts to validate your tes
### assertPartialMatchBySelector
:::php
```php
$this->assertPartialMatchBySelector('p.good',array(
'Test save was successful'
));
```
Asserts that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The
@ -58,21 +72,25 @@ assertion fails if one of the expectedMatches fails to appear.
### assertExactMatchBySelector
:::php
```php
$this->assertExactMatchBySelector("#MyForm_ID p.error", array(
"That email address is invalid."
));
```
Asserts that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The
assertion fails if one of the expectedMatches fails to appear.
### assertPartialHTMLMatchBySelector
```php
:::php
$this->assertPartialHTMLMatchBySelector("#MyForm_ID p.error", array(
"That email address is invalid."
));
```
Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The
@ -83,11 +101,12 @@ assertion fails if one of the expectedMatches fails to appear.
</div>
### assertExactHTMLMatchBySelector
```php
:::php
$this->assertExactHTMLMatchBySelector("#MyForm_ID p.error", array(
"That email address is invalid."
));
```
Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The

View File

@ -14,7 +14,9 @@ To include your fixture file in your tests, you should define it as your `$fixtu
**mysite/tests/MyNewTest.php**
:::php
```php
<?php
class MyNewTest extends SapphireTest {
@ -22,13 +24,16 @@ To include your fixture file in your tests, you should define it as your `$fixtu
protected static $fixture_file = 'fixtures.yml';
}
```
You can also use an array of fixture files, if you want to use parts of multiple other tests:
**mysite/tests/MyNewTest.php**
:::php
```php
<?php
class MyNewTest extends SapphireTest {
@ -39,13 +44,16 @@ You can also use an array of fixture files, if you want to use parts of multiple
);
}
```
Typically, you'd have a separate fixture file for each class you are testing - although overlap between tests is common.
Fixtures are defined in `YAML`. `YAML` is a markup language which is deliberately simple and easy to read, so it is
ideal for fixture generation. Say we have the following two DataObjects:
:::php
```php
<?php
use SilverStripe\ORM\DataObject;
@ -72,12 +80,15 @@ ideal for fixture generation. Say we have the following two DataObjects:
'Players' => 'Player'
);
}
```
We can represent multiple instances of them in `YAML` as follows:
**mysite/tests/fixtures.yml**
:::yml
```yml
Team:
hurricanes:
Name: The Hurricanes
@ -95,6 +106,7 @@ We can represent multiple instances of them in `YAML` as follows:
jack:
Name: Jack
Team: =>Team.crusaders
```
This `YAML` is broken up into three levels, signified by the indentation of each line. In the first level of
indentation, `Player` and `Team`, represent the class names of the objects we want to be created.
@ -102,8 +114,11 @@ indentation, `Player` and `Team`, represent the class names of the objects we wa
The second level, `john`/`joe`/`jack` & `hurricanes`/`crusaders`, are **identifiers**. Each identifier you specify
represents a new object and can be referenced in the PHP using `objFromFixture`
:::php
```php
$player = $this->objFromFixture('Player', 'jack');
```
The third and final level represents each individual object's fields.
@ -131,7 +146,9 @@ This style of relationship declaration can be used for any type of relationship
We can also declare the relationships conversely. Another way we could write the previous example is:
:::yml
```yml
Player:
john:
Name: John
@ -148,12 +165,15 @@ We can also declare the relationships conversely. Another way we could write the
Name: Crusaders
Origin: Canterbury
Players: =>Player.joe,=>Player.jack
```
The database is populated by instantiating `DataObject` objects and setting the fields declared in the `YAML`, then
calling `write()` on those objects. Take for instance the `hurricances` record in the `YAML`. It is equivalent to
writing:
:::php
```php
$team = new Team(array(
'Name' => 'Hurricanes',
'Origin' => 'Wellington'
@ -162,6 +182,7 @@ writing:
$team->write();
$team->Players()->add($john);
```
<div class="notice" markdown="1">
As the YAML fixtures will call `write`, any `onBeforeWrite()` or default value logic will be executed as part of the
@ -172,7 +193,9 @@ test.
As of SilverStripe 4 you will need to use fully qualfied class names in your YAML fixture files. In the above examples, they belong to the global namespace so there is nothing requires, but if you have a deeper DataObject, or it has a relationship to models that are part of the framework for example, you will need to include their namespaces:
:::yml
```yml
MyProject\Model\Player:
john:
Name: join
@ -182,6 +205,7 @@ As of SilverStripe 4 you will need to use fully qualfied class names in your YAM
Name: Crusaders
Origin: Canterbury
Players: =>MyProject\Model\Player.john
```
<div class="notice" markdown="1">
If your tests are failing and your database has table names that follow the fully qualified class names, you've probably forgotten to implement `private static $table_name = 'Player';` on your namespaced class. This property was introduced in SilverStripe 4 to reduce data migration work. See [DataObject](api:SilverStripe\ORM\DataObject) for an example.
@ -192,7 +216,9 @@ If your tests are failing and your database has table names that follow the full
`many_many` relations can have additional database fields attached to the relationship. For example we may want to
declare the role each player has in the team.
:::php
```php
use SilverStripe\ORM\DataObject;
class Player extends DataObject
@ -222,10 +248,13 @@ declare the role each player has in the team.
)
);
}
```
To provide the value for the `many_many_extraField` use the YAML list syntax.
:::yml
```yml
Player:
john:
Name: John
@ -247,6 +276,7 @@ To provide the value for the `many_many_extraField` use the YAML list syntax.
Role: Captain
- =>Player.jack:
Role: Winger
```
## Fixture Factories
@ -266,17 +296,23 @@ name, which is usually set to the class it creates such as `Member` or `Page`.
Blueprints are auto-created for all available DataObject subclasses, you only need to instantiate a factory to start
using them.
:::php
```php
$factory = Injector::inst()->create('FixtureFactory');
$obj = $factory->createObject('Team', 'hurricanes');
```
In order to create an object with certain properties, just add a third argument:
:::php
```php
$obj = $factory->createObject('Team', 'hurricanes', array(
'Name' => 'My Value'
));
```
<div class="warning" markdown="1">
It is important to remember that fixtures are referenced by arbitrary identifiers ('hurricanes'). These are internally
@ -285,26 +321,33 @@ mapped to their database identifiers.
After we've created this object in the factory, `getId` is used to retrieve it by the identifier.
:::php
$databaseId = $factory->getId('Team', 'hurricanes');
```php
$databaseId = $factory->getId('Team', 'hurricanes');
```
### Default Properties
Blueprints can be overwritten in order to customise their behavior. For example, if a Fixture does not provide a Team
name, we can set the default to be `Unknown Team`.
:::php
```php
$factory->define('Team', array(
'Name' => 'Unknown Team'
));
```
### Dependent Properties
Values can be set on demand through anonymous functions, which can either generate random defaults, or create composite
values based on other fixture data.
:::php
```php
$factory->define('Member', array(
'Email' => function($obj, $data, $fixtures) {
if(isset($data['FirstName']) {
@ -315,23 +358,29 @@ values based on other fixture data.
$obj->Score = rand(0,10);
}
));
```
### Relations
Model relations can be expressed through the same notation as in the YAML fixture format described earlier, through the
`=>` prefix on data values.
:::php
```php
$obj = $factory->createObject('Team', 'hurricanes', array(
'MyHasManyRelation' => '=>Player.john,=>Player.joe'
));
```
#### Callbacks
Sometimes new model instances need to be modified in ways which can't be expressed in their properties, for example to
publish a page, which requires a method call.
:::php
```php
$blueprint = Injector::inst()->create('FixtureBlueprint', 'Member');
$blueprint->addCallback('afterCreate', function($obj, $identifier, $data, $fixtures) {
@ -339,6 +388,7 @@ publish a page, which requires a method call.
});
$page = $factory->define('Page', $blueprint);
```
Available callbacks:
@ -351,7 +401,9 @@ Data of the same type can have variations, for example forum members vs. CMS adm
class, but have completely different properties. This is where named blueprints come in. By default, blueprint names
equal the class names they manage.
:::php
```php
$memberBlueprint = Injector::inst()->create('FixtureBlueprint', 'Member', 'Member');
$adminBlueprint = Injector::inst()->create('FixtureBlueprint', 'AdminMember', 'Member');
@ -366,6 +418,7 @@ equal the class names they manage.
$member = $factory->createObject('Member'); // not in admin group
$admin = $factory->createObject('AdminMember'); // in admin group
```
## Related Documentation

View File

@ -7,7 +7,9 @@ how you can load default records into the test database.
**mysite/tests/PageTest.php**
:::php
```php
<?php
use SilverStripe\Dev\SapphireTest;
@ -44,6 +46,7 @@ how you can load default records into the test database.
}
}
}
```
Firstly we define a static `$fixture_file`, this should point to a file that represents the data we want to test,
represented as a YAML [Fixture](../fixtures). When our test is run, the data from this file will be loaded into a test

View File

@ -9,7 +9,9 @@ response and modify the session within a test.
**mysite/tests/HomePageTest.php**
:::php
```php
<?php
class HomePageTest extends FunctionalTest {
@ -45,6 +47,7 @@ response and modify the session within a test.
));
}
}
```
## Related Documentation

View File

@ -8,7 +8,9 @@ see the [Fixtures](../fixtures) documentation.
In this how to we'll use a `FixtureFactory` and a custom blue print for giving us a shortcut for creating new objects
with information that we need.
:::php
```php
class MyObjectTest extends SapphireTest {
protected $factory;
@ -39,6 +41,7 @@ with information that we need.
// returns "My Custom Value"
}
}
```
## Related Documentation

View File

@ -6,7 +6,9 @@ SilverStripe's test system has built-in support for testing emails sent using th
running a [SapphireTest](api:SilverStripe\Dev\SapphireTest) test, then it holds off actually sending the email, and instead lets you assert that an
email was sent using this method.
:::php
```php
public function MyMethod() {
$e = new Email();
$e->To = "someone@example.com";
@ -14,15 +16,18 @@ email was sent using this method.
$e->Body = "I just really wanted to email you and say hi.";
$e->send();
}
```
To test that `MyMethod` sends the correct email, use the [SapphireTest::assertEmailSent()](api:SilverStripe\Dev\SapphireTest::assertEmailSent()) method.
:::php
```php
$this->assertEmailSent($to, $from, $subject, $body);
// to assert that the email is sent to the correct person
$this->assertEmailSent("someone@example.com", null, "/th.*e$/");
```
Each of the arguments (`$to`, `$from`, `$subject` and `$body`) can be either one of the following.

View File

@ -9,14 +9,17 @@ and behaviors. The environment is managed either through a [YML configuration fi
The definition of setting an environment type in a `mysite/_config/app.yml` looks like
:::yml
```yml
SilverStripe\Control\Director:
environment_type: 'dev'
```
The definition of setting an environment type in a `.env` file looks like
```
SS_ENVIRONMENT_TYPE="dev"
```
The three environment types you can set are `dev`, `test` and `live`.
### Dev
@ -38,13 +41,16 @@ Test mode is designed for staging environments or other private collaboration si
In this mode error messages are hidden from the user and SilverStripe includes [BasicAuth](api:SilverStripe\Security\BasicAuth) integration if you
want to password protect the site. You can enable that by adding this to your `mysite/_config/app.yml` file:
:::yml
```yml
---
Only:
environment: 'test'
---
SilverStripe\Security\BasicAuth:
entire_site_protected: true
```
### Live Mode
@ -60,6 +66,7 @@ Live sites should always run in live mode. You should not run production website
You can check for the current environment type in [config files](../configuration) through the `environment` variant.
**mysite/_config/app.yml**
```yml
---
Only:
environment: 'live'
@ -72,11 +79,11 @@ You can check for the current environment type in [config files](../configuratio
---
MyClass:
myvar: test_value
```
Checking for what environment you're running in can also be done in PHP. Your application code may disable or enable
certain functionality depending on the environment type.
```php
:::php
if (Director::isLive()) {
// is in live
} elseif (Director::isTest()) {
@ -84,4 +91,6 @@ certain functionality depending on the environment type.
} elseif (Director::isDev()) {
// is in dev mode
}
```

View File

@ -10,10 +10,11 @@ to track down a template or two. The template engine can help you along by displ
source code comments indicating which template is responsible for rendering each
block of html on your page.
:::yaml
```yaml
---
Only:
environment: 'dev'
---
SSViewer:
source_file_comments: true
```

View File

@ -4,13 +4,13 @@ summary: Cache SilverStripe templates to reduce database queries.
# Partial Caching
Partial caching is a feature that allows the caching of just a portion of a page.
```ss
:::ss
<% cached 'CacheKey' %>
$DataTable
...
<% end_cached %>
```
Each cache block has a cache key. A cache key is an unlimited number of comma separated variables and quoted strings.
Every time the cache key returns a different result, the contents of the block are recalculated. If the cache key is
@ -21,7 +21,9 @@ will invalidate the cache after a given amount of time has expired (default 10 m
Here are some more complex examples:
:::ss
```ss
<% cached 'database', $LastEdited %>
<!-- that updates every time the record changes. -->
<% end_cached %>
@ -33,6 +35,7 @@ Here are some more complex examples:
<% cached 'loginblock', $LastEdited, $CurrentMember.isAdmin %>
<!-- recached when block object changes, and if the user is admin -->
<% end_cached %>
```
An additional global key is incorporated in the cache lookup. The default value for this is
`$CurrentReadingMode, $CurrentUser.ID`. This ensures that the current [Versioned](api:SilverStripe\Versioned\Versioned) state and user ID are used.
@ -44,10 +47,12 @@ user does not influence your template content, you can update this key as below;
**mysite/_config/app.yml**
:::yaml
```yaml
SSViewer:
global_key: '$CurrentReadingMode, $Locale'
```
## Aggregates
@ -58,16 +63,22 @@ on sets of [DataObject](api:SilverStripe\ORM\DataObject)s - the most useful for
For example, if we have a menu, we want that menu to update whenever _any_ page is edited, but would like to cache it
otherwise. By using aggregates, we do that like this:
:::ss
```ss
<% cached 'navigation', $List('SiteTree').max('LastEdited'), $List('SiteTree').count() %>
```
The cache for this will update whenever a page is added, removed or edited.
If we have a block that shows a list of categories, we can make sure the cache updates every time a category is added
or edited
:::ss
```ss
<% cached 'categorylist', $List('Category').max('LastEdited'), $List('Category').count() %>
```
<div class="notice" markdown="1">
Note the use of both `.max('LastEdited')` and `.count()` - this takes care of both the case where an object has been
@ -85,7 +96,9 @@ fragment.
For example, a block that shows a collection of rotating slides needs to update whenever the relationship
`Page::$many_many = array('Slides' => 'Slide')` changes. In `PageController`:
:::php
```php
public function SliderCacheKey() {
$fragments = array(
@ -97,11 +110,15 @@ For example, a block that shows a collection of rotating slides needs to update
);
return implode('-_-', $fragments);
}
```
Then reference that function in the cache key:
:::ss
```ss
<% cached $SliderCacheKey %>
```
The example above would work for both a has_many and many_many relationship.
@ -119,24 +136,31 @@ data updates.
For instance, if we show some blog statistics, but are happy having them be slightly stale, we could do
:::ss
<% cached 'blogstatistics', $Blog.ID %>
```ss
<% cached 'blogstatistics', $Blog.ID %>
```
which will invalidate after the cache lifetime expires. If you need more control than that (cache lifetime is
configurable only on a site-wide basis), you could add a special function to your controller:
:::php
```php
public function BlogStatisticsCounter() {
return (int)(time() / 60 / 5); // Returns a new number every five minutes
}
```
and then use it in the cache key
:::ss
<% cached 'blogstatistics', $Blog.ID, $BlogStatisticsCounter %>
```ss
<% cached 'blogstatistics', $Blog.ID, $BlogStatisticsCounter %>
```
## Cache block conditionals
@ -147,17 +171,22 @@ value must be true for that block to be cached. Conversely if 'unless' is used,
Following on from the previous example, you might wish to only cache slightly-stale data if the server is experiencing
heavy load:
:::ss
<% cached 'blogstatistics', $Blog.ID if $HighLoad %>
```ss
<% cached 'blogstatistics', $Blog.ID if $HighLoad %>
```
By adding a `HighLoad` function to your `PageController`, you could enable or disable caching dynamically.
To cache the contents of a page for all anonymous users, but dynamically calculate the contents for logged in members,
use something like:
:::ss
```ss
<% cached unless $CurrentUser %>
```
## Uncached
@ -165,9 +194,11 @@ The template tag 'uncached' can be used - it is the exact equivalent of a cached
returns false. The key and conditionals in an uncached tag are ignored, so you can easily temporarily disable a
particular cache block by changing just the tag, leaving the key and conditional intact.
:::ss
<% uncached %>
```ss
<% uncached %>
```
## Nested cache blocks
@ -179,7 +210,9 @@ portion dynamic, without having to include any member info in the page's cache k
An example:
:::ss
```ss
<% cached $LastEdited %>
Our wonderful site
@ -189,7 +222,7 @@ An example:
$ASlowCalculation
<% end_cached %>
```
This will cache the entire outer section until the next time the page is edited, but will display a different welcome
message depending on the logged in member.
@ -197,7 +230,9 @@ message depending on the logged in member.
Cache conditionals and the uncached tag also work in the same nested manner. Since Member.Name is fast to calculate, you
could also write the last example as:
:::ss
```ss
<% cached $LastEdited %>
Our wonderful site
@ -207,6 +242,7 @@ could also write the last example as:
$ASlowCalculation
<% end_cached %>
```
<div class="warning" markdown="1">
Currently a nested cache block can not be contained within an if or loop block. The template engine will throw an error
@ -215,7 +251,9 @@ letting you know if you've done this. You can often get around this using aggreg
Failing example:
:::ss
```ss
<% cached $LastEdited %>
<% loop $Children %>
@ -225,10 +263,13 @@ Failing example:
<% end_loop %>
<% end_cached %>
```
Can be re-written as:
:::ss
```ss
<% cached $LastEdited %>
<% cached $AllChildren.max('LastEdited') %>
@ -238,10 +279,13 @@ Can be re-written as:
<% end_cached %>
<% end_cached %>
```
Or:
:::ss
```ss
<% cached $LastEdited %>
(other code)
<% end_cached %>
@ -251,6 +295,7 @@ Or:
$Name
<% end_cached %>
<% end_loop %>
```
## Cache expiry
@ -261,6 +306,11 @@ logic is sound, you could increase the expiry dramatically.
**mysite/_config.php**
:::php
```php
// Set partial cache expiry to 7 days
SS_Cache::set_cache_lifetime('cacheblock', 60 * 60 * 24 * 7);
```

View File

@ -31,20 +31,26 @@ but also exposes caches following the PSR-16 interface.
Cache objects are configured via YAML
and SilverStripe's [dependency injection](/developer-guides/extending/injector) system.
:::yml
```yml
SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.myCache:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "myCache"
```
Cache objects are instantiated through a [CacheFactory](SilverStripe\Core\Cache\CacheFactory),
which determines which cache adapter is used (see "Adapters" below for details).
This factory allows us you to globally define an adapter for all cache instances.
:::php
```php
use Psr\SimpleCache\CacheInterface
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
```
Caches are namespaced, which might allow granular clearing of a particular cache without affecting others.
In our example, the namespace is "myCache", expressed in the service name as
@ -59,10 +65,13 @@ service doesn't support this. See "Invalidation" for alternative strategies.
Cache objects follow the [PSR-16](http://www.php-fig.org/psr/psr-16/) class interface.
:::php
```php
use Psr\SimpleCache\CacheInterface
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
// create a new item by trying to get it from the cache
$myValue = $cache->get('myCacheKey');
@ -73,6 +82,7 @@ Cache objects follow the [PSR-16](http://www.php-fig.org/psr/psr-16/) class inte
if (!$cache->has('myCacheKey')) {
// ... item does not exists in the cache
}
```
## Invalidation
@ -80,30 +90,40 @@ Caches can be invalidated in different ways. The easiest is to actively clear th
entire cache. If the adapter supports namespaced cache clearing,
this will only affect a subset of cache keys ("myCache" in this example):
:::php
```php
use Psr\SimpleCache\CacheInterface
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
// remove all items in this (namespaced) cache
$cache->clear();
```
You can also delete a single item based on it's cache key:
:::php
```php
use Psr\SimpleCache\CacheInterface
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
// remove the cache item
$cache->delete('myCacheKey');
```
Individual cache items can define a lifetime, after which the cached value is marked as expired:
:::php
```php
use Psr\SimpleCache\CacheInterface
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
// remove the cache item
$cache->set('myCacheKey', 'myValue', 300); // cache for 300 seconds
```
If a lifetime isn't defined on the `set()` call, it'll use the adapter default.
In order to increase the chance of your cache actually being hit,
@ -112,11 +132,14 @@ You can also set your lifetime to `0`, which means they won't expire.
Since many adapters don't have a way to actively remove expired caches,
you need to be careful with resources here (e.g. filesystem space).
:::yml
```yml
SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.cacheblock:
constructor:
defaultLifetime: 3600
```
In most cases, invalidation and expiry should be handled by your cache key.
For example, including the `LastEdited` value when caching `DataObject` results
@ -125,13 +148,16 @@ The following example caches a member's group names, and automatically
creates a new cache key when any group is edited. Depending on the used adapter,
old cache keys will be garbage collected as the cache fills up.
:::php
```php
use Psr\SimpleCache\CacheInterface
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
// Automatically changes when any group is edited
$cacheKey = implode(['groupNames', $member->ID, Groups::get()->max('LastEdited')]);
$cache->set($cacheKey, $member->Groups()->column('Title'));
```
If `?flush=1` is requested in the URL, this will trigger a call to `flush()` on
any classes that implement the [Flushable](/developer_guides/execution_pipeline/flushable/)
@ -163,7 +189,9 @@ Example: Configure core caches to use [memcached](http://www.danga.com/memcached
which requires the [memcached PHP extension](http://php.net/memcached),
and takes a `MemcachedClient` instance as a constructor argument.
:::yml
```yml
---
After:
- '#corecache'
@ -177,6 +205,7 @@ and takes a `MemcachedClient` instance as a constructor argument.
class: 'SilverStripe\Core\Cache\MemcachedCacheFactory'
constructor:
client: '%$MemcachedClient
```
## Additional Caches

View File

@ -17,17 +17,21 @@ headers:
## Customizing Cache Headers
### HTTP::set_cache_age
```php
:::php
HTTP::set_cache_age(0);
```
Used to set the max-age component of the cache-control line, in seconds. Set it to 0 to disable caching; the "no-cache"
clause in `Cache-Control` and `Pragma` will be included.
### HTTP::register_modification_date
:::php
```php
HTTP::register_modification_date('2014-10-10');
```
Used to set the modification date to something more recent than the default. [DataObject::__construct](api:SilverStripe\ORM\DataObject::__construct) calls
[HTTP::register_modification_date(](api:SilverStripe\Control\HTTP::register_modification_date() whenever a record comes from the database ensuring the newest date is present.

View File

@ -23,7 +23,8 @@ SilverStripe can request more resources through `Environment::increaseMemoryLimi
`Environment::increaseTimeLimitTo()` functions.
</div>
:::php
```php
public function myBigFunction() {
Environment::increaseTimeLimitTo(400);
}
```

View File

@ -15,26 +15,30 @@ The [Member](api:SilverStripe\Security\Member) class comes with 2 static methods
Retrieves the ID (int) of the current logged in member. Returns *0* if user is not logged in. Much lighter than the
next method for testing if you just need to test.
:::php
```php
// Is a member logged in?
if( Member::currentUserID() ) {
// Yes!
} else {
// No!
}
```
**Security::getCurrentUser()**
Returns the full *Member* Object for the current user, returns *null* if user is not logged in.
:::php
```php
if( $member = Security::getCurrentUser() ) {
// Work with $member
} else {
// Do non-member stuff
}
```
## Subclassing
@ -45,20 +49,25 @@ This is the least desirable way of extending the [Member](api:SilverStripe\Secur
You can define subclasses of [Member](api:SilverStripe\Security\Member) to add extra fields or functionality to the built-in membership system.
:::php
```php
class MyMember extends Member {
private static $db = array(
"Age" => "Int",
"Address" => "Text",
);
}
```
To ensure that all new members are created using this class, put a call to [Object::useCustomClass()](api:Object::useCustomClass()) in
(project)/_config.php:
:::php
```php
Object::useCustomClass("Member", "MyMember");
```
Note that if you want to look this class-name up, you can call Object::getCustomClass("Member")
@ -68,7 +77,9 @@ If you overload the built-in public function getCMSFields(), then you can change
details in the newsletter system. This function returns a [FieldList](api:SilverStripe\Forms\FieldList) object. You should generally start by calling
parent::getCMSFields() and manipulate the [FieldList](api:SilverStripe\Forms\FieldList) from there.
:::php
```php
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->insertBefore("HTMLEmail", new TextField("Age"));
@ -76,7 +87,7 @@ parent::getCMSFields() and manipulate the [FieldList](api:SilverStripe\Forms\Fie
$fields->removeByName("Organisation");
return $fields;
}
```
## Extending Member or DataObject?
@ -93,16 +104,21 @@ 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. Add the following to your
`[config.yml](/developer_guides/configuration/configuration/#configuration-yaml-syntax-and-rules)`.
:::yml
```yml
Member:
extensions:
- MyMemberExtension
```
A role extension is simply a subclass of [DataExtension](api:SilverStripe\ORM\DataExtension) that is designed to be used to add behaviour to [Member](api:SilverStripe\Security\Member).
The roles affect the entire class - all members will get the additional behaviour. However, if you want to restrict
things, you should add appropriate [Permission::checkMember()](api:SilverStripe\Security\Permission::checkMember()) calls to the role's methods.
:::php
```php
class MyMemberExtension extends DataExtension {
/**
@ -126,6 +142,7 @@ things, you should add appropriate [Permission::checkMember()](api:SilverStripe\
// You can add any other methods you like, which you can call directly on the member object.
}
}
```
## Saved User Logins ##
@ -153,7 +170,9 @@ reasonably be expected to be allowed to do.
E.g.
:::php
```php
class CleanRecordsTask extends BuildTask
{
public function run($request)
@ -166,7 +185,7 @@ E.g.
DataRecord::get()->filter('Dirty', true)->removeAll();
});
}
```
## API Documentation

View File

@ -38,11 +38,11 @@ CMS access for the first time. SilverStripe provides a default admin configurati
and password to be configured for a single special user outside of the normal membership system.
It is advisable to configure this user in your `.env` file inside of the web root, as below:
```
# Configure a default username and password to access the CMS on all sites in this environment.
SS_DEFAULT_ADMIN_USERNAME="admin"
SS_DEFAULT_ADMIN_PASSWORD="password"
```
When a user logs in with these credentials, then a [Member](api:SilverStripe\Security\Member) with the Email 'admin' will be generated in
the database, but without any password information. This means that the password can be reset or changed by simply
updating the `.env` file.

View File

@ -25,8 +25,8 @@ must still be taken when working with literal values or table/column identifiers
come from user input.
Example:
```php
:::php
$records = DB::prepared_query('SELECT * FROM "MyClass" WHERE "ID" = ?', array(3));
$records = MyClass::get()->where(array('"ID" = ?' => 3));
$records = MyClass::get()->where(array('"ID"' => 3));
@ -34,10 +34,13 @@ Example:
$records = DataObject::get_one('MyClass', array('"ID" = ?' => 3));
$records = MyClass::get()->byID(3);
$records = SQLSelect::create()->addWhere(array('"ID"' => 3))->execute();
```
Parameterised updates and inserts are also supported, but the syntax is a little different
:::php
```php
SQLInsert::create('"MyClass"')
->assign('"Name"', 'Daniel')
->addAssignments(array(
@ -52,7 +55,7 @@ Parameterised updates and inserts are also supported, but the syntax is a little
'INSERT INTO "MyClass" ("Name", "Position", "Age", "Created") VALUES(?, ?, GREATEST(0,?,?), NOW())'
array('Daniel', 'Accountant', 24, 28)
);
```
### Automatic escaping
@ -76,8 +79,8 @@ Data is not escaped when writing to object-properties, as inserts and updates ar
handled via prepared statements.
Example:
```php
:::php
// automatically escaped/quoted
$members = Member::get()->filter('Name', $_GET['name']);
// automatically escaped/quoted
@ -86,6 +89,7 @@ Example:
$members = Member::get()->where(array('"Name" = ?' => $_GET['name']));
// needs to be escaped and quoted manually (note raw2sql called with the $quote parameter set to true)
$members = Member::get()->where(sprintf('"Name" = %s', Convert::raw2sql($_GET['name'], true)));
```
<div class="warning" markdown='1'>
It is NOT good practice to "be sure" and convert the data passed to the functions above manually. This might
@ -108,7 +112,9 @@ and [datamodel](/developer_guides/model) for ways to parameterise, cast, and con
Example:
:::php
```php
class MyForm extends Form {
public function save($RAW_data, $form) {
// Pass true as the second parameter of raw2sql to quote the value safely
@ -117,14 +123,16 @@ Example:
// ...
}
}
```
* `FormField->Value()`
* URLParams passed to a Controller-method
Example:
:::php
```php
class MyController extends Controller {
private static $allowed_actions = array('myurlaction');
public function myurlaction($RAW_urlParams) {
@ -134,13 +142,15 @@ Example:
// ...
}
}
```
As a rule of thumb, you should escape your data **as close to querying as possible**
(or preferably, use parameterised queries). This means if you've got a chain of functions
passing data through, escaping should happen at the end of the chain.
:::php
```php
class MyController extends Controller {
/**
* @param array $RAW_data All names in an indexed array (not SQL-safe)
@ -155,6 +165,7 @@ passing data through, escaping should happen at the end of the chain.
DB::query("UPDATE Player SET Name = {$SQL_name}");
}
}
```
This might not be applicable in all cases - especially if you are building an API thats likely to be customised. If
you're passing unescaped data, make sure to be explicit about it by writing *phpdoc*-documentation and *prefixing* your
@ -188,9 +199,9 @@ stripped out
To enable filtering, set the HtmlEditorField::$sanitise_server_side [configuration](/developer_guides/configuration/configuration) property to
true, e.g.
```
HtmlEditorField::config()->sanitise_server_side = true
```
The built in sanitiser enforces the TinyMCE whitelist rules on the server side, and is sufficient to eliminate the
most common XSS vectors.
@ -218,23 +229,27 @@ object-properties by [casting](/developer_guides/model/data_types_and_casting) i
PHP:
:::php
```php
class MyObject extends DataObject {
private static $db = array(
'MyEscapedValue' => 'Text', // Example value: <b>not bold</b>
'MyUnescapedValue' => 'HTMLText' // Example value: <b>bold</b>
);
}
```
Template:
:::php
```php
<ul>
<li>$MyEscapedValue</li> // output: &lt;b&gt;not bold&lt;b&gt;
<li>$MyUnescapedValue</li> // output: <b>bold</b>
</ul>
```
The example below assumes that data wasn't properly filtered when saving to the database, but are escaped before
outputting through SSViewer.
@ -246,7 +261,9 @@ You can force escaping on a casted value/object by using an [escape type](/devel
Template (see above):
:::php
```php
<ul>
// output: <a href="#" title="foo &amp; &#quot;bar&quot;">foo &amp; "bar"</a>
<li><a href="#" title="$Title.ATT">$Title</a></li>
@ -254,7 +271,7 @@ Template (see above):
<li>$MyUnescapedValue</li> // output: <b>bold</b>
<li>$MyUnescapedValue.XML</li> // output: &lt;b&gt;bold&lt;b&gt;
</ul>
```
### Escaping custom attributes and getters
@ -263,7 +280,9 @@ static *$casting* array. Caution: Casting only applies when using values in a te
PHP:
:::php
```php
class MyObject extends DataObject {
public $Title = '<b>not bold</b>'; // will be escaped due to Text casting
@ -277,17 +296,19 @@ PHP:
return $this->Title . '<small>(' . $suffix. ')</small>';
}
}
```
Template:
:::php
```php
<ul>
<li>$Title</li> // output: &lt;b&gt;not bold&lt;b&gt;
<li>$Title.RAW</li> // output: <b>not bold</b>
<li>$TitleWithHTMLSuffix</li> // output: <b>not bold</b>: <small>(...)</small>
</ul>
```
Note: Avoid generating HTML by string concatenation in PHP wherever possible to minimize risk and separate your
presentation from business logic.
@ -302,7 +323,9 @@ also used by *XML* and *ATT* in template code).
PHP:
:::php
```php
class MyController extends Controller {
private static $allowed_actions = array('search');
public function search($request) {
@ -313,13 +336,15 @@ PHP:
));
}
}
```
Template:
:::php
<h2 title="Searching for $Query.ATT">$HTMLTitle</h2>
```php
<h2 title="Searching for $Query.ATT">$HTMLTitle</h2>
```
Whenever you insert a variable into an HTML attribute within a template, use $VarName.ATT, no not $VarName.
@ -332,7 +357,9 @@ user data, not *Convert::raw2att()*. Use raw ampersands in your URL, and cast t
PHP:
:::php
```php
class MyController extends Controller {
private static $allowed_actions = array('search');
public function search($request) {
@ -343,13 +370,15 @@ PHP:
));
}
}
```
Template:
:::php
<a href="$RSSLink.ATT">RSS feed</a>
```php
<a href="$RSSLink.ATT">RSS feed</a>
```
Some rules of thumb:
@ -395,7 +424,9 @@ passed, such as *mysite.com/home/add/dfsdfdsfd*, then it returns 0.
Below is an example with different ways you would use this casting technique:
:::php
```php
public function CaseStudies() {
// cast an ID from URL parameters e.g. (mysite.com/home/action/ID)
@ -410,7 +441,7 @@ Below is an example with different ways you would use this casting technique:
// perform a byID(), which ensures the ID is an integer before querying
return CaseStudy::get()->byID($categoryID);
}
```
The same technique can be employed anywhere in your PHP code you know something must be of a certain type. A list of PHP
cast types can be found here:
@ -441,13 +472,13 @@ with a `.yml` or `.yaml` extension through the default web server rewriting rule
If you need users to access files with this extension,
you can bypass the rules for a specific directory.
Here's an example for a `.htaccess` file used by the Apache web server:
```
<Files *.yml>
Order allow,deny
Allow from all
</Files>
```
### User uploaded files
Certain file types are by default excluded from user upload. html, xhtml, htm, and xml files may have embedded,
@ -491,12 +522,15 @@ So in addition to storing the password in a secure fashion,
you can also enforce specific password policies by configuring
a [PasswordValidator](api:SilverStripe\Security\PasswordValidator):
:::php
```php
$validator = new PasswordValidator();
$validator->minLength(7);
$validator->checkHistoricalPasswords(6);
$validator->characterStrength(3, array("lowercase", "uppercase", "digits", "punctuation"));
Member::set_password_validator($validator);
```
In addition, you can tighten password security with the following configuration settings:
@ -518,14 +552,16 @@ included in HTML "frame" or "iframe" elements, and thereby prevent the most comm
attack vector. This is done through a HTTP header, which is usually added in your
controller's `init()` method:
:::php
```php
class MyController extends Controller {
public function init() {
parent::init();
$this->getResponse()->addHeader('X-Frame-Options', 'SAMEORIGIN');
}
}
```
This is a recommended option to secure any controller which displays
or submits sensitive user input, and is enabled by default in all CMS controllers,
@ -537,9 +573,9 @@ To prevent a forged hostname appearing being used by the application, SilverStri
allows the configure of a whitelist of hosts that are allowed to access the system. By defining
this whitelist in your `.env` file, any request presenting a `Host` header that is
_not_ in this list will be blocked with a HTTP 400 error:
```
SS_ALLOWED_HOSTS="www.mysite.com,mysite.com,subdomain.mysite.com"
```
Please note that if this configuration is defined, you _must_ include _all_ subdomains (eg www.)
that will be accessing the site.
@ -554,23 +590,25 @@ into visiting external sites.
In order to prevent this kind of attack, it's necessary to whitelist trusted proxy
server IPs using the SS_TRUSTED_PROXY_IPS define in your `.env`.
```
SS_TRUSTED_PROXY_IPS="127.0.0.1,192.168.0.1"
```
If you wish to change the headers that are used to find the proxy information, you should reconfigure the
TrustedProxyMiddleware service:
:::yml
```yml
SilverStripe\Control\TrustedProxyMiddleware:
properties:
ProxyHostHeaders: X-Forwarded-Host
ProxySchemeHeaders: X-Forwarded-Protocol
ProxyIPHeaders: X-Forwarded-Ip
SS_TRUSTED_PROXY_HOST_HEADER="HTTP_X_FORWARDED_HOST"
SS_TRUSTED_PROXY_IP_HEADER="HTTP_X_FORWARDED_FOR"
SS_TRUSTED_PROXY_PROTOCOL_HEADER="HTTP_X_FORWARDED_PROTOCOL"
```
At the same time, you'll also need to define which headers you trust from these proxy IPs. Since there are multiple ways through which proxies can pass through HTTP information on the original hostname, IP and protocol, these values need to be adjusted for your specific proxy. The header names match their equivalent `$_SERVER` values.
@ -582,14 +620,14 @@ This behaviour is enabled whenever `SS_TRUSTED_PROXY_IPS` is defined, or if the
`BlockUntrustedIPs` environment variable is declared. It is advisable to include the
following in your .htaccess to ensure this behaviour is activated.
```
<IfModule mod_env.c>
# Ensure that X-Forwarded-Host is only allowed to determine the request
# hostname for servers ips defined by SS_TRUSTED_PROXY_IPS in your .env
# Note that in a future release this setting will be always on.
SetEnv BlockUntrustedIPs true
</IfModule>
```
In a future release this behaviour will be changed to be on by default, and this environment
variable will be no longer necessary, thus it will be necessary to always set
@ -599,12 +637,13 @@ variable will be no longer necessary, thus it will be necessary to always set
SilverStripe recommends the use of TLS(HTTPS) for your application, and you can easily force the use through the
director function `forceSSL()`
```php
:::php
if (!Director::isDev()) {
Director::forceSSL();
}
```
Forcing HTTPS so requires a certificate to be purchased or obtained through a vendor such as
[lets encrypt](https://letsencrypt.org/) and configured on your web server.
@ -612,10 +651,11 @@ Forcing HTTPS so requires a certificate to be purchased or obtained through a ve
We also want to ensure cookies are not shared between secure and non-secure sessions, so we must tell SilverStripe to
use a [secure session](https://docs.silverstripe.org/en/3/developer_guides/cookies_and_sessions/sessions/#secure-session-cookie).
To do this, you may set the `cookie_secure` parameter to `true` in your `config.yml` for `Session`
```yml
:::yml
Session:
cookie_secure: true
```
For other cookies set by your application we should also ensure the users are provided with secure cookies by setting
the "Secure" and "HTTPOnly" flags. These flags prevent them from being stolen by an attacker through javascript.
@ -627,13 +667,13 @@ clear text and can be intercepted and stolen by an attacker who is listening on
- The `HTTPOnly` flag lets the browser know whether or not a cookie should be accessible by client-side JavaScript
code. It is best practice to set this flag unless the application is known to use JavaScript to access these cookies
as this prevents an attacker who achieves cross-site scripting from accessing these cookies.
```php
:::php
Cookie::set('cookie-name', 'chocolate-chip', $expiry = 30, $path = null, $domain = null, $secure = true,
$httpOnly = false
);
```
## Security Headers
@ -656,10 +696,9 @@ For sensitive pages, such as members areas, or places where sensitive informatio
and `Date: <current date>` will ensure that sensitive content is not stored locally or able to be retrieved by
unauthorised local persons. SilverStripe adds the current date for every request, and we can add the other cache
headers to the request for our secure controllers:
```php
:::php
class MySecureController extends Controller {
public function init() {
@ -676,6 +715,7 @@ unauthorised local persons. SilverStripe adds the current date for every request
$this->response->addHeader('Strict-Transport-Security', 'max-age=86400; includeSubDomains');
}
}
```
## Related

View File

@ -22,9 +22,12 @@ SilverStripe\Core\Injector\Injector:
### Sending plain text only
:::php
```php
$email = new Email($from, $to, $subject, $body);
$email->sendPlain();
```
### Sending combined HTML and plain text
@ -32,9 +35,12 @@ By default, emails are sent in both HTML and Plaintext format. A plaintext repre
from the system by stripping HTML markup, or transforming it where possible (e.g. `<strong>text</strong>` is converted
to `*text*`).
:::php
```php
$email = new Email($from, $to, $subject, $body);
$email->send();
```
<div class="info" markdown="1">
The default HTML template for emails is named `GenericEmail` and is located in `framework/templates/SilverStripe/Email/`.
@ -50,9 +56,12 @@ email object additional information using the `setData` and `addData` methods.
**mysite/templates/Email/MyCustomEmail.ss**
:::ss
```ss
<h1>Hi $Member.FirstName</h1>
<p>You can go to $Link.</p>
```
The PHP Logic..
@ -97,10 +106,13 @@ You can set the default sender address of emails through the `Email.admin_email`
**mysite/_config/app.yml**
:::yaml
```yaml
SilverStripe\Control\Email\Email:
admin_email: support@silverstripe.org
```
<div class="alert" markdown="1">
Remember, setting a `from` address that doesn't come from your domain (such as the users email) will likely see your
@ -122,12 +134,15 @@ Configuration of those properties looks like the following:
**mysite/_config.php**
:::php
```php
if(Director::isLive()) {
Config::inst()->update('Email', 'bcc_all_emails_to', "client@example.com");
} else {
Config::inst()->update('Email', 'send_all_emails_to', "developer@example.com");
}
```
### Setting custom "Reply To" email address.
@ -135,20 +150,24 @@ For email messages that should have an email address which is replied to that ac
email, do the following. This is encouraged especially when the domain responsible for sending the message isn't
necessarily the same which should be used for return correspondence and should help prevent your message from being
marked as spam.
```php
:::php
$email = new Email(..);
$email->setReplyTo('me@address.com');
```
### Setting Custom Headers
For email headers which do not have getters or setters (like setTo(), setFrom()) you can manipulate the underlying
`Swift_Message` that we provide a wrapper for.
:::php
```php
$email = new Email(...);
$email->getSwiftMessage()->getHeaders()->addTextHeader('HeaderName', 'HeaderValue');
..
```
<div class="info" markdown="1">
See this [Wikipedia](http://en.wikipedia.org/wiki/E-mail#Message_header) entry for a list of header names.

View File

@ -27,16 +27,19 @@ Feature overview:
You can use the CsvBulkLoader without subclassing or other customizations, if the column names
in your CSV file match `$db` properties in your dataobject. E.g. a simple import for the
[Member](api:SilverStripe\Security\Member) class could have this data in a file:
```
FirstName,LastName,Email
Donald,Duck,donald@disney.com
Daisy,Duck,daisy@disney.com
```
The loader would be triggered through the `load()` method:
:::php
```php
$loader = new CsvBulkLoader('Member');
$result = $loader->load('<my-file-path>');
```
By the way, you can import [Member](api:SilverStripe\Security\Member) and [Group](api:SilverStripe\Security\Group) data through `http://localhost/admin/security`
interface out of the box.
@ -45,7 +48,9 @@ interface out of the box.
The simplest way to use [CsvBulkLoader](api:SilverStripe\Dev\CsvBulkLoader) is through a [ModelAdmin](api:SilverStripe\Admin\ModelAdmin) interface - you get an upload form out of the box.
:::php
```php
<?php
class PlayerAdmin extends ModelAdmin {
private static $managed_models = array(
@ -57,6 +62,7 @@ The simplest way to use [CsvBulkLoader](api:SilverStripe\Dev\CsvBulkLoader) is t
private static $url_segment = 'players';
}
?>
```
The new admin interface will be available under `http://localhost/admin/players`, the import form is located
below the search form on the left.
@ -68,7 +74,9 @@ Let's create a simple upload form (which is used for `MyDataObject` instances).
You'll need to add a route to your controller to make it accessible via URL
(see [director](/reference/director)).
:::php
```php
<?php
class MyController extends Controller {
@ -108,6 +116,7 @@ You'll need to add a route to your controller to make it accessible via URL
return $this->redirectBack();
}
}
```
Note: This interface is not secured, consider using [Permission::check()](api:SilverStripe\Security\Permission::check()) to limit the controller to users
with certain access rights.
@ -117,16 +126,18 @@ with certain access rights.
We're going to use our knowledge from the previous example to import a more sophisticated CSV file.
Sample CSV Content
```
"Number","Name","Birthday","Team"
11,"John Doe",1982-05-12,"FC Bayern"
12,"Jane Johnson", 1982-05-12,"FC Bayern"
13,"Jimmy Dole",,"Schalke 04"
```
Datamodel for Player
:::php
```php
<?php
class Player extends DataObject {
private static $db = array(
@ -140,11 +151,13 @@ Datamodel for Player
);
}
?>
```
Datamodel for FootballTeam:
:::php
```php
<?php
class FootballTeam extends DataObject {
private static $db = array(
@ -155,7 +168,7 @@ Datamodel for FootballTeam:
);
}
?>
```
Sample implementation of a custom loader. Assumes a CSV-file in a certain format (see below).
@ -163,9 +176,8 @@ Sample implementation of a custom loader. Assumes a CSV-file in a certain format
* Splits a combined "Name" fields from the CSV-data into `FirstName` and `Lastname` by a custom importer method
* Avoids duplicate imports by a custom `$duplicateChecks` definition
* Creates `Team` relations automatically based on the `Gruppe` column in the CSV data
```php
:::php
<?php
class PlayerCsvBulkLoader extends CsvBulkLoader {
public $columnMap = array(
@ -194,10 +206,13 @@ Sample implementation of a custom loader. Assumes a CSV-file in a certain format
}
}
?>
```
Building off of the ModelAdmin example up top, use a custom loader instead of the default loader by adding it to `$model_importers`. In this example, `CsvBulkLoader` is replaced with `PlayerCsvBulkLoader`.
:::php
```php
<?php
class PlayerAdmin extends ModelAdmin {
private static $managed_models = array(
@ -209,7 +224,7 @@ Building off of the ModelAdmin example up top, use a custom loader instead of th
private static $url_segment = 'players';
}
?>
```
## Related

View File

@ -19,7 +19,9 @@ in the template.
**mysite/code/Page.php**
:::php
```php
public function getWellingtonWeather() {
$fetch = new RestfulService(
'https://query.yahooapis.com/v1/public/yql'
@ -48,18 +50,24 @@ in the template.
return $output;
}
```
## Features
### Basic Authenication
:::php
```php
$service = new RestfulService("http://example.harvestapp.com");
$service->basicAuth('username', 'password');
```
### Make multiple requests
:::php
```php
$service = new RestfulService("http://example.harvestapp.com");
$peopleXML = $service->request('/people');
@ -69,44 +77,55 @@ in the template.
$taskXML = $service->request('/tasks');
$tasks = $service->getValues($taskXML, 'task');
```
### Caching
To set the cache interval you can pass it as the 2nd argument to constructor.
:::php
```php
$expiry = 60 * 60; // 1 hour;
$request = new RestfulService("http://example.harvestapp.com", $expiry );
```
### Getting Values & Attributes
You can traverse through document tree to get the values or attribute of a particular node using XPath. Take for example
the following XML that is returned.
:::xml
```xml
<entries>
<entry id='12'>Sally</entry>
<entry id='15'>Ted</entry>
<entry id='30'>Matt</entry>
<entry id='22'>John</entry>
</entries>
```
To extract the id attributes of the entries use:
:::php
```php
$this->getAttributes($xml, "entries", "entry");
// array(array('id' => 12), array('id' => '15'), ..)
```
To extract the values (the names) of the entries use:
:::php
```php
$this->getValues($xml, "entries", "entry");
// array('Sally', 'Ted', 'Matt', 'John')
```
### Searching for Values & Attributes
@ -116,18 +135,23 @@ If you don't know the exact position of DOM tree where the node will appear you
This is the recommended method for retrieving values of name spaced nodes.
</div>
:::xml
```xml
<media:guide>
<media:entry id="2030">video</media:entry>
</media:guide>
```
To get the value of entry node with the namespace media, use:
:::php
```php
$this->searchValue($response, "//media:guide/media:entry");
// array('video');
```
## Best Practices
@ -137,7 +161,9 @@ If the web service returned an error (for example, API key not available or inad
[RestfulService](api:RestfulService) can delegate the error handling to it's descendant class. To handle the errors, subclass
`RestfulService and define a function called errorCatch.
:::php
```php
<?php
class MyRestfulService extends RestfulService {
@ -152,10 +178,13 @@ If the web service returned an error (for example, API key not available or inad
return $response;
}
}
```
If you want to bypass error handling, define `checkErrors` in the constructor for `RestfulService`
:::php
```php
<?php
class MyRestfulService extends RestfulService {
@ -166,7 +195,7 @@ If you want to bypass error handling, define `checkErrors` in the constructor fo
$this->checkErrors = false;
}
}
```
### Setting cURL options

View File

@ -22,7 +22,9 @@ web pages need to link to the URL to notify users that the RSS feed is available
An outline of step one looks like:
:::php
```php
$feed = new RSSFeed(
$list,
$link,
@ -36,12 +38,14 @@ An outline of step one looks like:
);
$feed->outputToBrowser();
```
To achieve step two include the following code where ever you want to include the `<link>` tag to the RSS Feed. This
will normally go in your `Controllers` `init` method.
```php
:::php
RSSFeed::linkToFeed($link, $title);
```
## Examples
@ -52,7 +56,9 @@ You can use [RSSFeed](api:SilverStripe\Control\RSS\RSSFeed) to easily create a f
**mysite/code/Page.php**
:::php
```php
<?php
..
@ -84,6 +90,7 @@ You can use [RSSFeed](api:SilverStripe\Control\RSS\RSSFeed) to easily create a f
return Page::get()->sort("LastEdited", "DESC")->limit(10);
}
}
```
### Rendering DataObjects in a RSSFeed
@ -97,7 +104,9 @@ If the items are all displayed on a single page you may simply hard code the lin
Take an example, we want to create an RSS feed of all the `Players` objects in our site. We make sure the `AbsoluteLink`
method is defined and returns a string to the full website URL.
:::php
```php
<?php
class Player extends DataObject {
@ -112,10 +121,13 @@ method is defined and returns a string to the full website URL.
);
}
}
```
Then in our controller, we add a new action which returns a the XML list of `Players`.
:::php
```php
<?php
class PageController extends ContentController {
@ -140,6 +152,7 @@ Then in our controller, we add a new action which returns a the XML list of `Pla
return $rss->outputToBrowser();
}
}
```
### Customizing the RSS Feed template
@ -150,7 +163,9 @@ Say from that last example we want to include the Players Team in the XML feed w
**mysite/templates/PlayersRss.ss**
:::xml
```xml
<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
@ -167,12 +182,15 @@ Say from that last example we want to include the Players Team in the XML feed w
<% end_loop %>
</channel>
</rss>
```
`setTemplate` can then be used to tell RSSFeed to use that new template.
**mysite/code/Page.php**
:::php
```php
public function players() {
$rss = new RSSFeed(
@ -185,6 +203,7 @@ Say from that last example we want to include the Players Team in the XML feed w
return $rss->outputToBrowser();
}
```
<div class="warning">
As we've added a new template (PlayersRss.ss) make sure you clear your SilverStripe cache.

View File

@ -6,7 +6,9 @@ You can have more customised logic and interface feedback through a custom contr
form (which is used for `MyDataObject` instances). You can access it through
`http://yoursite.com/MyController/?flush=all`.
:::php
```php
<?php
class MyController extends Controller {
@ -62,6 +64,7 @@ form (which is used for `MyDataObject` instances). You can access it through
return $this->redirectBack();
}
}
```
<div class="alert" markdown="1">
This interface is not secured, consider using [Permission::check()](api:SilverStripe\Security\Permission::check()) to limit the controller to users with certain

View File

@ -4,18 +4,20 @@ title: A custom CSVBulkLoader instance
A an implementation of a custom `CSVBulkLoader` loader. In this example. we're provided with a unique CSV file
containing a list of football players and the team they play for. The file we have is in the format like below.
```
"SpielerNummer", "Name", "Geburtsdatum", "Gruppe"
11, "John Doe", 1982-05-12,"FC Bayern"
12, "Jane Johnson", 1982-05-12,"FC Bayern"
13, "Jimmy Dole",,"Schalke 04"
```
This data needs to be imported into our application. For this, we have two `DataObjects` setup. `Player` contains
information about the individual player and a relation set up for managing the `Team`.
**mysite/code/Player.php**.
:::php
```php
<?php
class Player extends DataObject {
@ -31,10 +33,13 @@ information about the individual player and a relation set up for managing the `
'Team' => 'FootballTeam'
);
}
```
**mysite/code/FootballTeam.php**
:::php
```php
<?php
class FootballTeam extends DataObject {
@ -47,6 +52,7 @@ information about the individual player and a relation set up for managing the `
'Players' => 'Player'
);
}
```
Now going back to look at the CSV, we can see that what we're provided with does not match what our data model looks
like, so we have to create a sub class of `CsvBulkLoader` to handle the unique file. Things we need to consider with
@ -62,7 +68,9 @@ Our final import looks like this.
**mysite/code/PlayerCsvBulkLoader.php**
:::php
```php
<?php
class PlayerCsvBulkLoader extends CsvBulkLoader {
@ -96,6 +104,7 @@ Our final import looks like this.
return FootballTeam::get()->filter('Title', $val)->First();
}
}
```
## Related

View File

@ -9,7 +9,9 @@ First, we write the code to query the API feed.
**mysite/code/Page.php**
:::php
```php
public function getWellingtonWeather() {
$fetch = new RestfulService(
'https://query.yahooapis.com/v1/public/yql'
@ -38,18 +40,22 @@ First, we write the code to query the API feed.
return $output;
}
```
This will provide our `Page` template with a new `WellingtonWeather` variable (an [ArrayList](api:SilverStripe\ORM\ArrayList)). Each item has a
single field `Description`.
**mysite/templates/Page.ss**
:::ss
```ss
<% if WellingtonWeather %>
<% loop WellingtonWeather %>
$Description
<% end_loop %>
<% end_if %>
```
## Related

View File

@ -18,7 +18,9 @@ The default output of a [SearchContext](api:SilverStripe\ORM\Search\SearchContex
Defining search-able fields on your DataObject.
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -28,6 +30,7 @@ Defining search-able fields on your DataObject.
'ProductCode'
);
}
```
## Customizing fields and filters
@ -35,7 +38,9 @@ In this example we're defining three attributes on our MyDataObject subclass: `P
and `MyDate`. The attribute `HiddenProperty` should not be searchable, and `MyDate` should only search for dates
*after* the search entry (with a `GreaterThanFilter`).
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -63,6 +68,7 @@ and `MyDate`. The attribute `HiddenProperty` should not be searchable, and `MyDa
);
}
}
```
<div class="notice" markdown="1">
See the [SearchFilter](../model/searchfilters) documentation for more information about filters to use such as the
@ -76,7 +82,9 @@ the `$fields` constructor parameter.
### Generating a search form from the context
:::php
```php
<?php
// ..
@ -106,6 +114,7 @@ the `$fields` constructor parameter.
))->renderWith('Page_results');
}
}
```
### Pagination
@ -114,7 +123,9 @@ For pagination records on multiple pages, you need to wrap the results in a
in order to read page limit information. It is also passed the current
`HTTPRequest` object so it can read the current page from a GET var.
:::php
```php
public function getResults($searchCriteria = array()) {
$start = ($this->getRequest()->getVar('start')) ? (int)$this->getRequest()->getVar('start') : 0;
$limit = 10;
@ -132,11 +143,13 @@ in order to read page limit information. It is also passed the current
return $records;
}
```
notice that if you want to use this getResults function, you need to change the function doSearch for this one:
:::php
```php
public function doSearch($data, $form) {
$context = singleton('MyDataObject')->getCustomSearchContext();
$results = $this->getResults($data);
@ -144,7 +157,7 @@ notice that if you want to use this getResults function, you need to change the
'Results' => $results
))->renderWith(array('Catalogo_results', 'Page'));
}
```
The change is in **$results = $this->getResults($data);**, because you are using a custom getResults function.
@ -160,9 +173,8 @@ to show the results of your custom search you need at least this content in your
Results.PaginationSummary(4) defines how many pages the search will show in the search results. something like:
**Next 1 2 *3* 4 5 &hellip; 558**
```ss
:::ss
<% if $Results %>
<ul>
<% loop $Results %>
@ -200,7 +212,7 @@ Results.PaginationSummary(4) defines how many pages the search will show in the
</p>
</div>
<% end_if %>
```
## Available SearchFilters

View File

@ -21,7 +21,9 @@ storage engine.
You can do so by adding this static variable to your class definition:
:::php
```php
<?php
class MyDataObject extends DataObject {
@ -30,6 +32,7 @@ You can do so by adding this static variable to your class definition:
'MySQLDatabase' => 'ENGINE=MyISAM'
);
}
```
The [FulltextSearchable](api:SilverStripe\ORM\Search\FulltextSearchable) extension will add the correct `Fulltext` indexes to the data model.
@ -46,7 +49,9 @@ SilverStripe provides a [FulltextFilter](api:SilverStripe\ORM\Filters\FulltextFi
Example DataObject:
:::php
```php
class SearchableDataObject extends DataObject {
private static $db = array(
@ -66,11 +71,15 @@ Example DataObject:
);
}
```
Performing the search:
:::php
```php
SearchableDataObject::get()->filter('SearchFields:Fulltext', 'search term');
```
If your search index is a single field size, then you may also specify the search filter by the name of the
field instead of the index.

View File

@ -27,11 +27,13 @@ The i18n class is enabled by default.
To set the locale you just need to call [i18n::set_locale()](api:SilverStripe\i18n\i18n::set_locale()) passing, as a parameter, the name of the locale that
you want to set.
:::php
```php
// mysite/_config.php
i18n::set_locale('de_DE'); // Setting the locale to German (Germany)
i18n::set_locale('ca_AD'); // Setting to Catalan (Andorra)
```
Once we set a locale, all the calls to the translator function will return strings according to the set locale value, if
these translations are available. See [unicode.org](http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html)
@ -51,13 +53,15 @@ As you set the locale you can also get the current value, just by calling [i18n:
To let browsers know which language they're displaying a document in, you can declare a language in your template.
:::html
```html
//'Page.ss' (HTML)
<html lang="$ContentLocale">
//'Page.ss' (XHTML)
<html lang="$ContentLocale" xml:lang="$ContentLocale" xmlns="http://www.w3.org/1999/xhtml">
```
Setting the `<html>` attribute is the most commonly used technique. There are other ways to specify content languages
(meta tags, HTTP headers), explained in this [w3.org article](http://www.w3.org/International/tutorials/language-decl/).
@ -66,17 +70,23 @@ You can also set the [script direction](http://www.w3.org/International/question
which is determined by the current locale, in order to indicate the preferred flow of characters
and default alignment of paragraphs and tables to browsers.
:::html
```html
<html lang="$ContentLocale" dir="$i18nScriptDirection">
```
### Date and time formats
Formats can be set globally in the i18n class.
You can use these settings for your own view logic.
:::php
```php
Config::inst()->update('i18n', 'date_format', 'dd.MM.yyyy');
Config::inst()->update('i18n', 'time_format', 'HH:mm');
```
Localization in SilverStripe uses PHP's [intl extension](http://php.net/intl).
Formats for it's [IntlDateFormatter](http://php.net/manual/en/class.intldateformatter.php)
@ -96,20 +106,26 @@ They can be accessed via the `i18n.common_languages` and `i18n.common_locales` [
In order to add a value, add the following to your `config.yml`:
:::yml
```yml
i18n:
common_locales:
de_CGN:
name: German (Cologne)
native: Kölsch
```
Similarly, to change an existing language label, you can overwrite one of these keys:
:::yml
```yml
i18n:
common_locales:
en_NZ:
native: Niu Zillund
```
### i18n in URLs
@ -139,22 +155,27 @@ in a localised format chosen by the browser and operating system.
Fields can be forced to use a certain locale and date/time format by calling `setHTML5(false)`,
followed by `setLocale()` or `setDateFormat()`/`setTimeFormat()`.
:::php
```php
$field = new DateField();
$field->setLocale('de_AT'); // set Austrian/German locale, defaulting format to dd.MM.y
$field->setDateFormat('d.M.y'); // set a more specific date format (single digit day/month)
```
## Translating text
Adapting a module to make it localizable is easy with SilverStripe. You just need to avoid hardcoding strings that are
language-dependent and use a translator function call instead.
:::php
```php
// without i18n
echo "This is a string";
// with i18n
echo _t("Namespace.Entity","This is a string");
```
All strings passed through the `_t()` function will be collected in a separate language table (see [Collecting text](#collecting-text)), which is the starting point for translations.
@ -192,7 +213,9 @@ with both a 'one' and 'other' key (as per the CLDR for the default `en` language
For instance, this is an example of how to correctly declare pluralisations for an object
:::php
```php
class MyObject extends DataObject, implements i18nEntityProvider
{
public function provideI18nEntities()
@ -207,13 +230,15 @@ For instance, this is an example of how to correctly declare pluralisations for
];
}
}
```
In YML format this will be expressed as the below. This follows the
[ruby i18n convention](guides.rubyonrails.org/i18n.html#pluralization) for plural forms.
:::yaml
```yaml
en:
MyObject:
SINGULAR_NAME: 'object'
@ -221,14 +246,16 @@ In YML format this will be expressed as the below. This follows the
PLURALS:
one: 'An object',
other: '{count} objects'
```
Note: i18nTextCollector support for pluralisation is not yet available.
Please ensure that any required plurals are exposed via provideI18nEntities.
#### Usage in PHP Files
:::php
```php
// Simple string translation
_t('LeftAndMain.FILESIMAGES','Files & Images');
@ -241,7 +268,7 @@ Please ensure that any required plurals are exposed via provideI18nEntities.
// Plurals are invoked via a `|` pipe-delimeter with a {count} argument
_t('MyObject.PLURALS', 'An object|{count} objects', [ 'count' => '$count ]);
```
#### Usage in Template Files
@ -256,7 +283,9 @@ the PHP version of the function.
* The original language string and the natural language comment parameters are separated by ` on `.
* The final parameter (which is an array in PHP) is passed as a space separated list of key/value pairs.
:::ss
```ss
// Simple string translation
<%t Namespace.Entity "String to translate" %>
@ -265,19 +294,22 @@ the PHP version of the function.
// Plurals follow the same convention, required a `|` and `{count}` in the default string
<%t MyObject.PLURALS 'An item|{count} items' count=$Count %>
```
#### Caching in Template Files with locale switching
When caching a `<% loop %>` or `<% with %>` with `<%t params %>`. It is important to add the Locale to the cache key
otherwise it won't pick up locale changes.
:::ss
```ss
<% cached 'MyIdentifier', $CurrentLocale %>
<% loop $Students %>
$Name
<% end_loop %>
<% end_cached %>
```
## Collecting text
@ -308,7 +340,7 @@ By default, the language files are loaded from modules in this order:
This default order is configured in `framework/_config/i18n.yml`. This file specifies two blocks of module ordering: `basei18n`, listing admin, and framework, and `defaulti18n` listing all other modules.
To create a custom module order, you need to specify a config fragment that inserts itself either after or before those items. For example, you may have a number of modules that have to come after the framework/admin, but before anyhting else. To do that, you would use this
```yml
---
Name: customi18n
Before: 'defaulti18n'
@ -318,7 +350,7 @@ To create a custom module order, you need to specify a config fragment that inse
- module1
- module2
- module3
```
The config option being set is `i18n.module_priority`, and it is a list of module names.
There are a few special cases:
@ -337,21 +369,21 @@ By default, SilverStripe uses a YAML format which is loaded via the
[symfony/translate](http://symfony.com/doc/current/translation.html) library.
Example: framework/lang/en.yml (extract)
```yml
en:
ImageUploader:
Attach: 'Attach {title}'
UploadField:
NOTEADDFILES: 'You can add files once you have saved for the first time.'
```
Translation table: framework/lang/de.yml (extract)
```yml
de:
ImageUploader:
ATTACH: '{title} anhängen'
UploadField:
NOTEADDFILES: 'Sie können Dateien hinzufügen sobald Sie das erste mal gespeichert haben'
```
Note that translations are cached across requests.
The cache can be cleared through the `?flush=1` query parameter,
or explicitly through `Zend_Translate::getCache()->clean(Zend_Cache::CLEANING_MODE_ALL)`.
@ -370,9 +402,11 @@ the browser: The current locale, and the default locale as a fallback.
The `Requirements` class has a special method to determine these includes:
Just point it to a directory instead of a file, and the class will figure out the includes.
:::php
Requirements::add_i18n_javascript('<my-module-dir>/javascript/lang');
```php
Requirements::add_i18n_javascript('<my-module-dir>/javascript/lang');
```
### Translation Tables in JavaScript
@ -381,7 +415,9 @@ As a fallback for partially translated tables we always include the master table
Master Table (`<my-module-dir>/javascript/lang/en.js`)
:::js
```js
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
console.error('Class ss.i18n not defined');
} else {
@ -389,14 +425,17 @@ Master Table (`<my-module-dir>/javascript/lang/en.js`)
'MYMODULE.MYENTITY' : "Really delete these articles?"
});
}
```
Example Translation Table (`<my-module-dir>/javascript/lang/de.js`)
:::js
```js
ss.i18n.addDictionary('de', {
'MYMODULE.MYENTITY' : "Artikel wirklich löschen?"
});
```
For most core modules, these files are generated by a
[build task](https://github.com/silverstripe/silverstripe-buildtools/blob/master/src/GenerateJavascriptI18nTask.php),
@ -405,9 +444,11 @@ format which can be processed more easily by external translation providers (see
### Basic Usage
:::js
alert(ss.i18n._t('MYMODULE.MYENTITY'));
```js
alert(ss.i18n._t('MYMODULE.MYENTITY'));
```
### Advanced Use
@ -417,7 +458,9 @@ The `ss.i18n` object contain a couple functions to help and replace dynamic vari
`sprintf()` will substitute occurencies of `%s` in the main string with each of the following arguments passed to the function. The substitution is done sequentially.
:::js
```js
// MYMODULE.MYENTITY contains "Really delete %s articles by %s?"
alert(ss.i18n.sprintf(
ss.i18n._t('MYMODULE.MYENTITY'),
@ -425,20 +468,22 @@ The `ss.i18n` object contain a couple functions to help and replace dynamic vari
'Douglas Adams'
));
// Displays: "Really delete 42 articles by Douglas Adams?"
```
#### Variable injection with inject()
`inject()` will substitute variables in the main string like `{myVar}` by the keys in the object passed as second argument. Each variable can be in any order and appear multiple times.
:::js
```js
// MYMODULE.MYENTITY contains "Really delete {count} articles by {author}?"
alert(ss.i18n.inject(
ss.i18n._t('MYMODULE.MYENTITY'),
{count: 42, author: 'Douglas Adams'}
));
// Displays: "Really delete 42 articles by Douglas Adams?"
```
## Limitations

View File

@ -38,9 +38,12 @@ In order to retain existing file paths in line with framework version 3 you shou
`\SilverStripe\Assets\Flysystem\FlysystemAssetStore.legacy_filenames` config to true.
Note that this will not allow you to utilise certain file versioning features in 4.0.
:::yaml
```yaml
\SilverStripe\Assets\Flysystem\FlysystemAssetStore:
legacy_filenames: true
```
## Loading content into `DBFile`
@ -48,9 +51,8 @@ A file can be written to the backend from a file which exists on the local files
within the assets folder).
For example, to load a temporary file into a DataObject you could use the below:
```php
:::php
<?
class Banner extends DataObject {
private static $db = array(
@ -61,7 +63,7 @@ For example, to load a temporary file into a DataObject you could use the below:
// Image could be assigned in other parts of the code using the below
$banner = new Banner();
$banner->Image->setFromLocalFile($tempfile['path'], 'uploads/banner-file.jpg');
```
When uploading a file it's normally necessary to give the file a useful name and directory, otherwise the
asset storage backend will choose one for you.

View File

@ -35,7 +35,9 @@ images are preserved (meaning images are not stretched).
Here are some examples, assuming the `$Image` object has dimensions of 200x100px:
:::ss
```ss
// Scaling functions
$Image.ScaleWidth(150) // Returns a 150x75px image
$Image.ScaleMaxWidth(100) // Returns a 100x50px image (like ScaleWidth but prevents up-sampling)
@ -66,11 +68,15 @@ Here are some examples, assuming the `$Image` object has dimensions of 200x100px
$Image.FileName // Returns the actual file name including directory path from web root
$Image.Link // Returns relative URL path to image
$Image.AbsoluteLink // Returns absolute URL path to image
```
Image methods are chainable. Example:
:::ss
```ss
<body style="background-image:url($Image.ScaleWidth(800).CropHeight(800).Link)">
```
### Padded Image Resize
@ -80,10 +86,13 @@ pad any surplus space. You can specify the color of the padding using a hex code
You can also specify a level of transparency to apply to the padding color in a fourth param. This will only effect
png images.
:::php
```php
$Image.Pad(80, 80, FFFFFF, 50) // white padding with 50% transparency
$Image.Pad(80, 80, FFFFFF, 100) // white padding with 100% transparency
$Image.Pad(80, 80, FFFFFF) // white padding with no transparency
```
### Manipulating images in PHP
@ -97,7 +106,9 @@ Please refer to the [ImageManipulation](api:SilverStripe\Assets\ImageManipulatio
You can also create your own functions by decorating the `Image` class.
:::php
```php
class ImageExtension extends \SilverStripe\Core\Extension
{
@ -134,6 +145,7 @@ You can also create your own functions by decorating the `Image` class.
SilverStripe\Filesystem\Storage\DBFile:
extensions:
- ImageExtension
```
### Form Upload
@ -164,24 +176,30 @@ place. If you expect the images in your asset store to already have
compression applied and want to serve up the original when no resampling is
necessary, you can add this to your mysite/config/config.yml file:
:::yml
```yml
# Configure resampling for File dataobject
File:
force_resample: false
# DBFile can be configured independently
SilverStripe\Filesystem\Storage\DBFile:
force_resample: false
```
#### Resampled image quality
To adjust the quality of the generated images when they are resampled, add the
following to your mysite/config/config.yml file:
:::yml
```yml
SilverStripe\Core\Injector\Injector:
SilverStripe\Assets\InterventionBackend:
properties:
Quality: 90
```
## Changing the manipulation driver to Imagick

View File

@ -18,12 +18,14 @@ For instance, in order to write an asset to a protected location you can use the
config option:
:::php
```php
$store = singleton(AssetStore::class);
$store->setFromString('My protected content', 'Documents/Mydocument.txt', null, null, array(
'visibility' => AssetStore::VISIBILITY_PROTECTED
));
```
## User access control
@ -36,7 +38,9 @@ An automated system will, in most cases, handle this whitelisting for you. Calls
will automatically whitelist access to that file for the current user. Using this as a guide, you can easily
control access to embedded assets at a template level.
:::ss
```ss
<ul class="files">
<% loop $File %>
<% if $canView %>
@ -46,6 +50,7 @@ control access to embedded assets at a template level.
<% end_if %>
<% end_loop >
</ul>
```
Users who are able to guess the value of $URL will not be able to access those urls without being
authorised by this code.
@ -58,7 +63,9 @@ authorised users, the following should be considered:
file via PHP for the current user instead, by using the following code to grant access.
:::php
```php
class PageController extends ContentController {
public function init() {
parent::init();
@ -70,6 +77,7 @@ authorised users, the following should be considered:
}
}
}
```
* If a user does not have access to a file, you can still generate the URL but suppress the default
permission whitelist by invoking the getter as a method, but pass in a falsey value as a parameter.
@ -87,7 +95,9 @@ authorised users, the following should be considered:
the `revokeFile` method.
:::php
```php
class PageController extends ContentController {
public function init() {
parent::init();
@ -102,7 +112,7 @@ authorised users, the following should be considered:
}
}
}
```
## Controlling asset visibility
@ -118,22 +128,26 @@ public facing area.
E.g.
:::php
```php
$object->MyFile->setFromLocalFile($tmpFile['Path'], $filename, null, null, array(
'visibility' => AssetStore::VISIBILITY_PROTECTED
));
```
You can also adjust the visibility of any existing file to either public or protected.
:::
```php
// This will make the file available only when a user calls `->grant()`
$object->SecretFile->protectFile();
// This file will be available to everyone with the URL
$object->PublicFile->publishFile();
```
<div class="notice" markdown="1">
One thing to note is that all variants of a single file will be treated as
@ -157,7 +171,7 @@ in the protected store, awaiting publishing.
Internally your folder structure would look something like:
:::
```
assets/
.htaccess
.protected/
@ -166,7 +180,7 @@ Internally your folder structure would look something like:
NewCompanyLogo.gif
33be1b95cb/
OldCompanyLogo.gif
```
The urls for these two files, however, do not reflect the physical structure directly.
@ -181,15 +195,17 @@ will be moved to `assets/a870de278b/NewCompanyLogo.gif`, and will be served dire
the web server, bypassing the need for additional PHP requests.
:::php
```php
$store = singleton(AssetStore::class);
$store->publish('NewCompanyLogo.gif', 'a870de278b475cb75f5d9f451439b2d378e13af1');
```
After this the filesystem will now look like below:
:::
```
assets/
.htaccess
.protected/
@ -198,7 +214,7 @@ After this the filesystem will now look like below:
OldCompanyLogo.gif
a870de278b/
NewCompanyLogo.gif
```
## Performance considerations
@ -234,23 +250,22 @@ root altogether.
For instance, given your web root is in the folder `/sites/mysite/www`, you can tell the asset store
to put protected files into `/sites/mysite/protected` with the below `.env` setting:
```
SS_PROTECTED_ASSETS_PATH="/sites/mysite/protected"
```
### Configuring: File types
In addition to configuring file locations, it's also important to ensure that you have allowed the
appropriate file extensions for your instance. This can be done by setting the `File.allowed_extensions`
config.
```yaml
:::yaml
File:
allowed_extensions:
- 7zip
- xzip
```
<div class="warning" markdown="1">
Any file not included in this config, or in the default list of extensions, will be blocked from
@ -268,11 +283,13 @@ When a protected file is served it will also be transmitted with all headers def
You can customise this with the below config:
:::yaml
```yaml
SilverStripe\Filesystem\Flysystem\FlysystemAssetStore:
file_response_headers:
Pragma: 'no-cache'
```
### Configuring: Archive behaviour
@ -294,22 +311,26 @@ config to true on that class. Note that this feature only works with dataobjects
the `Versioned` extension.
:::php
```php
class MyVersiondObject extends DataObject {
/** Ensure assets are archived along with the DataObject */
private static $keep_archived_assets = true;
/** Versioned */
private static $extensions = array('Versioned');
}
```
The extension can also be globally disabled by removing it at the root level:
:::yaml
```yaml
DataObject:
AssetControl: null
```
### Configuring: Web server settings
@ -330,12 +351,12 @@ In order to ensure that public files are served correctly, you should check that
.htaccess bypasses PHP requests for files that do exist. The default template
(declared by `Assets_HTAccess.ss`) has the following section, which may be customised in your project:
```
# Non existant files passed to requesthandler
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ../framework/main.php?url=%1 [QSA]
```
You will need to ensure that your core apache configuration has the necessary `AllowOverride`
settings to support the local .htaccess file.
@ -348,7 +369,7 @@ while ensuring non-existent files are processed via the Framework.
The default rule for IIS is as below (only partial configuration displayed):
```
<rule name="Protected and 404 File rewrite" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
@ -356,7 +377,7 @@ The default rule for IIS is as below (only partial configuration displayed):
</conditions>
<action type="Rewrite" url="../framework/main.php?url={R:1}" appendQueryString="true" />
</rule>
```
You will need to make sure that the `allowOverride` property of your root web.config is not set
to false, to allow these to take effect.
@ -370,9 +391,9 @@ For instance, this will allow your nginx site to serve files directly, while ens
dynamic requests are processed via the Framework:
:::
```
location ^~ /assets/ {
sendfile on;
try_files $uri /framework/main.php?url=$uri&$query_string;
}
```

View File

@ -19,7 +19,9 @@ a category.
**mysite/code/Product.php**
:::php
```php
<?php
class Product extends DataObject {
@ -34,10 +36,13 @@ a category.
'Category' => 'Category'
);
}
```
**mysite/code/Category.php**
:::php
```php
<?php
class Category extends DataObject {
@ -50,6 +55,7 @@ a category.
'Products' => 'Product'
);
}
```
To create your own `ModelAdmin`, simply extend the base class, and edit the `$managed_models` property with the list of
DataObject's you want to scaffold an interface for. The class can manage multiple models in parallel, if required.
@ -58,7 +64,9 @@ We'll name it `MyAdmin`, but the class name can be anything you want.
**mysite/code/MyAdmin.php**
:::php
```php
<?php
class MyAdmin extends ModelAdmin {
@ -72,6 +80,7 @@ We'll name it `MyAdmin`, but the class name can be anything you want.
private static $menu_title = 'My Product Admin';
}
```
This will automatically add a new menu entry to the SilverStripe Admin UI entitled `My Product Admin` and logged in
users will be able to upload and manage `Product` and `Category` instances through http://yoursite.com/admin/products.
@ -96,7 +105,9 @@ permissions by default. For most cases, less restrictive checks make sense, e.g.
**mysite/code/Category.php**
:::php
```php
<?php
class Category extends DataObject {
@ -116,6 +127,7 @@ permissions by default. For most cases, less restrictive checks make sense, e.g.
public function canCreate($member = null) {
return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
}
```
## Searching Records
@ -129,7 +141,9 @@ class (see [SearchContext](../search/searchcontext) docs for details).
**mysite/code/Product.php**
:::php
```php
<?php
class Product extends DataObject {
@ -139,6 +153,7 @@ class (see [SearchContext](../search/searchcontext) docs for details).
'ProductCode'
);
}
```
<div class="hint" markdown="1">
[SearchContext](../search/searchcontext) documentation has more information on providing the search functionality.
@ -152,7 +167,9 @@ model class, where you can add or remove columns. To change the title, use [Data
**mysite/code/Product.php**
:::php
```php
<?php
class Product extends DataObject {
@ -166,6 +183,7 @@ model class, where you can add or remove columns. To change the title, use [Data
'Price'
);
}
```
The results list are retrieved from [SearchContext::getResults()](api:SilverStripe\ORM\Search\SearchContext::getResults()), based on the parameters passed through the search
form. If no search parameters are given, the results will show every record. Results are a [DataList](api:SilverStripe\ORM\DataList) instance, so
@ -175,7 +193,9 @@ For example, we might want to exclude all products without prices in our sample
**mysite/code/MyAdmin.php**
:::php
```php
<?php
class MyAdmin extends ModelAdmin {
@ -191,13 +211,16 @@ For example, we might want to exclude all products without prices in our sample
return $list;
}
}
```
You can also customize the search behavior directly on your `ModelAdmin` instance. For example, we might want to have a
checkbox which limits search results to expensive products (over $100).
**mysite/code/MyAdmin.php**
:::php
```php
<?php
class MyAdmin extends ModelAdmin {
@ -224,13 +247,16 @@ checkbox which limits search results to expensive products (over $100).
return $list;
}
}
```
To alter how the results are displayed (via [GridField](api:SilverStripe\Forms\GridField\GridField)), you can also overload the `getEditForm()` method. For
example, to add a new component.
**mysite/code/MyAdmin.php**
:::php
```php
<?php
class MyAdmin extends ModelAdmin {
@ -256,13 +282,16 @@ example, to add a new component.
return $form;
}
}
```
The above example will add the component to all `GridField`s (of all managed models). Alternatively we can also add it
to only one specific `GridField`:
**mysite/code/MyAdmin.php**
:::php
```php
<?php
class MyAdmin extends ModelAdmin {
@ -285,6 +314,7 @@ to only one specific `GridField`:
return $form;
}
}
```
## Data Import
@ -302,7 +332,9 @@ This is handled through the [GridFieldExportButton](api:SilverStripe\Forms\GridF
To customize the exported columns, create a new method called `getExportFields` in your `ModelAdmin`:
:::php
```php
<?php
class MyAdmin extends ModelAdmin {
@ -316,7 +348,7 @@ To customize the exported columns, create a new method called `getExportFields`
);
}
}
```
## Related Documentation

View File

@ -54,7 +54,9 @@ coding conventions.
The CMS interface can be accessed by default through the `admin/` URL. You can change this by setting your own [Director routing rule](director#routing-rules) to the `[AdminRootController](api:SilverStripe\Admin\AdminRootController)` and clear the old rule like in the example below.
:::yml
```yml
---
Name: myadmin
After:
@ -65,23 +67,33 @@ The CMS interface can be accessed by default through the `admin/` URL. You can c
'admin': ''
'newAdmin': 'AdminRootController'
---
```
When extending the CMS or creating modules, you can take advantage of various functions that will return the configured admin URL (by default 'admin/' is returned):
In PHP you should use:
:::php
```php
AdminRootController::admin_url()
```
When writing templates use:
:::ss
```ss
$AdminURL
```
And in JavaScript, this is avaible through the `ss` namespace
:::js
```js
ss.config.adminUrl
```
### Multiple Admin URL and overrides
@ -149,7 +161,9 @@ of a `PjaxResponseNegotiator` to handle its display.
Basic example form in a CMS controller subclass:
:::php
```php
class MyAdmin extends LeftAndMain {
function getEditForm() {
return CMSForm::create(
@ -176,6 +190,7 @@ Basic example form in a CMS controller subclass:
->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
}
}
```
Note: Usually you don't need to worry about these settings,
and will simply call `parent::getEditForm()` to modify an existing,
@ -280,22 +295,20 @@ routing mechanism for this section. However, there are two major differences:
Firstly, `reactRouter` must be passed as a boolean flag to indicate that this section is
controlled by the react section, and thus should suppress registration of a page.js route
for this section.
```php
:::php
public function getClientConfig() {
return array_merge(parent::getClientConfig(), [
'reactRouter' => true
]);
}
```
Secondly, you should ensure that your react CMS section triggers route registration on the client side
with the reactRouteRegister component. This will need to be done on the `DOMContentLoaded` event
to ensure routes are registered before window.load is invoked.
```js
:::js
import { withRouter } from 'react-router';
import ConfigHelpers from 'lib/Config';
import reactRouteRegister from 'lib/ReactRouteRegister';
@ -312,19 +325,18 @@ to ensure routes are registered before window.load is invoked.
],
});
});
```
Child routes can be registered post-boot by using `ReactRouteRegister` in the same way.
```js
:::js
// Register a nested url under `sectionConfig.url`
const sectionConfig = ConfigHelpers.getSection('MyAdmin');
reactRouteRegister.add({
path: 'nested',
component: NestedComponent,
}, [ sectionConfig.url ]);
```
## PJAX: Partial template replacement through Ajax
@ -349,7 +361,9 @@ Example: Create a bare-bones CMS subclass which shows breadcrumbs (a built-in me
as well as info on the current record. A single link updates both sections independently
in a single Ajax request.
:::php
```php
// mysite/code/MyAdmin.php
class MyAdmin extends LeftAndMain {
private static $url_segment = 'myadmin';
@ -366,8 +380,9 @@ in a single Ajax request.
return $this->renderWith('MyRecordInfo');
}
}
```
:::js
```js
// MyAdmin.ss
<% include SilverStripe\\Admin\\CMSBreadcrumbs %>
<div>Static content (not affected by update)</div>
@ -381,17 +396,18 @@ in a single Ajax request.
<div data-pjax-fragment="MyRecordInfo">
Current Record: $currentPage.Title
</div>
```
A click on the link will cause the following (abbreviated) ajax HTTP request:
```
GET /admin/myadmin HTTP/1.1
X-Pjax:MyRecordInfo,Breadcrumbs
X-Requested-With:XMLHttpRequest
```
... and result in the following response:
```
{"MyRecordInfo": "<div...", "CMSBreadcrumbs": "<div..."}
```
Keep in mind that the returned view isn't always decided upon when the Ajax request
is fired, so the server might decide to change it based on its own logic,
sending back different `X-Pjax` headers and content.
@ -399,9 +415,9 @@ sending back different `X-Pjax` headers and content.
On the client, you can set your preference through the `data-pjax-target` attributes
on links or through the `X-Pjax` header. For firing off an Ajax request that is
tracked in the browser history, use the `pjax` attribute on the state data.
```js
$('.cms-container').loadPanel(ss.config.adminUrl+'pages', null, {pjax: 'Content'});
```
## Loading lightweight PJAX fragments
Normal navigation between URLs in the admin section of the Framework occurs through `loadPanel` and `submitForm`.
@ -415,11 +431,11 @@ unrelated to the main flow.
In this case you can use the `loadFragment` call supplied by `LeftAndMain.js`. You can trigger as many of these in
parallel as you want. This will not disturb the main navigation.
```js
$('.cms-container').loadFragment(ss.config.adminUrl+'foobar/', 'Fragment1');
$('.cms-container').loadFragment(ss.config.adminUrl+'foobar/', 'Fragment2');
$('.cms-container').loadFragment(ss.config.adminUrl+'foobar/', 'Fragment3');
```
The ongoing requests are tracked by the PJAX fragment name (Fragment1, 2, and 3 above) - resubmission will
result in the prior request for this fragment to be aborted. Other parallel requests will continue undisturbed.
@ -435,23 +451,23 @@ has been found on an element (this element will get completely replaced). Afterw
will be triggered. In case of a request error a `loadfragmenterror` will be raised and DOM will not be touched.
You can hook up a response handler that obtains all the details of the XHR request via Entwine handler:
```js
'from .cms-container': {
onafterloadfragment: function(e, data) {
// Say 'success'!
alert(data.status);
}
}
```
Alternatively you can use the jQuery deferred API:
```js
$('.cms-container')
.loadFragment(ss.config.adminUrl+'foobar/', 'Fragment1')
.success(function(data, status, xhr) {
// Say 'success'!
alert(status);
});
```
## Ajax Redirects
Sometimes, a server response represents a new URL state, e.g. when submitting an "add record" form,
@ -471,7 +487,9 @@ For example, the currently used controller class might've changed due to a "redi
which affects the currently active menu entry. We're using HTTP response headers to contain this data
without affecting the response body.
:::php
```php
class MyController extends LeftAndMain {
class myaction() {
// ...
@ -479,6 +497,7 @@ without affecting the response body.
return $html;
}
}
```
Built-in headers are:
@ -529,12 +548,15 @@ from "Page" to "Files & Images". To communicate this state change, a controller
response has the option to pass along a special HTTP response header,
which is picked up by the menu:
:::php
```php
public function mycontrollermethod() {
// .. logic here
$this->getResponse()->addHeader('X-Controller', 'AssetAdmin');
return 'my response';
}
```
This is usually handled by the existing [LeftAndMain](api:SilverStripe\Admin\LeftAndMain) logic,
so you don't need to worry about it. The same concept applies for
@ -579,7 +601,9 @@ since all others should render with their tab navigation inline.
Form template with custom tab navigation (trimmed down):
:::ss
```ss
<form $FormAttributes data-layout-type="border">
<div class="cms-content-header north">
@ -603,10 +627,13 @@ Form template with custom tab navigation (trimmed down):
</div>
</form>
```
Tabset template without tab navigation (e.g. `CMSTabset.ss`)
:::ss
```ss
<div $AttributesHTML>
<% loop Tabs %>
<% if Tabs %>
@ -620,6 +647,7 @@ Tabset template without tab navigation (e.g. `CMSTabset.ss`)
<% end_if %>
<% end_loop %>
</div>
```
Lazy loading works based on the `href` attribute of the tab navigation.
The base behaviour is applied through adding a class `.cms-tabset` to a container.
@ -629,7 +657,9 @@ This is achieved by template conditionals (see "MyActiveCondition").
The `.cms-panel-link` class will automatically trigger the ajax loading,
and load the HTML content into the main view. Example:
:::ss
```ss
<div id="my-tab-id" class="cms-tabset" data-ignore-tab-state="true">
<ul>
<li class="<% if MyActiveCondition %> ui-tabs-active<% end_if %>">
@ -644,6 +674,7 @@ and load the HTML content into the main view. Example:
</li>
</ul>
</div>
```
The URL endpoints `{$AdminURL}mytabs/tab1` and `{$AdminURL}mytabs/tab2`
should return HTML fragments suitable for inserting into the content area,

View File

@ -21,8 +21,11 @@ children setting sizes and positions, which in turn requires redrawing of some o
The easiest way to update the layout of the CMS is to call `redraw` on the top-level `.cms-container` element.
:::js
```js
$('.cms-container').redraw();
```
This causes the framework to:
@ -60,13 +63,16 @@ Call `redraw` on `.cms-container` to re-layout the CMS.
Layout manager will automatically apply algorithms to the children of `.cms-container` by inspecting the
`data-layout-type` attribute. Let's take the content toolbar as an example of a second-level layout application:
:::html
```html
<div class="cms-content-tools west cms-panel cms-panel-layout"
data-expandOnClick="true"
data-layout-type="border"
id="cms-content-tools-CMSMain">
<%-- content utilising border's north, south, east, west and center classes --%>
</div>
```
For detailed discussion on available algorithms refer to
[jLayout algorithms](https://github.com/bramstein/jlayout#layout-algorithms).
@ -104,8 +110,11 @@ by the algorithm that are initially taken from the `LeftAndMain::LayoutOptions`
Use provided factory method to generate algorithm instances.
:::js
```js
jLayout.threeColumnCompressor(<column-spec-object>, <options-object>);
```
The parameters are as follows:

View File

@ -50,7 +50,9 @@ Note how the configuration happens in different entwine namespaces
("ss.preview" and "ss"), as well as applies to different selectors
(".cms-preview" and ".cms-container").
:::js
```js
(function($) {
$.entwine('ss.preview', function($){
$('.cms-preview').entwine({
@ -72,14 +74,18 @@ Note how the configuration happens in different entwine namespaces
});
});
}(jQuery));
```
Load the file in the CMS via setting adding 'mysite/javascript/MyLeftAndMain.Preview.js'
to the `LeftAndMain.extra_requirements_javascript` [configuration value](../configuration)
:::yml
```yml
LeftAndMain:
extra_requirements_javascript:
- mysite/javascript/MyLeftAndMain.Preview.js
```
In order to find out which configuration values are available, the source code
is your best reference at the moment - have a look in `framework/admin/javascript/src/LeftAndMain.Preview.js`.

View File

@ -6,23 +6,32 @@ summary: Add custom CSS properties to the rich-text editor.
SilverStripe lets you customise the style of content in the CMS. This is done by setting up a CSS file called
`editor.css` in either your theme or in your `mysite` folder. This is set through
:::php
```php
HtmlEditorConfig::get('cms')->setOption('content_css', project() . '/css/editor.css');
```
Will load the `mysite/css/editor.css` file.
If using this config option in `mysite/_config.php`, you will have to instead call:
:::php
```php
HtmlEditorConfig::get('cms')->setOption('content_css', project() . '/css/editor.css');
```
Any CSS classes within this file will be automatically added to the `WYSIWYG` editors 'style' dropdown. For instance, to
add the color 'red' as an option within the `WYSIWYG` add the following to the `editor.css`
:::css
```css
.red {
color: red;
}
```
<div class="notice" markdown="1">
After you have defined the `editor.css` make sure you clear your SilverStripe cache for it to take effect.

View File

@ -44,12 +44,15 @@ plugin. See "[How jQuery Works](http://docs.jquery.com/How_jQuery_Works)" for a
You should write all your custom jQuery code in a closure.
:::javascript
```javascript
(function($) {
$(document).ready(function(){
// your code here.
})
})(jQuery);
```
## jQuery Plugins
@ -70,7 +73,9 @@ development, most importantly:
Example: A plugin to highlight a collection of elements with a configurable foreground and background colour
(abbreviated example from [learningjquery.com](http://www.learningjquery.com/2007/10/a-plugin-development-pattern)).
:::js
```js
// create closure
(function($) {
// plugin definition
@ -96,11 +101,13 @@ Example: A plugin to highlight a collection of elements with a configurable fore
};
// end of closure
})(jQuery);
```
Usage:
:::js
```js
(function($) {
// Highlight all buttons with default colours
jQuery(':button').highlight();
@ -111,7 +118,7 @@ Usage:
// Set all further highlight() calls to have a green background
$.fn.hilight.defaults.background = "green";
})(jQuery);
```
## jQuery UI Widgets
@ -130,7 +137,9 @@ See the [official developer guide](http://jqueryui.com/docs/Developer_Guide) and
Example: Highlighter
:::js
```js
(function($) {
$.widget("ui.myHighlight", {
getBlink: function () {
@ -156,11 +165,13 @@ Example: Highlighter
blink: false
};
})(jQuery);
```
Usage:
:::js
```js
(function($) {
// call with default options
$(':button').myHighlight();
@ -177,7 +188,7 @@ Usage:
// Get property
$(':button').myHighlight('getBlink');
})(jQuery);
```
### jQuery.Entwine
@ -192,7 +203,9 @@ It is also suited for more complex applications beyond a single-purpose plugin.
Example: Highlighter
:::js
```js
(function($) {
$(':button').entwine({
Foreground: 'red',
@ -203,11 +216,13 @@ Example: Highlighter
}
});
})(jQuery);
```
Usage:
:::js
```js
(function($) {
// call with default options
$(':button').entwine().highlight();
@ -218,7 +233,7 @@ Usage:
// get property
$(':button').entwine().getBackground();
})(jQuery);
```
This is a deliberately simple example, the strength of jQuery.entwine over simple jQuery plugins lies in its public
properties, namespacing, as well as its inheritance based on CSS selectors. Please see the [project
@ -238,13 +253,15 @@ jQuery with a few lines of code. Your jQuery code will normally end up as a ser
Global properties are evil. They are accessible by other scripts, might be overwritten or misused. A popular case is the `$` shortcut in different libraries: in PrototypeJS it stands for `document.getElementByID()`, in jQuery for `jQuery()`.
:::js
```js
// you can't rely on '$' being defined outside of the closure
(function($) {
var myPrivateVar; // only available inside the closure
// inside here you can use the 'jQuery' object as '$'
})(jQuery);
```
You can run `[jQuery.noConflict()](http://docs.jquery.com/Core/jQuery.noConflict)` to avoid namespace clashes.
NoConflict mode is enabled by default in the SilverStripe CMS javascript.
@ -254,12 +271,14 @@ NoConflict mode is enabled by default in the SilverStripe CMS javascript.
You have to ensure that DOM elements you want to act on are loaded before using them. jQuery provides a wrapper around
the `window.onload` and `document.ready` events.
:::js
```js
// DOM elements might not be available here
$(document).ready(function() {
// The DOM is fully loaded here
});
```
See [jQuery FAQ: Launching Code on Document
Ready](http://docs.jquery.com/How_jQuery_Works#Launching_Code_on_Document_Ready).
@ -273,7 +292,9 @@ Caution: Only applies to certain events, see the [jQuery.on() documentation](htt
Example: Add a 'loading' classname to all pressed buttons
:::js
```js
// manual binding, only applies to existing elements
$('input[[type=submit]]').on('click', function() {
$(this).addClass('loading');
@ -283,7 +304,7 @@ Example: Add a 'loading' classname to all pressed buttons
$('.cms-container').on('click', 'input[[type=submit]]', function() {
$(this).addClass('loading');
});
```
### Assume Element Collections
@ -292,14 +313,16 @@ makes sense). Encapsulate your code by nesting your jQuery commands inside a `jQ
Example: ComplexTableField implements a paginated table with a pop-up for displaying
:::js
```js
$('div.ComplexTableField').each(function() {
// This is the over code for the tr elements inside a ComplexTableField.
$(this).find('tr').hover(
// ...
);
});
```
### Use plain HTML and jQuery.data() to store data
@ -310,18 +333,22 @@ Example: Simple form change tracking to prevent submission of unchanged data
Through CSS properties
:::js
```js
$('form :input').bind('change', function(e) {
$(this.form).addClass('isChanged');
});
$('form').bind('submit', function(e) {
if($(this).hasClass('isChanged')) return false;
});
```
Through jQuery.data()
:::js
```js
$('form :input').bind('change', function(e) {
$(this.form).data('isChanged', true);
});
@ -329,7 +356,7 @@ Through jQuery.data()
alert($(this).data('isChanged'));
if($(this).data('isChanged')) return false;
});
```
See [interactive example on jsbin.com](http://jsbin.com/opuva)
@ -339,11 +366,16 @@ rendering a form element through the SilverStripe templating engine.
Example: Restricted numeric value field
:::ss
```ss
<input type="text" class="restricted-text {min:4,max:10}" />
```
:::js
```js
$('.restricted-text').bind('change', function(e) {
if(
e.target.value < $(this).metadata().min
@ -353,7 +385,7 @@ Example: Restricted numeric value field
return false;
}
});
```
See [interactive example on jsbin.com](http://jsbin.com/axafa)
@ -375,17 +407,21 @@ Example: Autocomplete input field loading page matches through AJAX
Template:
:::ss
```ss
<ul>
<% loop $Results %>
<li id="Result-$ID">$Title</li>
<% end_loop %>
</ul>
```
PHP:
:::php
```php
class MyController {
public function autocomplete($request) {
$results = Page::get()->filter("Title", $request->getVar('title'));
@ -401,11 +437,13 @@ PHP:
))->renderWith('AutoComplete');
}
}
```
HTML
:::ss
```ss
<form action"#">
<div class="autocomplete {url:'MyController/autocomplete'}">
<input type="text" name="title" />
@ -413,11 +451,13 @@ HTML
</div>
<input type="submit" value="action_autocomplete" />
</form>
```
JavaScript:
:::js
```js
$('.autocomplete input').on('change', function() {
var resultsEl = $(this).siblings('.results');
resultsEl.load(
@ -435,7 +475,7 @@ JavaScript:
}
);
});
```
Although they are the minority of cases, there are times when a simple HTML fragment isn't enough. For example, if you
have server side code that needs to trigger the update of a couple of elements in the CMS left-hand tree, it would be
@ -456,7 +496,9 @@ events](http://docs.jquery.com/Namespaced_Events).
Example: Trigger custom 'validationfailed' event on form submission for each empty element
:::js
```js
$('form').bind('submit', function(e) {
// $(this) refers to form
$(this).find(':input').each(function() {
@ -471,7 +513,7 @@ Example: Trigger custom 'validationfailed' event on form submission for each emp
// $(this) refers to input field
alert($(this).attr('name'));
});
```
See [interactive example on jsbin.com](http://jsbin.com/ipeca).
@ -513,7 +555,9 @@ JSDoc-toolkit is a command line utility, see [usage](http://code.google.com/p/js
Example: jQuery.entwine
:::js
```js
/**
* Available Custom Events:
@ -560,7 +604,7 @@ Example: jQuery.entwine
}
};
]]);
```
### Unit Testing
@ -572,16 +616,18 @@ start with JSpec, as it provides a much more powerful testing framework.
Example: QUnit test (from [jquery.com](http://docs.jquery.com/QUnit#Using_QUnit)):
:::js
```js
test("a basic test example", function() {
ok( true, "this test is fine" );
var value = "hello";
equals( "hello", value, "We expect value to be hello" );
});
```
Example: JSpec Shopping cart test (from [visionmedia.github.com](http://visionmedia.github.com/jspec/))
```
describe 'ShoppingCart'
before_each
cart = new ShoppingCart
@ -594,7 +640,7 @@ Example: JSpec Shopping cart test (from [visionmedia.github.com](http://visionme
end
end
end
```
## Related
* [Unobtrusive Javascript](http://www.onlinetools.org/articles/unobtrusivejavascript/chapter1.html)

View File

@ -26,7 +26,9 @@ state already, so you just need to add the alternate state using two data additi
Here is the configuration code for the button:
:::php
```php
public function getCMSActions() {
$fields = parent::getCMSActions();
@ -41,6 +43,7 @@ Here is the configuration code for the button:
return $fields;
}
```
You can control the state of the button from the backend by applying `ss-ui-alternate` class to the `FormAction`. To
simplify our example, let's assume the button state is controlled on the backend only, but you'd usually be better off
@ -50,7 +53,9 @@ used for initialisation though.
Here we initialise the button based on the backend check, and assume that the button will only update after page reload
(or on CMS action).
:::php
```php
public function getCMSActions() {
// ...
if ($this->needsCleaning()) {
@ -59,6 +64,7 @@ Here we initialise the button based on the backend check, and assume that the bu
}
// ...
}
```
## Frontend support
@ -72,24 +78,35 @@ frontend. You can affect the state of the button through the jQuery UI calls.
First of all, you can toggle the state of the button - execute this code in the browser's console to see how it works.
:::js
```js
jQuery('.cms-edit-form .btn-toolbar #Form_EditForm_action_cleanup').button('toggleAlternate');
```
Another, more useful, scenario is to check the current state.
:::js
```js
jQuery('.cms-edit-form .btn-toolbar #Form_EditForm_action_cleanup').button('option', 'showingAlternate');
```
You can also force the button into a specific state by using UI options.
:::js
```js
jQuery('.cms-edit-form .btn-toolbar #Form_EditForm_action_cleanup').button({showingAlternate: true});
```
This will allow you to react to user actions in the CMS and give immediate feedback. Here is an example taken from the
CMS core that tracks the changes to the input fields and reacts by enabling the *Save* and *Save & publish* buttons
(changetracker will automatically add `changed` class to the form if a modification is detected).
:::js
```js
/**
* Enable save buttons upon detecting changes to content.
* "changed" class is added by jQuery.changetracker.
@ -107,6 +124,7 @@ CMS core that tracks the changes to the input fields and reacts by enabling the
this._super(e);
}
});
```
## Frontend hooks
@ -136,7 +154,9 @@ disassembled into:
Here is the entire handler put together. You don't need to add any separate initialisation code, this will handle all
cases.
:::js
```js
(function($) {
$.entwine('mysite', function($){
@ -157,6 +177,7 @@ cases.
});
}(jQuery));
```
## Summary

View File

@ -9,17 +9,23 @@ shown alongside the field, a tooltip which shows on demand, or toggleable descri
The `FormField->setDescription()` method will add a `<span class="description">`
at the last position within the field, and expects unescaped HTML content.
:::php
```php
TextField::create('MyText', 'My Text Label')
->setDescription('More <strong>detailed</strong> help');
```
To show the help text as a tooltip instead of inline,
add a `.cms-description-tooltip` class.
:::php
```php
TextField::create('MyText', 'My Text Label')
->setDescription('More <strong>detailed</strong> help')
->addExtraClass('cms-description-tooltip');
```
Tooltips are only supported
for native, focusable input elements, which excludes
@ -34,19 +40,25 @@ Another option you have available is making the field's description togglable. T
the UI tidy by hiding the description until the user requests more information
by clicking the 'info' icon displayed alongside the field.
:::php
```php
TextField::create('MyText', 'My Text Label')
->setDescription('More <strong>detailed</strong> help')
->addExtraClass('cms-description-toggle');
```
If you want to provide a custom icon for toggling the description, you can do that
by setting an additional `RightTitle`.
:::php
```php
TextField::create('MyText', 'My Text Label')
->setDescription('More <strong>detailed</strong> help')
->addExtraClass('cms-description-toggle')
->setRightTitle('<a class="cms-description-trigger">My custom icon</a>');
```
Note: For more advanced help text we recommend using
[Custom form field templates](/developer_guides/forms/form_templates);

View File

@ -20,11 +20,14 @@ First we'll need a custom icon. For this purpose SilverStripe uses 16x16
black-and-transparent PNG graphics. In this case we'll place the icon in
`mysite/images`, but you are free to use any location.
:::php
```php
class ProductAdmin extends ModelAdmin {
// ...
private static $menu_icon = 'mysite/images/product-icon.png';
}
```
### Defining a Custom Title
@ -32,11 +35,14 @@ The title of menu entries is configured through the `$menu_title` static.
If its not defined, the CMS falls back to using the class name of the
controller, removing the "Admin" bit at the end.
:::php
```php
class ProductAdmin extends ModelAdmin {
// ...
private static $menu_title = 'My Custom Admin';
}
```
In order to localize the menu title in different languages, use the
`<classname>.MENUTITLE` entity name, which is automatically created when running
@ -54,7 +60,9 @@ Google to the menu.
First, we need to define a [LeftAndMainExtension](api:SilverStripe\Admin\LeftAndMainExtension) which will contain our
button configuration.
:::php
```php
<?php
class CustomLeftAndMain extends LeftAndMainExtension {
@ -82,14 +90,17 @@ button configuration.
CMSMenu::add_link($id, $title, $link, $priority, $attributes);
}
}
```
To have the link appear, make sure you add the extension to the `LeftAndMain`
class. For more information about configuring extensions see the
[extensions reference](/developer_guides/extending/extensions).
:::php
LeftAndMain::add_extension('CustomLeftAndMain')
```php
LeftAndMain::add_extension('CustomLeftAndMain')
```
## Related

View File

@ -17,7 +17,9 @@ You can use these two classes as a starting point for your customizations.
Here's a brief example on how to add sorting and a new column for a
hypothetical `NewsPageHolder` type, which contains `NewsPage` children.
:::php
```php
// mysite/code/NewsPageHolder.php
class NewsPageHolder extends Page {
private static $allowed_children = array('NewsPage');
@ -29,6 +31,7 @@ hypothetical `NewsPageHolder` type, which contains `NewsPage` children.
'Author' => 'Member',
);
}
```
We'll now add an `Extension` subclass to `LeftAndMain`, which is the main CMS controller.
This allows us to intercept the list building logic, and alter the `GridField`
@ -36,7 +39,9 @@ before its rendered. In this case, we limit our logic to the desired page type,
although it's just as easy to implement changes which apply to all page types,
or across page types with common characteristics.
:::php
```php
// mysite/code/NewsPageHolderCMSMainExtension.php
class NewsPageHolderCMSMainExtension extends Extension {
function updateListView($listView) {
@ -61,12 +66,13 @@ or across page types with common characteristics.
}
}
}
```
Now you just need to enable the extension in your [configuration file](../../configuration).
```yml
// mysite/_config/config.yml
LeftAndMain:
extensions:
- NewsPageHolderCMSMainExtension
```
You're all set! Don't forget to flush the caches by appending `?flush=all` to the URL.

View File

@ -20,7 +20,9 @@ link that wraps around the node title, a node's id which is given as id attribut
tags showing the node status, etc. SilverStripe tree node will be typically rendered into html
code like this:
:::ss
```ss
...
<ul>
...
@ -39,6 +41,7 @@ code like this:
...
</ul>
...
```
By applying the proper style sheet, the snippet html above could produce the look of:
![Page Node Screenshot](../../../_images/tree_node.png "Page Node")
@ -61,7 +64,9 @@ will be used for the class attribute of &lt;li&gt; tag of the tree node.
### Add new flag
__Example: using a subclass__
:::php
```php
class Page extends SiteTree {
public function getScheduledToPublish(){
// return either true or false
@ -73,6 +78,7 @@ __Example: using a subclass__
return $flags;
}
}
```
The above subclass of [SiteTree](api:SilverStripe\CMS\Model\SiteTree) will add a new flag for indicating its
__'Scheduled To Publish'__ status. The look of the page node will be changed

View File

@ -33,8 +33,8 @@ Inside the *mysite/code* folder create a file called *CustomSideReport.php*. Ins
The following example will create a report to list every page on the current site.
###CustomSideReport.php
```php
:::php
class CustomSideReport_NameOfReport extends SS_Report {
// the name of the report
@ -56,6 +56,7 @@ The following example will create a report to list every page on the current sit
return $fields;
}
}
```
More useful reports can be created by changing the `DataList` returned in the `sourceRecords` function.

View File

@ -28,7 +28,9 @@ Copy the template markup of the base implementation at `framework/admin/template
into `mysite/templates/Includes/LeftAndMain_Menu.ss`. It will automatically be picked up by
the CMS logic. Add a new section into the `<ul class="cms-menu-list">`
:::ss
```ss
...
<ul class="cms-menu-list">
<!-- ... -->
@ -40,6 +42,7 @@ the CMS logic. Add a new section into the `<ul class="cms-menu-list">`
</li>
</ul>
...
```
Refresh the CMS interface with `admin/?flush=all`, and you should see those
hardcoded links underneath the left-hand menu. We'll make these dynamic further down.
@ -51,16 +54,22 @@ we'll add some CSS, and get it to load
with the CMS interface. Paste the following content into a new file called
`mysite/css/BookmarkedPages.css`:
:::css
```css
.bookmarked-link.first {margin-top: 1em;}
```
Load the new CSS file into the CMS, by setting the `LeftAndMain.extra_requirements_css`
[configuration value](../../configuration).
:::yml
```yml
LeftAndMain:
extra_requirements_css:
- mysite/css/BookmarkedPages.css
```
## Create a "bookmark" flag on pages
@ -69,7 +78,9 @@ the database. For this we need to decorate the page record with a
`DataExtension`. Create a new file called `mysite/code/BookmarkedPageExtension.php`
and insert the following code.
:::php
```php
<?php
class BookmarkedPageExtension extends DataExtension {
@ -84,13 +95,17 @@ and insert the following code.
);
}
}
```
Enable the extension in your [configuration file](../../configuration)
:::yml
```yml
SiteTree:
extensions:
- 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.
@ -104,7 +119,9 @@ links)? Again, we extend a core class: The main CMS controller called
Add the following code to a new file `mysite/code/BookmarkedLeftAndMainExtension.php`;
:::php
```php
<?php
class BookmarkedPagesLeftAndMainExtension extends LeftAndMainExtension {
@ -113,19 +130,25 @@ Add the following code to a new file `mysite/code/BookmarkedLeftAndMainExtension
return Page::get()->filter("IsBookmarked", 1);
}
}
```
Enable the extension in your [configuration file](../../configuration)
:::yml
```yml
LeftAndMain:
extensions:
- BookmarkedPagesLeftAndMainExtension
```
As the last step, replace the hardcoded links with our list from the database.
Find the `<ul>` you created earlier in `mysite/admin/templates/LeftAndMain.ss`
and replace it with the following:
:::ss
```ss
<ul class="cms-menu-list">
<!-- ... -->
<% loop $BookmarkedPages %>
@ -134,6 +157,7 @@ and replace it with the following:
</li>
<% end_loop %>
</ul>
```
## Extending the CMS actions
@ -165,26 +189,38 @@ First of all we can add a regular standalone button anywhere in the set. Here
we are inserting it in the front of all other actions. We could also add a
button group (`CompositeField`) in a similar fashion.
:::php
```php
$fields->unshift(FormAction::create('normal', 'Normal button'));
```
We can affect the existing button group by manipulating the `CompositeField`
already present in the `FieldList`.
:::php
```php
$fields->fieldByName('MajorActions')->push(FormAction::create('grouped', 'New group button'));
```
Another option is adding actions into the drop-up - best place for placing
infrequently used minor actions.
:::php
```php
$fields->addFieldToTab('ActionMenus.MoreOptions', FormAction::create('minor', 'Minor action'));
```
We can also easily create new drop-up menus by defining new tabs within the
`TabSet`.
:::php
```php
$fields->addFieldToTab('ActionMenus.MyDropUp', FormAction::create('minor', 'Minor action in a new drop-up'));
```
<div class="hint" markdown='1'>
Empty tabs will be automatically removed from the `FieldList` to prevent clutter.
@ -205,7 +241,9 @@ Your newly created buttons need handlers to bind to before they will do anything
To implement these handlers, you will need to create a `LeftAndMainExtension` and add
applicable controller actions to it:
:::php
```php
class CustomActionsExtension extends LeftAndMainExtension {
private static $allowed_actions = array(
@ -218,18 +256,25 @@ applicable controller actions to it:
}
}
```
The extension then needs to be registered:
:::yaml
```yaml
LeftAndMain:
extensions:
- CustomActionsExtension
```
You can now use these handlers with your buttons:
:::php
```php
$fields->push(FormAction::create('sampleAction', 'Perform Sample Action'));
```
## Summary

View File

@ -3,20 +3,26 @@
Sometimes you'll work with ModelAdmins from other modules. To customise these interfaces, you can always subclass. But there's
also another tool at your disposal: The [Extension](api:SilverStripe\Core\Extension) API.
:::php
```php
class MyAdminExtension extends Extension {
// ...
public function updateEditForm(&$form) {
$form->Fields()->push(/* ... */)
}
}
```
Now enable this extension through your `[config.yml](/topics/configuration)` file.
:::yml
```yml
MyAdmin:
extensions:
- MyAdminExtension
```
The following extension points are available: `updateEditForm()`, `updateSearchContext()`,
`updateSearchForm()`, `updateList()`, `updateImportForm`.

View File

@ -18,7 +18,9 @@ this defines the actions that need to be executed on a flush request.
This example uses [Cache](api:Cache) in some custom code, and the same cache is cleaned on flush:
:::php
```php
<?php
class MyClass extends DataObject implements Flushable {
@ -37,6 +39,7 @@ This example uses [Cache](api:Cache) in some custom code, and the same cache is
}
}
```
### Using with filesystem
@ -44,7 +47,7 @@ Another example, some temporary files are created in a directory in assets, and
useful in an example like `GD` or `Imagick` generating resampled images, but we want to delete any cached images on
flush so they are re-created on demand.
:::php
```php
<?php
class MyClass extends DataObject implements Flushable {
@ -55,4 +58,4 @@ flush so they are re-created on demand.
}
}
```

View File

@ -8,9 +8,12 @@ which a SilverStripe application must have available in order for requests to be
This can be accessed in user code via Injector
:::php
```php
$kernel = Injector::inst()->get(Kernel::class);
echo "Current environment: " . $kernel->getEnvironment();
```
## Kernel services
@ -35,7 +38,9 @@ As with Config and Injector the Kernel can be nested to safely modify global app
and subsequently restore state. Unlike those classes, however, there is no `::unnest()`. Instead
you should call `->activate()` on the kernel instance you would like to unnest to.
:::php
```php
$oldKernel = Injector::inst()->get(Kernel::class);
try {
// Injector::inst() / Config::inst() are automatically updated to the new kernel
@ -46,7 +51,7 @@ you should call `->activate()` on the kernel instance you would like to unnest t
// Any changes to config (or other application state) have now been reverted
$oldKernel->activate();
}
```
# Application
@ -64,7 +69,9 @@ This class provides basic support for HTTP Middleware, such as [ErrorControlChai
`main.php` contains the default application implementation.
:::php
```php
<?php
use SilverStripe\Control\HTTPApplication;
@ -83,7 +90,7 @@ This class provides basic support for HTTP Middleware, such as [ErrorControlChai
$app->addMiddleware(new ErrorControlChainMiddleware($app));
$response = $app->handle($request);
$response->output();
```
Users can customise their own application by coping the above to a file in `mysite/main.php`, and
updating their `.htaccess` to point to the new file.
@ -102,7 +109,9 @@ Note: This config must also be duplicated in the below template which provide as
`silverstripe-assets/templates/SilverStripe/Assets/Flysystem/PublicAssetAdapter_HTAccess.ss`:
:::ss
```ss
<IfModule mod_rewrite.c>
# ...
# Non existant files passed to requesthandler
@ -110,6 +119,7 @@ Note: This config must also be duplicated in the below template which provide as
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ../mysite/main.php?url=%1 [QSA]
</IfModule>
```
## Custom application actions
@ -124,7 +134,9 @@ For example, the below will setup a request, session, and current controller,
but will leave the application in a "ready" state without performing any
routing.
:::php
```php
$request = CLIRequestBuilder::createFromEnvironment();
$kernel = new TestKernel(BASE_PATH);
$app = new HTTPApplication($kernel);
@ -138,6 +150,6 @@ routing.
$controller->pushCurrent();
$controller->doInit();
}, true);
```

View File

@ -8,10 +8,11 @@ needs to interface over the command line.
The main entry point for any command line execution is `framework/cli-script.php`. For example, to run a database
rebuild from the command line, use this command:
```bash
:::bash
cd your-webroot/
php framework/cli-script.php dev/build
```
<div class="notice">
Your command line php version is likely to use a different configuration as your webserver (run `php -i` to find out
@ -32,10 +33,10 @@ when running the command php -v, then you may not have php-cli installed so sake
### Installation
`sake` can be invoked using `./framework/sake`. For easier access, copy the `sake` file into `/usr/bin/sake`.
```
cd your-webroot/
sudo ./framework/sake installsake
```
<div class="warning">
This currently only works on UNIX like systems, not on Windows.
</div>
@ -56,22 +57,27 @@ SS_BASE_URL="http://localhost/base-url"
Sake can run any controller by passing the relative URL to that controller.
:::bash
```bash
sake /
# returns the homepage
sake dev/
# shows a list of development operations
```
Sake is particularly useful for running build tasks.
```bash
:::bash
sake dev/build "flush=1"
```
It can also be handy if you have a long running script..
```bash
:::bash
sake dev/tasks/MyReallyLongTask
```
### Running processes
@ -85,7 +91,9 @@ sleep when the process is in the middle of doing things, and a long sleep when d
This code provides a good template:
:::php
```php
<?php
class MyProcess extends Controller {
@ -107,14 +115,16 @@ This code provides a good template:
}
}
}
```
Then the process can be managed through `sake`
:::bash
```bash
sake -start MyProcess
sake -stop MyProcess
```
<div class="notice">
`sake` stores `pid` and log files in the site root directory.
@ -124,14 +134,20 @@ Then the process can be managed through `sake`
Parameters can be added to the command. All parameters will be available in `$_GET` array on the server.
:::bash
```bash
cd your-webroot/
php framework/cli-script.php myurl myparam=1 myotherparam=2
```
Or if you're using `sake`
:::bash
```bash
sake myurl "myparam=1&myotherparam=2"
```
## Running Regular Tasks With Cron
@ -140,5 +156,6 @@ On a UNIX machine, you can typically run a scheduled task with a [cron job](http
The following will run `MyTask` every minute.
:::bash
```bash
* * * * * /your/site/folder/sake dev/tasks/MyTask
```

View File

@ -13,30 +13,38 @@ the [Cookie](api:SilverStripe\Control\Cookie) class. This class mostly follows t
Sets the value of cookie with configuration.
:::php
```php
Cookie::set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false);
// Cookie::set('MyApplicationPreference', 'Yes');
```
### get
Returns the value of cookie.
:::php
```php
Cookie::get($name);
// Cookie::get('MyApplicationPreference');
// returns 'Yes'
```
### force_expiry
Clears a given cookie.
:::php
```php
Cookie::force_expiry($name, $path = null, $domain = null);
// Cookie::force_expiry('MyApplicationPreference')
```
## Cookie_Backend
@ -47,7 +55,9 @@ that fetches, sets and expires cookies. By default we use a [CookieJar](api:Silv
The [CookieJar](api:SilverStripe\Control\CookieJar) keeps track of cookies that have been set by the current process as well as those that were received
from the browser.
:::php
```php
$myCookies = array(
'cookie1' => 'value1',
);
@ -57,6 +67,7 @@ from the browser.
Injector::inst()->registerService($newBackend, 'Cookie_Backend');
Cookie::get('cookie1');
```
### Resetting the Cookie_Backend state
@ -64,28 +75,34 @@ Assuming that your application hasn't messed around with the `$_COOKIE` superglo
`Cookie_Backend` by simply unregistering the `CookieJar` service with `Injector`. Next time you access `Cookie` it'll
create a new service for you using the `$_COOKIE` superglobal.
:::php
```php
Injector::inst()->unregisterNamedObject('Cookie_Backend');
Cookie::get('cookiename'); // will return $_COOKIE['cookiename'] if set
```
Alternatively, if you know that the superglobal has been changed (or you aren't sure it hasn't) you can attempt to use
the current `CookieJar` service to tell you what it was like when it was registered.
:::php
```php
//store the cookies that were loaded into the `CookieJar`
$recievedCookie = Cookie::get_inst()->getAll(false);
//set a new `CookieJar`
Injector::inst()->registerService(new CookieJar($recievedCookie), 'CookieJar');
```
### Using your own Cookie_Backend
If you need to implement your own Cookie_Backend you can use the injector system to force a different class to be used.
:::yml
```yml
---
Name: mycookie
After: '#cookie'
@ -93,6 +110,7 @@ If you need to implement your own Cookie_Backend you can use the injector system
Injector:
Cookie_Backend:
class: MyCookieJar
```
To be a valid backend your class must implement the [Cookie_Backend](api:SilverStripe\Control\Cookie_Backend) interface.
@ -105,23 +123,28 @@ came from the browser as part of the request.
Using the `Cookie_Backend` we can do this like such:
:::php
```php
Cookie::set('CookieName', 'CookieVal');
Cookie::get('CookieName'); //gets the cookie as we set it
//will return the cookie as it was when it was sent in the request
Cookie::get('CookieName', false);
```
### Accessing all the cookies at once
One can also access all of the cookies in one go using the `Cookie_Backend`
:::php
```php
Cookie::get_inst()->getAll(); //returns all the cookies including ones set during the current process
Cookie::get_inst()->getAll(false); //returns all the cookies in the request
```
## API Documentation

View File

@ -12,26 +12,35 @@ unit-testing, you can create multiple Controllers, each with their own session.
## set
:::php
```php
Session::set('MyValue', 6);
```
Saves the value of to session data. You can also save arrays or serialized objects in session (but note there may be
size restrictions as to how much you can save).
:::php
```php
// saves an array
Session::set('MyArrayOfValues', array('1','2','3'));
// saves an object (you'll have to unserialize it back)
$object = new Object();
Session::set('MyObject', serialize($object));
```
## get
Once you have saved a value to the Session you can access it by using the `get` function. Like the `set` function you
can use this anywhere in your PHP files.
:::php
```php
echo Session::get('MyValue');
// returns 6
@ -40,36 +49,43 @@ can use this anywhere in your PHP files.
$object = unserialize(Session::get('MyObject', $object));
// $object = Object()
```
## get_all
You can also get all the values in the session at once. This is useful for debugging.
```php
:::php
Session::get_all();
// returns an array of all the session values.
```
## clear
Once you have accessed a value from the Session it doesn't automatically wipe the value from the Session, you have
to specifically remove it.
```php
:::php
Session::clear('MyValue');
```
Or you can clear every single value in the session at once. Note SilverStripe stores some of its own session data
including form and page comment information. None of this is vital but `clear_all` will clear everything.
```php
:::php
Session::clear_all();
```
## Secure Session Cookie
In certain circumstances, you may want to use a different `session_name` cookie when using the `https` protocol for security purposes. To do this, you may set the `cookie_secure` parameter to `true` on your `config.yml`
:::yml
```yml
Session:
cookie_secure: true
```
This uses the session_name `SECSESSID` for `https` connections instead of the default `PHPSESSID`. Doing so adds an extra layer of security to your session cookie since you no longer share `http` and `https` sessions.

View File

@ -212,14 +212,15 @@ Further guidelines:
* Mention important changed classes and methods in the commit summary.
Example: Bad commit message
```
finally fixed this dumb rendering bug that Joe talked about ... LOL
also added another form field for password validation
```
Example: Good commit message
```
BUG Formatting through prepValueForDB()
Added prepValueForDB() which is called on DBField->writeToManipulation()
to ensure formatting of value before insertion to DB on a per-DBField type basis (fixes #1234).
Added documentation for DBField->writeToManipulation() (related to a4bd42fd).
```

View File

@ -61,7 +61,9 @@ How to deprecate an API:
Here's an example for replacing `Director::isDev()` with a (theoretical) `Env::is_dev()`:
:::php
```php
/**
* Returns true if your are in development mode
* @deprecated 4.0 Use {@link Env::is_dev()} instead.
@ -70,6 +72,7 @@ Here's an example for replacing `Director::isDev()` with a (theoretical) `Env::i
Deprecation::notice('4.0', 'Use Env::is_dev() instead');
return Env::is_dev();
}
```
This change could be committed to a minor release like *3.2.0*, and remains deprecated in all subsequent minor releases
(e.g. *3.3.0*, *3.4.0*), until a new major release (e.g. *4.0.0*), at which point it gets removed from the codebase.
@ -82,9 +85,11 @@ notices are always disabled on both live and test.
`mysite/_config.php`
:::php
Deprecation::set_enabled(false);
```php
Deprecation::set_enabled(false);
```
`.env`

View File

@ -39,7 +39,7 @@ As a core contributor it is necessary to have installed the following set of too
* A good `.env` setup in your localhost webroot.
Example `.env`:
```
# Environent
SS_TRUSTED_PROXY_IPS="*"
SS_ENVIRONMENT_TYPE="dev"
@ -59,7 +59,7 @@ Example `.env`:
# Basic CLI request url default
SS_BASE_URL="http://localhost/"
```
You will also need to be assigned the following permissions. Contact one of the SS staff from
the [core committers](core_committers), who will assist with setting up your credentials.
@ -168,9 +168,9 @@ doe not make any upstream changes (so it's safe to run without worrying about
any mistakes migrating their way into the public sphere).
Invoked by running `cow release` in the format as below:
```
cow release <version> -vvv
```
This command has the following parameters:
* `<version>` The version that is to be released. E.g. 3.2.4 or 4.0.0-alpha4
@ -238,9 +238,9 @@ building an archive, and uploading to
[www.silverstripe.org](http://www.silverstripe.org/software/download/) download page.
Invoked by running `cow release:publish` in the format as below:
```
cow release:publish <version> -vvv
```
As with the `cow release` command, this step is broken down into the following
subtasks which are invoked in sequence:

View File

@ -90,31 +90,31 @@ sparingly.
</div>
Code for a Tip box:
```
<div class="hint" markdown='1'>
...
</div>
```
<div class="notice" markdown='1'>
"Notification box": A notification box is good for technical notifications relating to the main text. For example, notifying users about a deprecated feature.
</div>
Code for a Notification box:
```
<div class="notice" markdown='1'>
...
</div>
```
<div class="warning" markdown='1'>
"Warning box": A warning box is useful for highlighting a severe bug or a technical issue requiring a user's attention. For example, suppose a rare edge case sometimes leads to a variable being overwritten incorrectly. A warning box can be used to alert the user to this case so they can write their own code to handle it.
</div>
Code for a Warning box:
```
<div class="warning" markdown='1'>
...
</div>
```
See [markdown extra documentation](http://michelf.com/projects/php-markdown/extra/#html) for more restrictions
on placing HTML blocks inside Markdown.

View File

@ -71,13 +71,14 @@ under the "silverstripe" user, see
Translations need to be reviewed before being committed, which is a process that happens roughly once per month. We're
merging back translations into all supported release branches as well as the `master` branch. The following script
should be applied to the oldest release branch, and then merged forward into newer branches:
```bash
:::bash
tx pull
# Manually review changes through git diff, then commit
git add lang/*
git commit -m "Updated translations"
```
<div class="notice" markdown="1">
You can download your work right from Transifex in order to speed up the process for your desired language.
@ -88,9 +89,10 @@ You can download your work right from Transifex in order to speed up the process
SilverStripe also supports translating strings in JavaScript (see [i18n](/developer_guides/i18n)), but there's a
conversion step involved in order to get those translations syncing with Transifex. Our translation files stored in
`mymodule/javascript/lang/*.js` call `ss.i18n.addDictionary()` to add files.
```js
:::js
ss.i18n.addDictionary('de', {'MyNamespace.MyKey': 'My Translation'});
```
But Transifex only accepts structured formats like JSON.
@ -99,8 +101,8 @@ But Transifex only accepts structured formats like JSON.
```
First of all, you need to create those source files in JSON, and store them in `mymodule/javascript/lang/src/*.js`. In your `.tx/config` you can configure this path as a separate master location.
```ruby
:::ruby
[main]
host = https://www.transifex.com
@ -115,16 +117,17 @@ First of all, you need to create those source files in JSON, and store them in `
source_file = javascript/lang/src/en.js
source_lang = en
type = KEYVALUEJSON
```
Then you can upload the source files via a normal `tx push`. Once translations come in, you need to convert the source
files back into the JS files SilverStripe can actually read. This requires an installation of our
[buildtools](https://github.com/silverstripe/silverstripe-buildtools).
```
tx pull
(cd .. && phing -Dmodule=mymodule translation-generate-javascript-for-module)
git add javascript/lang/*
git commit -m "Updated javascript translations"
```
# Related
* [i18n](/developer_guides/i18n/): Developer-level documentation of Silverstripe's i18n capabilities

View File

@ -25,19 +25,23 @@ except when necessitated by third party conventions (e.g using PHP's `Serializab
SilverStripe's [Config API]() can read its defaults from variables declared as `private static` on classes.
As opposed to other variables, these should be declared as lower case with underscores.
:::php
```php
class MyClass
{
private static $my_config_variable = 'foo';
}
```
## Prefer identical (===) comparisons over equality (==)
Where possible, use type-strict identical comparisons instead of loosely typed equality comparisons.
Read more in the PHP documentation for [comparison operators](http://php.net/manual/en/language.operators.comparison.php) and [object comparison](http://php.net/manual/en/language.oop5.object-comparison.php).
:::php
```php
// good - only need to cast to (int) if $a might not already be an int
if ((int)$a === 100) {
doThis();
@ -47,13 +51,15 @@ Read more in the PHP documentation for [comparison operators](http://php.net/man
if ($a == 100) {
doThis();
}
```
## Separation of Logic and Presentation
Try to avoid using PHP's ability to mix HTML into the code.
:::php
```php
// PHP code
public function getTitle() {
return "<h2>Bad Example</h2>";
@ -61,10 +67,13 @@ Try to avoid using PHP's ability to mix HTML into the code.
// Template code
$Title
```
Better: Keep HTML in template files:
:::php
```php
// PHP code
public function getTitle() {
return "Better Example";
@ -72,6 +81,7 @@ Better: Keep HTML in template files:
// Template code
<h2>$Title</h2>
```
## Comments
@ -87,7 +97,9 @@ and [tag overview](http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumen
Example:
:::php
```php
/**
* My short description for this class.
* My longer description with
@ -103,7 +115,6 @@ Example:
*/
class MyClass extends Class
{
/**
* My Method.
* This method returns something cool. {@link MyParentMethod} has other cool stuff in it.
@ -117,6 +128,7 @@ Example:
}
}
```
## Class Member Ordering
@ -137,16 +149,22 @@ Put code into the classes in the following order (where applicable).
If you have to use raw SQL, make sure your code works across databases. Make sure you escape your queries like below,
with the column or table name escaped with double quotes as below.
:::php
```php
MyClass::get()->where(array("\"Score\" > ?" => 50));
```
It is preferable to use parameterised queries whenever necessary to provide conditions
to a SQL query, where values placeholders are each replaced with a single unquoted question mark.
If it's absolutely necessary to use literal values in a query make sure that values
are single quoted.
:::php
```php
MyClass::get()->where("\"Title\" = 'my title'");
```
Use [ANSI SQL](http://en.wikipedia.org/wiki/SQL#Standardization) format where possible.