-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove unnecessary templates, use onBeforeRender
- Loading branch information
Uncle Cheese
committed
Dec 22, 2014
0 parents
commit 2871854
Showing
15 changed files
with
1,268 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Display Logic Module for SilverStripe 3 | ||
|
||
## Description | ||
The Display Logic module allows you to add conditions for displaying or hiding certain form fields based on client-side behavior. | ||
|
||
## Example usage | ||
```php | ||
<?php | ||
$products->displayIf("HasProducts")->isChecked(); | ||
|
||
$sizes->hideUnless("ProductType")->isEqualTo("t-shirt") | ||
->andIf("Price")->isGreaterThan(10); | ||
|
||
$payment->hideIf("Price")->isEqualTo(0); | ||
|
||
$shipping->displayIf("ProductType")->isEqualTo("furniture") | ||
->andIf() | ||
->group() | ||
->orIf("RushShipping")->isChecked() | ||
->orIf("ShippingAddress")->isNotEmpty() | ||
->end(); | ||
``` | ||
|
||
## Available comparison functions | ||
- isEqualTo | ||
- isNotEqualTo | ||
- isGreaterThan | ||
- isLessThan | ||
- contains | ||
- startsWith | ||
- endsWith | ||
- isEmpty | ||
- isNotEmpty | ||
- isBetween | ||
- isChecked | ||
- hasCheckedOption | ||
- hasCheckedAtLeast | ||
- hasCheckedLessThan | ||
|
||
## Available display conditions | ||
- displayIf() | ||
- displayUnless() | ||
- hideIf() | ||
- hideUnless() | ||
|
||
## Kitchen sink example | ||
```php | ||
<?php | ||
public function getCMSFields() { | ||
$fields = parent::getCMSFields(); | ||
$fields->addFieldsToTab("Root.Test", array( | ||
TextField::create("Name","Name of event"), | ||
TextField::create("VenueSize","Size of venue"), | ||
$refreshments = CheckboxField::create("Refreshments","This is a large venue. Are there refreshments?"), | ||
$vendors = CheckboxSetField::create("Vendors","Vendors", Member::get()->map()), | ||
$tent = TextField::create("TentSize","You're going to need a tent. What size is it?"), | ||
|
||
OptionSetField::create("LinkType", "", array('internal' => 'Link to an internal page', 'external' => 'Link to an external page')), | ||
$internal = DropdownField::create("InternalLinkID", "Choose a page", SiteTree::get()->map()->toArray())->setEmptyString("-- choose --"), | ||
$external = TextField::create("ExternalLink", "Link to external page"), | ||
$label = TextField::create("LinkLabel", "Label for link"), | ||
|
||
$useEmbed = CheckboxField::create("UseEmbedCode","I have embed code"), | ||
$embed = TextareaField::create("EmbedCode","Enter the embed code.") | ||
|
||
)); | ||
|
||
$refreshments->displayIf("VenueSize")->isGreaterThan(100); | ||
$vendors->displayIf("Refreshments")->isChecked(); | ||
$tent->displayIf("Vendors")->hasCheckedAtLeast(3); | ||
|
||
|
||
$internal->displayIf("LinkType")->isEqualTo("internal"); | ||
$external->displayIf("LinkType")->isEqualTo("external"); | ||
$label->displayIf("LinkType")->isEqualTo("internal")->orIf("LinkType")->isEqualTo("external"); | ||
|
||
$useEmbed->displayIf("LinkType")->isEqualTo("external"); | ||
$embed->displayIf("UseEmbedCode")->isChecked() | ||
->orIf() | ||
->group() | ||
->orIf("ExternalLink")->contains("youtube.com") | ||
->orIf("ExternalLink")->contains("vimeo.com") | ||
->end(); | ||
|
||
|
||
return $fields; | ||
} | ||
``` | ||
|
||
## Kitchen sink example, with chaining | ||
```php | ||
<?php | ||
public function getCMSFields() { | ||
$fields = parent::getCMSFields(); | ||
$fields->addFieldsToTab("Root.Test", array( | ||
TextField::create("Name","Name of event"), | ||
TextField::create("VenueSize","Size of venue"), | ||
CheckboxField::create("Refreshments","This is a large venue. Are there refreshments?") | ||
->displayIf("VenueSize")->isGreaterThan(100)->end(), | ||
CheckboxSetField::create("Vendors","Vendors", StaffMember::get()->map()) | ||
->displayIf("Refreshments")->isChecked()->end(), | ||
TextField::create("TentSize","You're going to need a tent. What size is it?") | ||
->displayIf("Vendors")->hasCheckedAtLeast(3)->end(), | ||
|
||
OptionSetField::create("LinkType", "", array('internal' => 'Link to an internal page', 'external' => 'Link to an external page')), | ||
DropdownField::create("InternalLinkID", "Choose a page", SiteTree::get()->map()->toArray())->setEmptyString("-- choose --") | ||
->displayIf("LinkType")->isEqualTo("internal") | ||
->end(), | ||
TextField::create("ExternalLink", "Link to external page") | ||
->displayIf("LinkType")->isEqualTo("external") | ||
->end(), | ||
TextField::create("LinkLabel", "Label for link") | ||
->displayIf("LinkType")->isChecked()->end(), | ||
CheckboxField::create("UseEmbedCode","I have embed code") | ||
->displayIf("LinkType")->isEqualTo("external") | ||
->end(), | ||
TextareaField::create("EmbedCode","Enter the embed code.") | ||
->displayIf("UseEmbedCode")->isChecked() | ||
->orIf() | ||
->group() | ||
->orIf("ExternalLink")->contains("youtube.com") | ||
->orIf("ExternalLink")->contains("vimeo.com") | ||
->end() | ||
)); | ||
|
||
return $fields; | ||
} | ||
``` | ||
|
||
## Dealing with non-standard form fields | ||
Sometimes you will want to wrap display logic around a form field that does not use the standard FormField template, such as GridField or LiteralField. For these cases, you can wrap the form field in DisplayLogicWrapper(). | ||
```php | ||
$fields->addFieldToTab("Root.Main", DisplayLogicWrapper::create( | ||
LiteralField::create("foo","<h2>Hello</h2>") | ||
) | ||
->displayIf("Title")->isEmpty()->end() | ||
); | ||
``` | ||
|
||
## What's the difference between displayIf() and hideUnless()? | ||
Not much. hideUnless() will take a CSS class that hides it by default before the script loads, to eliminate any flash of form fields before the script has loaded. In general, use displayIf() when the common case is for the field to be hidden, and hideUnless() when the common case is for the field to be shown. | ||
The same logic applies to hideIf() and displayUnless(). | ||
|
||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
define('DISPLAY_LOGIC_DIR', basename(dirname(__FILE__))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
DisplayLogicCriteria: | ||
comparisons: | ||
- isEqualTo | ||
- isNotEqualTo | ||
- isGreaterThan | ||
- isLessThan | ||
- contains | ||
- startsWith | ||
- endsWith | ||
- isEmpty | ||
- isNotEmpty | ||
- isBetween | ||
- isChecked | ||
- hasCheckedOption | ||
- hasCheckedAtLeast | ||
- hasCheckedLessThan | ||
|
||
FormField: | ||
extensions: | ||
- DisplayLogicFormField | ||
DisplayLogic: | ||
jquery_included: true |
Oops, something went wrong.