-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ninty9notout/master
Started port to SS4 + cleaned up code a little
- Loading branch information
Showing
5 changed files
with
85 additions
and
37 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
# Has One Edit | ||
|
||
This module allows you to directly edit the fields of a related has\_one object directly, without having to mess around with GridField or links to ModelAdmin. If the related has\_one doesn't exist yet, then this module also creates the object and sets up the relation for you on first write. | ||
This module allows you to directly edit the fields of a related `has_one` object directly, without having to mess around with `GridField` or links to `ModelAdmin`. If the related `has_one` doesn't exist yet, then this module also creates the object and sets up the relation for you on first write. | ||
|
||
This module has been tested editing a has\_one in both a GridFieldDetailForm and on a generic Page in CMSMain. | ||
This module has been tested editing a `has_one` in both a `GridFieldDetailForm` and on a generic `Page` in `CMSMain`. | ||
|
||
## Requirements | ||
|
||
SilverStripe 3.x. | ||
SilverStripe 4.x. | ||
|
||
This module has been tested on both 3.0.x-dev and 3.1.x-dev. There is no separate version for the two different Framework branches. | ||
Basic testing has been carried out on 4.0.x-dev. Extensive testing may be required as SS4 becomes more solid. | ||
|
||
## Usage | ||
|
||
To use this module, simply add a field to the CMS fields for your object in your `getCMSFields()` method. The name of the field should be `HasOneName-_1_-FieldName`. | ||
|
||
For example, say you have a has\_one called `Show` and that has\_one has a field called `Title` you want to edit. You'd add the field `TextField::create('Show-_1_-Title', 'Show Title')`. | ||
For example, say you have a `has_one` called `Show` and that `has_one` has a field called `Title` you want to edit. You'd add the field `TextField::create('Show-_1_-Title', 'Show Title')`. | ||
|
||
If you do not require that the outputted name of the field matches the value you supply, you can also use a colon as a separator instead of `-_1_-`. | ||
|
||
### Using with your own form | ||
|
||
To add support to your own forms, you need to add the `sgn_hasoneedit_UpdateFormExtension` extension to your controller and call `$this->extend('updateEditForm', $form)` before returning the form to the template. Without this, the fields will not get populated with the values from the has\_one though saving will work. | ||
To add support to your own forms, you need to add the `SGN\HasOneEdit\UpdateFormExtension` extension to your controller and call `$this->extend('updateEditForm', $form)` before returning the form to the template. Without this, the fields will not get populated with the values from the `has_one` though saving will work. |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
--- | ||
Name: hasoneedit | ||
After: '*' | ||
After: framework/* | ||
--- | ||
CMSMain: | ||
SilverStripe\CMS\Controllers\CMSMain: | ||
extensions: | ||
- 'sgn_hasoneedit_UpdateFormExtension' | ||
GridFieldDetailForm_ItemRequest: | ||
- SGN\HasOneEdit\UpdateFormExtension | ||
SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest: | ||
extensions: | ||
- 'sgn_hasoneedit_UpdateFormExtension' | ||
DataObject: | ||
- SGN\HasOneEdit\UpdateFormExtension | ||
SilverStripe\ORM\DataObject: | ||
extensions: | ||
- 'sgn_hasoneedit_DataObjectExtension' | ||
- SGN\HasOneEdit\DataObjectExtension |
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
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 |
---|---|---|
@@ -1,43 +1,62 @@ | ||
<?php | ||
|
||
class sgn_hasoneedit_UpdateFormExtension extends \Extension { | ||
public function updateEditForm(\Form $form) { | ||
namespace SGN\HasOneEdit; | ||
|
||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Forms\Form; | ||
|
||
class UpdateFormExtension extends Extension | ||
{ | ||
/** | ||
* @param Form $form | ||
*/ | ||
public function updateEditForm(Form $form) | ||
{ | ||
$record = $form->getRecord(); | ||
$fields = $form->Fields()->dataFields(); | ||
|
||
foreach($fields as $name => $field) { | ||
$name = str_replace(array(':', '/'), sgn_hasoneedit_DataObjectExtension::separator, $name); | ||
if(!strpos($name, sgn_hasoneedit_DataObjectExtension::separator)) { | ||
foreach ($fields as $name => $field) { | ||
$name = str_replace(array(':', '/'), DataObjectExtension::SEPARATOR, $name); | ||
|
||
if (!strpos($name, DataObjectExtension::SEPARATOR)) { | ||
// Also skip $name that starts with a separator | ||
continue; | ||
} | ||
|
||
$field->setName($name); | ||
if(!$record) { | ||
|
||
if (!$record) { | ||
continue; | ||
} | ||
if($field->Value()) { | ||
|
||
if ($field->Value()) { | ||
// Skip fields that already have a value | ||
continue; | ||
} | ||
list($hasone, $key) = explode(sgn_hasoneedit_DataObjectExtension::separator, $name, 2); | ||
if($record->has_one($hasone)) { | ||
$rel = $record->getComponent($hasone); | ||
|
||
list($hasOne, $key) = explode(DataObjectExtension::SEPARATOR, $name, 2); | ||
|
||
if ($record->hasOne($hasOne)) { | ||
$rel = $record->getComponent($hasOne); | ||
// Copied from loadDataFrom() | ||
$exists = ( | ||
isset($rel->$key) || | ||
$rel->hasMethod($key) || | ||
($rel->hasMethod('hasField') && $rel->hasField($key)) | ||
); | ||
|
||
if($exists) { | ||
if ($exists) { | ||
$value = $rel->__get($key); | ||
$field->setValue($value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function updateItemEditForm(\Form $form) { | ||
/** | ||
* @param Form $form | ||
*/ | ||
public function updateItemEditForm(Form $form) { | ||
$this->updateEditForm($form); | ||
} | ||
} |
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