Skip to content

Commit

Permalink
Begin rendering fields using layout components
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosnar committed Jun 21, 2022
1 parent c684987 commit 5e65d3b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -214,6 +216,7 @@ Craft.BulkEditModal = Garnish.Modal.extend({
* @returns {boolean}
*/
selfDestruct: function() {
debugger;
var instance = Craft.BulkEditModal.openInstance;

instance.hide();
Expand All @@ -230,6 +233,7 @@ Craft.BulkEditModal = Garnish.Modal.extend({
* @private
*/
_onHide: function() {
debugger;
Craft.BulkEditModal.openInstance = null;
this.$shade.remove();
this._unbindEventHandlersForFieldSelect();
Expand Down
54 changes: 36 additions & 18 deletions src/controllers/BulkEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand All @@ -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');

Expand All @@ -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));
}
}

Expand Down
13 changes: 3 additions & 10 deletions src/templates/elementactions/BulkEdit/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

<div class="bulkedit field-edit-modal">
<form id="bulk-edit-values-modal">
{% for field in fields %}
{% if fieldHtml %}
<div class="pane">
{# {% namespace 'fields' %}#}
{% include "_includes/field" with {
field: field,
required: false,
element: elementPlaceholder,
siteId: site.id
} only %}
{# {% endnamespace %}#}
{{ fieldHtml|raw }}
</div>
<div class="field-edit-modal-bar">
<div class="buttons right">
Expand All @@ -25,6 +18,6 @@
<div class="btn" id="field-edit-cancel">{{"Cancel"|t('venveo-bulk-edit')}}</div>
</div>
</div>
{% endfor %}
{% endif %}
</form>
</div>

0 comments on commit 5e65d3b

Please sign in to comment.