Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement inline creation of records.
* Builds on the editable cols component with GridFieldAddNewInlineButton. * Closes #2.
- Loading branch information
Showing
7 changed files
with
202 additions
and
2 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
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,142 @@ | ||
<?php | ||
/** | ||
* Builds on the {@link GridFieldEditableColumns} component to allow creating new records. | ||
*/ | ||
class GridFieldAddNewInlineButton implements GridField_HTMLProvider, GridField_SaveHandler { | ||
|
||
private $fragment; | ||
|
||
private $title; | ||
|
||
/** | ||
* @param string $fragment the fragment to render the button in | ||
*/ | ||
public function __construct($fragment = 'buttons-before-left') { | ||
$this->setFragment($fragment); | ||
$this->setTitle(_t('GridFieldExtensions.ADD', 'Add')); | ||
} | ||
|
||
/** | ||
* Gets the fragment name this button is rendered into. | ||
* | ||
* @return string | ||
*/ | ||
public function getFragment() { | ||
return $this->fragment; | ||
} | ||
|
||
/** | ||
* Sets the fragment name this button is rendered into. | ||
* | ||
* @param string $fragment | ||
*/ | ||
public function setFragment($fragment) { | ||
$this->fragment = $fragment; | ||
} | ||
|
||
/** | ||
* Gets the button title text. | ||
* | ||
* @return string | ||
*/ | ||
public function getTitle() { | ||
return $this->title; | ||
} | ||
|
||
/** | ||
* Sets the button title text. | ||
* | ||
* @param string $title | ||
*/ | ||
public function setTitle($title) { | ||
$this->title = $title; | ||
} | ||
|
||
public function getHTMLFragments($grid) { | ||
$class = $grid->getModelClass(); | ||
$fragment = $this->getFragment(); | ||
|
||
if(!$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns')) { | ||
throw new Exception('Inline adding requires the editable columns component'); | ||
} | ||
|
||
if(!singleton($class)->canCreate()) { | ||
return false; | ||
} | ||
|
||
Requirements::javascript(THIRDPARTY_DIR . '/javascript-templates/tmpl.js'); | ||
GridFieldExtensions::include_requirements(); | ||
|
||
$data = new ArrayData(array( | ||
'Title' => $this->getTitle(), | ||
)); | ||
|
||
return array( | ||
$fragment => $data->renderWith(__CLASS__), | ||
'after' => $this->getRowTemplate($grid, $editable) | ||
); | ||
} | ||
|
||
private function getRowTemplate(GridField $grid, GridFieldEditableColumns $editable) { | ||
$class = $grid->getModelClass(); | ||
|
||
$columns = new ArrayList(); | ||
$handled = array_keys($editable->getDisplayFields($grid)); | ||
|
||
$record = new $class(); | ||
$fields = $editable->getFields($grid, $record); | ||
|
||
foreach($grid->getColumns() as $column) { | ||
if(in_array($column, $handled)) { | ||
$field = $fields->dataFieldByName($column); | ||
$field->setName(sprintf( | ||
'%s[%s][{%%=o.num%%}][%s]', $grid->getName(), __CLASS__, $field->getName() | ||
)); | ||
|
||
$content = $field->Field(); | ||
} else { | ||
$content = null; | ||
} | ||
|
||
$attrs = ''; | ||
|
||
foreach($grid->getColumnAttributes($record, $column) as $attr => $val) { | ||
$attrs .= sprintf(' %s="%s"', $attr, Convert::raw2att($val)); | ||
} | ||
|
||
$columns->push(new ArrayData(array( | ||
'Content' => $content, | ||
'Attributes' => $attrs, | ||
'IsActions' => $column == 'Actions' | ||
))); | ||
} | ||
|
||
return $columns->renderWith('GridFieldAddNewInlineRow'); | ||
} | ||
|
||
public function handleSave(GridField $grid, DataObjectInterface $record) { | ||
$value = $grid->Value(); | ||
|
||
if(!isset($value[__CLASS__]) || !is_array($value[__CLASS__])) { | ||
return; | ||
} | ||
|
||
$class = $grid->getModelClass(); | ||
$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns'); | ||
$form = $editable->getForm($grid, $record); | ||
|
||
if(!singleton($class)->canCreate()) { | ||
return; | ||
} | ||
|
||
foreach($value[__CLASS__] as $fields) { | ||
$item = new $class(); | ||
|
||
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING); | ||
$form->saveInto($item); | ||
|
||
$grid->getList()->add($item); | ||
} | ||
} | ||
|
||
} |
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<button href="$Link" class="ss-gridfield-add-new-inline ss-ui-action-constructive ss-ui-button" data-icon="add"> | ||
$Title | ||
</button> |
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,13 @@ | ||
<script type="text/x-tmpl" class="ss-gridfield-add-inline-template"> | ||
<tr class="ss-gridfield-inline-new"> | ||
<% loop $Me %> | ||
<% if $IsActions %> | ||
<td$Attributes> | ||
<button class="ss-gridfield-delete-inline gridfield-button-delete ss-ui-button" data-icon="cross-circle"></button> | ||
</td> | ||
<% else %> | ||
<td$Attributes>$Content</td> | ||
<% end_if %> | ||
<% end_loop %> | ||
</tr> | ||
</script> |