Skip to content

Commit

Permalink
Merge pull request #3 from ninty9notout/master
Browse files Browse the repository at this point in the history
Started port to SS4 + cleaned up code a little
  • Loading branch information
stevie-mayhew authored Mar 21, 2017
2 parents da45ca9 + 3d7efe0 commit cf9cf6a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 37 deletions.
12 changes: 6 additions & 6 deletions README.md
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.
14 changes: 7 additions & 7 deletions _config/hasoneedit.yml
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
39 changes: 29 additions & 10 deletions code/DataObjectExtension.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
<?php

class sgn_hasoneedit_DataObjectExtension extends DataExtension {
const separator = '-_1_-';
namespace SGN\HasOneEdit;

public function onBeforeWrite() {
use SilverStripe\ORM\DataExtension;

class DataObjectExtension extends DataExtension
{
/**
* @var string
*/
const SEPARATOR = '-_1_-';

/**
* @see {@link SilverStripe\ORM\DataObject->onBeforeWrite()}
*/
public function onBeforeWrite()
{
$changed = $this->owner->getChangedFields();
$toWrite = array();
foreach($changed as $name => $value) {
if(!strpos($name, self::separator)) {

foreach ($changed as $name => $value) {

if (!strpos($name, self::SEPARATOR)) {
// Also skip $name that starts with a separator
continue;
}

$value = (string)$value['after'];
list($hasone, $key) = explode(self::separator, $name, 2);
if($this->owner->has_one($hasone) || $this->owner->belongs_to($hasone)) {
$rel = $this->owner->getComponent($hasone);

// Get original:
$original = (string)$rel->__get($key);
if($original !== $value) {
$original = (string) $rel->__get($key);

if ($original !== $value) {
$rel->setCastedField($key, $value);
$toWrite[$hasone] = $rel;
$toWrite[$hasOne] = $rel;
}
}
}
foreach($toWrite as $rel => $obj) {

foreach ($toWrite as $rel => $obj) {
$obj->write();

$key = $rel . 'ID';
if(!$this->owner->$key) {

if (!$this->owner->$key) {
$this->owner->$key = $obj->ID;
}
}
Expand Down
43 changes: 31 additions & 12 deletions code/UpdateFormExtension.php
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);
}
}
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@
"homepage": "http://simon.geek.nz"
}],
"require": {
"silverstripe/framework": "~3.0"
"silverstripe/framework": "~4.0.0-alpha3"
},
"replace" : {
"simonwelsh/hasoneedit": "*"
}
},
"autoload": {
"psr-4": {"SGN\\HasOneEdit\\": "code/"}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}

0 comments on commit cf9cf6a

Please sign in to comment.