Skip to content

Commit

Permalink
Prevent accidentally saving Neo field as different type
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Jun 16, 2024
1 parent aead997 commit a6bbf2c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Changed
- The Neo field type is now listed in the field type selection dropdown, and the save buttons replaced with a message indicating lack of support for saving the field type, preventing the possibility of accidentally saving a Neo field as a different field type

## 2.0.7 - 2023-10-16

### Fixed
Expand Down
24 changes: 24 additions & 0 deletions client/src/scripts/FieldModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ interface FieldModalInterface extends GarnishModal {
$deleteBtn: JQuery
$deleteSpinner: JQuery
$loadSpinner: JQuery
$cancelBtn: JQuery
$saveBtn: JQuery
$saveCopyBtn: JQuery
$unsupportedMessage: JQuery
$saveSpinner: JQuery
$observed: JQuery
executedJs: Record<string, boolean>
Expand All @@ -44,6 +46,10 @@ interface SettingsEvent extends Event {
$css: JQuery
}

const unsupportedFieldTypes = [
'benf\\neo\\Field'
]

/**
* FieldModal class
* Handles the modal window for creating new fields.
Expand Down Expand Up @@ -129,6 +135,7 @@ const FieldModal = Garnish.Modal.extend({
this.$cancelBtn = $('<div class="btn disabled" role="button">').text(Craft.t('quick-field', 'Cancel')).appendTo(this.$rightButtons)
this.$saveBtn = $('<div class="btn submit disabled" role="button">').text(Craft.t('quick-field', 'Save')).appendTo(this.$rightButtons)
this.$saveCopyBtn = $('<div class="btn submit disabled hidden" role="button">').text(Craft.t('quick-field', 'Save as a new field')).appendTo(this.$rightButtons)
this.$unsupportedMessage = $('<p class="error hidden">').text(Craft.t('quick-field', 'Saving the selected field type is unsupported.')).appendTo(this.$rightButtons)
this.$saveSpinner = $('<div class="spinner hidden">').appendTo(this.$rightButtons)

this.setContainer($container)
Expand Down Expand Up @@ -341,6 +348,23 @@ const FieldModal = Garnish.Modal.extend({

that.on('runExternalScripts', callback)
that.runExternalScripts(Object.keys(that.executedJs))

const fieldTypeDropdown = that.$main.find('select[name="qf[type]"]')
const checkSupportedFieldType: () => void = () => {
const selectedFieldType = fieldTypeDropdown.val() as string

if (unsupportedFieldTypes.includes(selectedFieldType)) {
this.$saveBtn.detach()
this.$saveCopyBtn.detach()
this.$unsupportedMessage.removeClass('hidden')
} else {
this.$cancelBtn.after(this.$saveBtn)
this.$saveBtn.after(this.$saveCopyBtn)
this.$unsupportedMessage.addClass('hidden')
}
}
checkSupportedFieldType()
fieldTypeDropdown.on('change', checkSupportedFieldType)
},

/**
Expand Down
4 changes: 4 additions & 0 deletions client/src/styles/modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@
margin: 0;
}
}

.error {
margin: 0;
}
}
}
21 changes: 1 addition & 20 deletions src/controllers/QuickFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace spicyweb\quickfield\controllers;

use benf\neo\Field as NeoField;
use Craft;
use craft\base\Field;
use craft\fieldlayoutelements\CustomField;
Expand Down Expand Up @@ -271,7 +270,7 @@ private function _getTemplate(Field $field = null): array
}

$fieldsService = Craft::$app->getFields();
$fieldTypes = $this->_getFieldTypes();
$fieldTypes = Craft::$app->getFields()->getAllFieldTypes();
$fieldTypeOptions = [];
$supportedTranslationMethods = [];
$compatibleFieldTypes = [];
Expand Down Expand Up @@ -314,22 +313,4 @@ private function _getTemplate(Field $field = null): array
'css' => $view->getHeadHtml(),
];
}

/**
* Returns the field types that Quick Field supports creating.
*
* @return string[]
*/
private function _getFieldTypes(): array
{
$fieldTypes = Craft::$app->getFields()->getAllFieldTypes();

if (Craft::$app->getPlugins()->getPlugin('neo')) {
return array_filter($fieldTypes, function($fieldType) {
return !($fieldType === NeoField::class);
});
}

return $fieldTypes;
}
}
2 changes: 1 addition & 1 deletion src/resources/css/main.css

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

2 changes: 1 addition & 1 deletion src/resources/css/main.css.map

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

2 changes: 1 addition & 1 deletion src/resources/js/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/resources/js/main.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/translations/en/quick-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'Rename' => 'Rename',
'Save' => 'Save',
'Save as a new field' => 'Save as a new field',
'Saving the selected field type is unsupported.' => 'Saving the selected field type is unsupported.',
'Settings' => 'Settings',
'The field requested to delete no longer exists.' => 'The field requested to delete no longer exists.',
'The field requested to edit no longer exists.' => 'The field requested to edit no longer exists.',
Expand Down

0 comments on commit a6bbf2c

Please sign in to comment.