From 5e65d3b1c5b0f860ed46bf29ff409cf23bb0ae2c Mon Sep 17 00:00:00 2001 From: Ransom Roberson Date: Mon, 20 Jun 2022 23:54:09 -0400 Subject: [PATCH] Begin rendering fields using layout components --- .../dist/js/BulkEditModal.js | 8 ++- src/controllers/BulkEditController.php | 54 ++++++++++++------- .../elementactions/BulkEdit/_edit.twig | 13 ++--- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/assetbundles/bulkeditelementaction/dist/js/BulkEditModal.js b/src/assetbundles/bulkeditelementaction/dist/js/BulkEditModal.js index fd459e4..7e23edf 100644 --- a/src/assetbundles/bulkeditelementaction/dist/js/BulkEditModal.js +++ b/src/assetbundles/bulkeditelementaction/dist/js/BulkEditModal.js @@ -99,9 +99,9 @@ Craft.BulkEditModal = Garnish.Modal.extend({ .then((response) => { const data = response.data this.$container.append(data.modalHtml); - Craft.initUiElements(this.$container); Craft.appendHeadHtml(data.headHtml); - Craft.appendFootHtml(data.footHtml); + Craft.appendBodyHtml(data.footHtml); + Craft.initUiElements(this.$container); this._unbindEventHandlersForFieldSelect(); this._bindEventHandlersForFieldEditor(); @@ -133,6 +133,8 @@ Craft.BulkEditModal = Garnish.Modal.extend({ _unbindEventHandlersForFieldSelect: function() { this.$container.find('#field-edit-cancel').off('click', this.hide.bind(this)); this.$container.find('#fields-table .lightswitch').off('change', this._handleFieldSelect.bind(this)); + this.$container.find('#select-fields-form').off('submit', this._handleFieldSelectSubmit.bind(this)); + this.$container.find('#bulk-edit-select-all').off('change', this._handleFieldSelectToggleAll.bind(this)) }, _handleFieldSelectToggleAll: function(e) { @@ -214,6 +216,7 @@ Craft.BulkEditModal = Garnish.Modal.extend({ * @returns {boolean} */ selfDestruct: function() { + debugger; var instance = Craft.BulkEditModal.openInstance; instance.hide(); @@ -230,6 +233,7 @@ Craft.BulkEditModal = Garnish.Modal.extend({ * @private */ _onHide: function() { + debugger; Craft.BulkEditModal.openInstance = null; this.$shade.remove(); this._unbindEventHandlersForFieldSelect(); diff --git a/src/controllers/BulkEditController.php b/src/controllers/BulkEditController.php index db5245d..7f957a0 100644 --- a/src/controllers/BulkEditController.php +++ b/src/controllers/BulkEditController.php @@ -13,7 +13,10 @@ use Craft; use craft\controllers\ElementIndexesController; use craft\errors\SiteNotFoundException; +use craft\fieldlayoutelements\CustomField; use craft\helpers\Json; +use craft\models\FieldLayout; +use craft\models\FieldLayoutTab; use craft\models\Site; use craft\records\Field; use craft\web\Response; @@ -112,7 +115,6 @@ public function actionGetEditScreen(): Response $this->requirePostRequest(); $this->requireAcceptsJson(); - $service = Plugin::getInstance()->bulkEdit; $fields = Craft::$app->getRequest()->getRequiredParam('fieldConfig'); $enabledFields = array_filter($fields, fn($field) => $field['enabled']); $fields = Field::findAll(array_keys($enabledFields)); @@ -134,23 +136,37 @@ public function actionGetEditScreen(): Response 'siteId' => $this->site->id, ]); - // We've gotta register any asset bundles - this won't actually be rendered + $fieldLayoutElements = []; + $fieldLayout = new FieldLayout(); + $fieldLayoutTab = new FieldLayoutTab(); + $fieldLayoutTab->setLayout($fieldLayout); + $fieldLayoutTab->name = 'Content'; + $fieldLayoutTab->uid = 'content'; + foreach ($fieldModels as $fieldModel) { - $view->renderPageTemplate('_includes/field', [ - 'field' => $fieldModel, - 'static' => true, - 'element' => $elementPlaceholder, - 'required' => false, - ]); + $fieldLayoutElement = new CustomField(); + $fieldLayoutElement->setField($fieldModel); + $fieldLayoutElements[] = $fieldLayoutElement; } + $fieldLayoutTab->setElements($fieldLayoutElements); + $fieldLayout->setTabs([$fieldLayoutTab]); + $fieldLayoutForm = $fieldLayout->createForm($elementPlaceholder, false, ['namespace' => 'asdf']); + $html = $fieldLayoutForm->render(); + + + // We've gotta register any asset bundles - this won't actually be rendered +// foreach ($fieldModels as $fieldModel) { +// $view->renderPageTemplate('_includes/field', [ +// 'field' => $fieldModel, +// 'static' => true, +// 'element' => $elementPlaceholder, +// 'required' => false, +// ]); +// } $modalHtml = $view->renderTemplate('venveo-bulk-edit/elementactions/BulkEdit/_edit', [ - 'fields' => $fieldModels, - 'elementType' => $this->elementType, - 'elementPlaceholder' => $elementPlaceholder, 'totalElements' => $this->elementQuery()->count(), - 'fieldData' => $enabledFields, - 'site' => $this->site, + 'fieldHtml' => $html ]); $responseData = [ 'success' => true, @@ -172,8 +188,10 @@ public function actionSaveContext(): \yii\web\Response $this->requirePostRequest(); $this->requireAcceptsJson(); // Converts the url encoded form values from the json payload to the expected format. - $values = []; - parse_str(Craft::$app->getRequest()->getRequiredParam('formValues'), $values); + $namespacedValues = []; + + parse_str(Craft::$app->getRequest()->getRequiredParam('formValues'), $namespacedValues); + $fieldValues = $namespacedValues['fields']; $fieldConfigData = Craft::$app->getRequest()->getRequiredParam('fieldConfig'); @@ -188,12 +206,12 @@ public function actionSaveContext(): \yii\web\Response if ($fieldConfig->type === FieldType::CustomField) { $fieldConfig->fieldId = (int)$fieldConfigDatum['id']; $fieldConfig->handle = Craft::$app->fields->getFieldById($fieldConfig->fieldId)->handle; - $fieldConfig->serializedValue = Json::encode($values[$fieldConfig->handle]); + $fieldConfig->serializedValue = Json::encode($fieldValues[$fieldConfig->handle]); } - if($fieldConfig->validate()) { + if ($fieldConfig->validate()) { $fieldConfigs[] = $fieldConfig; } else { - throw new \Exception('Failed to validate field configuration: '. Json::encode($fieldConfig)); + throw new \Exception('Failed to validate field configuration: ' . Json::encode($fieldConfig)); } } diff --git a/src/templates/elementactions/BulkEdit/_edit.twig b/src/templates/elementactions/BulkEdit/_edit.twig index cae3db3..c307986 100644 --- a/src/templates/elementactions/BulkEdit/_edit.twig +++ b/src/templates/elementactions/BulkEdit/_edit.twig @@ -2,16 +2,9 @@
- {% for field in fields %} + {% if fieldHtml %}
-{# {% namespace 'fields' %}#} - {% include "_includes/field" with { - field: field, - required: false, - element: elementPlaceholder, - siteId: site.id - } only %} -{# {% endnamespace %}#} + {{ fieldHtml|raw }}
@@ -25,6 +18,6 @@
{{"Cancel"|t('venveo-bulk-edit')}}
- {% endfor %} + {% endif %}