Skip to content

Commit

Permalink
Properly namespace forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosnar committed Jun 21, 2022
1 parent 5e65d3b commit 236163d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Craft.BulkEditModal = Garnish.Modal.extend({
type: null,
requestId: 0,

namespace: null,

/**
* Initialize the preview file modal.
* @returns {*|void}
Expand Down Expand Up @@ -76,6 +78,7 @@ Craft.BulkEditModal = Garnish.Modal.extend({
.then((response) => {
const data = response.data
this.$container.append(data.modalHtml);
this.namespace = data.namespace
Craft.initUiElements(this.$container);
this._bindEventHandlersForFieldSelect();
})
Expand All @@ -93,7 +96,8 @@ Craft.BulkEditModal = Garnish.Modal.extend({
Craft.sendActionRequest('POST', 'venveo-bulk-edit/bulk-edit/get-edit-screen', {
data: {
...this.viewParams,
fieldConfig: fieldConfig
fieldConfig: fieldConfig,
namespace: this.namespace
}
})
.then((response) => {
Expand Down Expand Up @@ -164,7 +168,8 @@ Craft.BulkEditModal = Garnish.Modal.extend({
data: {
...this.viewParams,
fieldConfig: this.fieldConfig,
formValues: (new URLSearchParams(formValues)).toString()
formValues: (new URLSearchParams(formValues)).toString(),
namespace: this.namespace
}
}).then(() => {
Craft.cp.trackJobProgress(false, true);
Expand Down
29 changes: 13 additions & 16 deletions src/controllers/BulkEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use craft\errors\SiteNotFoundException;
use craft\fieldlayoutelements\CustomField;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\models\Site;
Expand Down Expand Up @@ -76,13 +77,15 @@ public function actionGetFields(): Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();
$namespace = StringHelper::randomString(10);

$service = Plugin::getInstance()->bulkEdit;
$customFields = $service->getFieldWrappersForElementQuery($this->getElementQuery());
// $attributes = $service->getAttributeWrappersForElementQuery($this->getElementQuery());
$attributes = [];

$view = Craft::$app->getView();
$view->setNamespace($namespace);
$modalHtml = $view->renderTemplate('venveo-bulk-edit/elementactions/BulkEdit/_fields', [
'fieldWrappers' => $customFields,
'attributeWrappers' => [],
Expand All @@ -97,6 +100,7 @@ public function actionGetFields(): Response
'success' => true,
'modalHtml' => $modalHtml,
'siteId' => $this->site->id,
'namespace' => $namespace,
];
$responseData['headHtml'] = $view->getHeadHtml();
$responseData['footHtml'] = $view->getBodyHtml();
Expand All @@ -115,7 +119,8 @@ public function actionGetEditScreen(): Response
$this->requirePostRequest();
$this->requireAcceptsJson();

$fields = Craft::$app->getRequest()->getRequiredParam('fieldConfig');
$fields = $this->request->getRequiredParam('fieldConfig');
$namespace = $this->request->getRequiredParam('namespace');
$enabledFields = array_filter($fields, fn($field) => $field['enabled']);
$fields = Field::findAll(array_keys($enabledFields));
$fieldModels = [];
Expand Down Expand Up @@ -150,20 +155,11 @@ public function actionGetEditScreen(): Response
}
$fieldLayoutTab->setElements($fieldLayoutElements);
$fieldLayout->setTabs([$fieldLayoutTab]);
$fieldLayoutForm = $fieldLayout->createForm($elementPlaceholder, false, ['namespace' => 'asdf']);
$fieldLayoutForm = $fieldLayout->createForm($elementPlaceholder, false, [
'namespace' => $namespace
]);
$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', [
'totalElements' => $this->elementQuery()->count(),
'fieldHtml' => $html
Expand All @@ -187,13 +183,14 @@ public function actionSaveContext(): \yii\web\Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();
$namespace = $this->request->getRequiredParam('namespace');
// Converts the url encoded form values from the json payload to the expected format.
$namespacedValues = [];

parse_str(Craft::$app->getRequest()->getRequiredParam('formValues'), $namespacedValues);
$fieldValues = $namespacedValues['fields'];
parse_str($this->request->getRequiredParam('formValues'), $namespacedValues);
$fieldValues = $namespacedValues[$namespace]['fields'];

$fieldConfigData = Craft::$app->getRequest()->getRequiredParam('fieldConfig');
$fieldConfigData = $this->request->getRequiredParam('fieldConfig');

$fieldConfigs = [];
foreach ($fieldConfigData as $fieldConfigDatum) {
Expand Down
40 changes: 21 additions & 19 deletions src/templates/elementactions/BulkEdit/_edit.twig
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{% import '_includes/forms' as forms %}

<div class="bulkedit field-edit-modal">
<form id="bulk-edit-values-modal">
{% if fieldHtml %}
<div class="pane">
{{ fieldHtml|raw }}
</div>
<div class="field-edit-modal-bar">
<div class="buttons right">
<div class="btn" id="field-edit-cancel">{{"Cancel"|t('venveo-bulk-edit')}}</div>
<input type="button" value="Save {{ totalElements }} Elements" class="btn submit"></div>
</div>
{% else %}
<p>{{"It doesn't look like there are any fields that support bulk editing on these elements."|t('venveo-bulk-edit')}}</p>
<div class="field-edit-modal-bar">
<div class="buttons right">
<div class="btn" id="field-edit-cancel">{{"Cancel"|t('venveo-bulk-edit')}}</div>
</div>
</div>
{% endif %}
</form>
<form id="bulk-edit-values-modal">
{% if fieldHtml %}
{% namespace namespace ?? null %}
<div class="pane">
{{ fieldHtml|raw }}
</div>
{% endnamespace %}
<div class="field-edit-modal-bar">
<div class="buttons right">
<div class="btn" id="field-edit-cancel">{{ "Cancel"|t('venveo-bulk-edit') }}</div>
<input type="button" value="Save {{ totalElements }} Elements" class="btn submit"></div>
</div>
{% else %}
<p>{{ "It doesn't look like there are any fields that support bulk editing on these elements."|t('venveo-bulk-edit') }}</p>
<div class="field-edit-modal-bar">
<div class="buttons right">
<div class="btn" id="field-edit-cancel">{{ "Cancel"|t('venveo-bulk-edit') }}</div>
</div>
</div>
{% endif %}
</form>
</div>

0 comments on commit 236163d

Please sign in to comment.