Skip to content

Commit

Permalink
"Save and add another" action for Edit Field pages
Browse files Browse the repository at this point in the history
Resolves #13865
Resolves #9614
  • Loading branch information
brandonkelly committed Dec 29, 2023
1 parent a615785 commit f11b1ec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Improved the performance of large editable tables. ([#13852](https://github.com/craftcms/cms/issues/13852))
- Added partial support for field types storing data in JSON columns (excluding MariaDB). ([#13916](https://github.com/craftcms/cms/issues/13916))
- “Updating search indexes” jobs are no longer queued when saving elements with change tracking enabled, if no searchable fields or attributes were changed. ([#13917](https://github.com/craftcms/cms/issues/13917))
- Edit Field pages now have a “Save and add another” action. ([#13865](https://github.com/craftcms/cms/discussions/13865))
- Added the `disabledUtilities` config setting. ([#14044](https://github.com/craftcms/cms/discussions/14044))
- `resave` commands now pass an empty string (`''`) to fields’ `normalizeValue()` methods when `--to` is set to `:empty:`. ([#13951](https://github.com/craftcms/cms/issues/13951))
- The `index-assets/one` and `index-assets/all` commands now accept a `--delete-empty-folders` option. ([#13947](https://github.com/craftcms/cms/discussions/13947))
Expand Down
19 changes: 16 additions & 3 deletions src/controllers/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,17 @@ public function actionDeleteGroup(): Response
* @param int|null $fieldId The field’s ID, if editing an existing field
* @param FieldInterface|null $field The field being edited, if there were any validation errors
* @param int|null $groupId The default group ID that the field should be saved in
* @param string|null $type The field type to use by default
* @return Response
* @throws NotFoundHttpException if the requested field/field group cannot be found
* @throws ServerErrorHttpException if no field groups exist
*/
public function actionEditField(?int $fieldId = null, ?FieldInterface $field = null, ?int $groupId = null): Response
{
public function actionEditField(
?int $fieldId = null,
?FieldInterface $field = null,
?int $groupId = null,
?string $type = null,
): Response {
$this->requireAdmin();

$fieldsService = Craft::$app->getFields();
Expand All @@ -132,7 +137,7 @@ public function actionEditField(?int $fieldId = null, ?FieldInterface $field = n
}

if ($field === null) {
$field = $fieldsService->createField(PlainText::class);
$field = $fieldsService->createField($type ?? PlainText::class);
}

// Supported translation methods
Expand Down Expand Up @@ -354,6 +359,14 @@ public function actionSaveField(): ?Response
}

$this->setSuccessFlash(Craft::t('app', 'Field saved.'));

if ($this->request->getParam('addAnother')) {
return $this->redirect(UrlHelper::cpUrl('settings/fields/new', [
'groupId' => $field->groupId,
'type' => $field::class,
]));
}

return $this->redirectToPostedUrl($field);
}

Expand Down
8 changes: 7 additions & 1 deletion src/templates/settings/fields/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
shortcut: true,
retainScroll: true,
},
{
label: 'Save and add another'|t('app'),
shortcut: true,
shift: true,
params: {addAnother: 1},
},
fieldId ?? false ? {
label: 'Delete'|t('app'),
action: 'fields/delete-field',
Expand Down Expand Up @@ -147,7 +153,7 @@
{% endblock %}


{% if field is empty or field.handle is empty %}
{% if not (field.handle ?? false) %}
{% js %}
new Craft.HandleGenerator('#name', '#handle');
{% endjs %}
Expand Down

0 comments on commit f11b1ec

Please sign in to comment.