Skip to content

Commit

Permalink
Only show field type options where there's a chance the data will map…
Browse files Browse the repository at this point in the history
… over to the new column type

Fixes #1 and #4
  • Loading branch information
brandonkelly committed Feb 4, 2017
1 parent 02aadbb commit 8870c8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Craft CMS 3.0 Working Changelog
- The “Set status” batch element action now updates elements’ site statuses in addition to their global statuses, when setting the status to Enabled.
- Editable tables now support a `radioMode` checkbox column option, which prevents more than one of the column’s checkboxes from being checked at a time.
- `craft\helpers\Db::getNumericalColumnType()` no longer returns unsigned integer column types for MySQL.
- The “Field Type” setting on Edit Field pages no longer shows field type options where there’s no chance the existing field data will map over.

### Removed
- Removed the `afterSetStatus` event from `craft\elements\actions\SetStatus`.
Expand Down
36 changes: 35 additions & 1 deletion src/controllers/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\base\FieldInterface;
use craft\fields\MissingField;
use craft\fields\PlainText;
use craft\helpers\Db;
use craft\helpers\UrlHelper;
use craft\models\FieldGroup;
use craft\web\Controller;
Expand Down Expand Up @@ -145,7 +146,40 @@ public function actionEditField(int $fieldId = null, FieldInterface $field = nul
// Field types
// ---------------------------------------------------------------------

$allFieldTypes = $fieldsService->getAllFieldTypes();
/** @var string[]|FieldInterface[] $allFieldTypes */

if (!$field->id) {
// Can be anything
$allFieldTypes = $fieldsService->getAllFieldTypes();
} else if ($field::hasContentColumn()) {
// Can only be field types with compatible column types
$allFieldTypes = $fieldsService->getAllFieldTypes();
$fieldColumnType = $field->getContentColumnType();

foreach ($allFieldTypes as $i => $class) {
if ($class === get_class($field)) {
continue;
}

if (!$class::hasContentColumn()) {
$compatible = false;
} else {
/** @var FieldInterface $tempField */
$tempField = new $class();
$compatible = Db::areColumnTypesCompatible($fieldColumnType, $tempField->getContentColumnType());
}

if (!$compatible) {
unset($allFieldTypes[$i]);
}
}

// Reset keys
$allFieldTypes = array_values($allFieldTypes);
} else {
// No column so no compatible field types
$allFieldTypes = [get_class($field)];
}

// Make sure the selected field class is in there
if (!in_array(get_class($field), $allFieldTypes, true)) {
Expand Down

0 comments on commit 8870c8f

Please sign in to comment.