Skip to content

Commit

Permalink
Fix Upload Field (#35)
Browse files Browse the repository at this point in the history
* Refactor logic around regular form architecture so UploadField can works

---------

Co-authored-by: Maxime Rainville <maxime@silverstripe.com>
  • Loading branch information
maxime-rainville and Maxime Rainville authored Sep 18, 2024
1 parent 7087ed4 commit f90adbd
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 34 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/src/components/AnyPicker/AnyPicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@

&__handle {
position: relative;
margin-right: 6px;
padding-right: 6px;
line-height: 0;
vertical-align: middle;
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/AnyPicker/AnyPickerMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const AnyPickerMenu = ({ allowedDataObjectClasses, onSelect, baseDataObjectName,
toggle={toggle}
className="any-picker-menu"
>
<DropdownToggle className={`any-picker-menu__toggle font-icon-${baseDataObjectIcon || 'plus-1'}`} caret>
<DropdownToggle className={`any-picker-menu__toggle ${baseDataObjectIcon || 'plus-1'}`} caret>
{
i18n.sprintf(
i18n._t('AnyField.ADD_DATAOBJECT', 'Add %s'),
Expand All @@ -28,7 +28,7 @@ const AnyPickerMenu = ({ allowedDataObjectClasses, onSelect, baseDataObjectName,
</DropdownToggle>
<DropdownMenu>
{allowedDataObjectClasses.map(({ key, title, icon }) =>
<DropdownItem className={`font-icon-${icon || 'link'}`} key={key} onClick={() => onSelect(key)}>{title}</DropdownItem>
<DropdownItem className={`${icon || 'link'}`} key={key} onClick={() => onSelect(key)}>{title}</DropdownItem>
)}
</DropdownMenu>
</Dropdown>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/AnyPicker/AnyPickerTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AnyPickerTitle = ({ title, dataObjectClass, description, onClear, onClick,
id={id}
>
{sortable && <AnyPickerTitleHandle />}
<span className={`font-icon-${dataObjectClass.icon || 'link'} any-picker-title__icon` } />
<span className={`${dataObjectClass.icon || 'link'} any-picker-title__icon` } />
<div className="any-picker-title__detail">
<div className="any-picker-title__title">{title}</div>
<small className="any-picker-title__type">
Expand Down
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"require-dev": {
"silverstripe/recipe-testing": "^3",
"squizlabs/php_codesniffer": "^3",
"maxime-rainville/silverstripe-linkfield-tester": "dev-master",
"silverstripe/linkfield": "dev-pulls/3/anyfield-compat"
"maxime-rainville/anyfield-tester": "dev-master"
},
"license": "BSD-3-Clause",
"authors": [
Expand Down Expand Up @@ -44,11 +43,5 @@
"process-timeout": 600
},
"minimum-stability": "dev",
"prefer-stable": true,
"repositories": {
"silverstripe/linkfield": {
"type": "vcs",
"url": "git@github.com:creative-commoners/silverstripe-linkfield.git"
}
}
"prefer-stable": true
}
3 changes: 2 additions & 1 deletion src/Extensions/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\AnyField\Extensions;

use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField;
use SilverStripe\ORM\DataExtension;

/**
Expand All @@ -22,6 +23,6 @@ class Sortable extends DataExtension

public function updateCMSFields(FieldList $fields)
{
$fields->removeByName('Sort');
$fields->add(HiddenField::create('Sort'));
}
}
33 changes: 24 additions & 9 deletions src/Services/AnyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\InjectorNotFoundException;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\HiddenField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\SS_List;

Expand Down Expand Up @@ -41,6 +44,10 @@ public function generateDescription(string $className, array $data): array
$this->instanceOfDataObject($dummy);
$summary = $dummy->hasMethod('getSummary') ? (string)$dummy->getSummary() : '';

if (empty($summary) && $dummy->hasMethod('getDescription')) {
$summary = (string)$dummy->getDescription();
}

return [
'title' => $dummy->getTitle(),
'description' => $summary,
Expand All @@ -50,11 +57,14 @@ public function generateDescription(string $className, array $data): array
/**
* Given a DataObject, return a map of its fields so it can be edited in a AnyField
*/
public function map(DataObject $value): array
public function map(DataObject $record): array
{
$data = $value->toMap();
$data['dataObjectClassKey'] = $value->ClassName;
unset($data['ClassName']);
$fieldlist = $record->getCMSFields();
$fieldlist->add(HiddenField::create('ID'));
$form = Form::create(null, null, $fieldlist, FieldList::create());
$form->loadDataFrom($record);
$data = $form->getData();
$data['dataObjectClassKey'] = $record->ClassName;
return $data;
}

Expand Down Expand Up @@ -103,11 +113,16 @@ public function setData(DataObject $record, array $data): DataObject
}
}

foreach ($data as $key => $value) {
if ($key !== 'ID' && $record->hasField($key)) {
$record->setField($key, $value);
}
}
$fieldlist = $record->getCMSFields();
$form = Form::create(null, null, $fieldlist, FieldList::create());
$form->loadDataFrom($data);
$form->saveInto($record);

// foreach ($data as $key => $value) {
// if ($key !== 'ID' && $record->hasField($key)) {
// $record->setField($key, $value);
// }
// }

return $record;
}
Expand Down
21 changes: 12 additions & 9 deletions tests/behat/features/manage-single-link.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Feature: Manage Single item
And I should see an edit page form
And I click the "Link test" CMS tab


Scenario: I can fill an empty AnyField with a link
And I should see an empty "My test link" AnyField
Then I edit the "My test link" AnyField
Expand All @@ -27,7 +26,7 @@ Feature: Manage Single item
Then I add a "Site Tree Link" item to the "My test link" AnyField
And I should see a "Site Tree Link" AnyField modal
Then I select "Contact me" in the "#Form_ModalsAnyFieldForm_PageID_Holder" tree dropdown
And I fill in "Title" with "Test link site tree link"
And I fill in "Link text" with "Test link site tree link"
And I press the "Insert link" button
And I should see a "My test link" AnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-me"
Then I press the "Save" button
Expand All @@ -37,12 +36,13 @@ Feature: Manage Single item
Then I edit the "My test link" AnyField
And I add a "Site Tree Link" item to the "My test link" AnyField
And I select "Contact me" in the "#Form_ModalsAnyFieldForm_PageID_Holder" tree dropdown
And I fill in "Title" with "Test link site tree link"
And I fill in "Link text" with "Test link site tree link"
And I press the "Insert link" button
And I press the "Save" button
Then I should see a "My test link" AnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-me"
And I should see a clear button in the "My test link" AnyField
Then I clear the "My test link" AnyField
And I wait for 2 seconds
And I should see an empty "My test link" AnyField
Then I press the "Save" button
And I should see an empty "My test link" AnyField
Expand All @@ -51,12 +51,12 @@ Feature: Manage Single item
Then I edit the "My test link" AnyField
And I add a "Site Tree Link" item to the "My test link" AnyField
And I select "Contact me" in the "#Form_ModalsAnyFieldForm_PageID_Holder" tree dropdown
And I fill in "Title" with "Test link site tree link"
And I fill in "Link text" with "Test link site tree link"
And I press the "Insert link" button
And I press the "Save" button
Then I should see a "My test link" AnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-me"
Then I edit the "My test link" AnyField
And I fill in "Title" with "My udated test link"
And I fill in "Link text" with "My udated test link"
And I press the "Insert link" button
Then I should see a "My test link" AnyField filled with "My udated test link" and a description of "Site Tree Link: contact-me"
Then I press the "Save" button
Expand All @@ -65,7 +65,7 @@ Feature: Manage Single item
Scenario: I can fill a AnyField with an external item
Then I edit the "My test link" AnyField
And I add a "External Link" item to the "My test link" AnyField
Then I fill in "Title" with "Silverstripe"
Then I fill in "Link text" with "Silverstripe"
And I fill in "External url" with "https://www.silverstripe.org"
Then I press the "Insert link" button
And I press the "Save" button
Expand All @@ -82,17 +82,20 @@ Feature: Manage Single item
Scenario: I can fill a AnyField with a file link
Then I edit the "My test link" AnyField
And I add a "File Link" item to the "My test link" AnyField
Then I fill in "Link text" with "A file link"
And I press the "Choose existing" button
And I click on the ".insert-media-modal .toolbar__back-button" element
And I click on the ".gallery__files .gallery-item__thumbnail" element
Then I fill in "Link description" with "A file link"
Then I press the "Link to file" button
Then I press the "Insert" button
Then I press the "Insert link" button
And I press the "Save" button
Then I should see a "My test link" AnyField filled with "A file link" and a description of "File Link: file2.jpg"

Scenario: I can fill a AnyField with a phone link
Then I edit the "My test link" AnyField
And I add a "Phone Link" item to the "My test link" AnyField
Then I fill in "Phone" with "111"
And I fill in "Title" with "NZ Emergency services"
And I fill in "Link text" with "NZ Emergency services"
Then I press the "Insert link" button
And I press the "Save" button
Then I should see a "My test link" AnyField filled with "NZ Emergency services" and a description of "Phone Link: 111"

0 comments on commit f90adbd

Please sign in to comment.