Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Store View" field value empty when reloading form after failed save. #415

Merged
merged 1 commit into from
Jan 17, 2018

Conversation

colinmollenhour
Copy link
Member

The form uses 'store_id' as the element id but 'stores[]' as the input name since the model after save uses getStores for the new store ids. Without this change the store_id is empty and so the Store View field values get wiped when the form is reloaded.

@sreichel
Copy link
Contributor

Fix works.

Are there any downsides with fixing the field name? Why is it store_id when deleting CMS page and stores when editing? Should it be consistent?

@@ -102,6 +102,7 @@ public function editAction()
// 3. Set entered data if was error when we do save
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (! empty($data)) {
$data['store_id'] = $data['stores'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the form is using stores[] as the element name then it's meant to be an array. Here you seem to just be grabbing the array (or possible array) and assigning it to a field that's meant to be a scalar, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, store_id i not meant to be a scalar. Think these lines from #382 make it more clear ...

# is delete or edit action?
$isDeleteAction = $observer->getEvent()->getName() === 'cms_page_delete_before';
$storeIds = $isDeleteAction ? $page->getStoreId() : $page->getStores();

Copy link
Contributor

@LeeSaferite LeeSaferite Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree.

https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Cms/Model/Resource/Page.php#L122

The underlying model resource clearly expects that store_id is a scalar value that is used as a fallback when stores is not present. The code example you listed is faulty.

Edit: Faulty in the fact that if someone sets store_id to a scalar then it'll likely cause issues since the code shown expects that it's an array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I'll follow up with this example that is contradictory to what I just said but which I consider a bug.

https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Cms/Model/Resource/Page.php#L184

store_id is clearly singular and in every other context on other models is a scalar. They clearly expect it to be a scalar on save since they cast it to an array. The fact that after loading they assign the array of store IDs to store_id vs. stores is, IMHO, 100% a bug in the core code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further evidence that the core code is buggy regarding the treatment of store_id: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Cms/Model/Resource/Page.php#L203-L204

Here the object loading code clearly expects store_id to be a scalar, not an array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, 100% a bug in the core code.

Thats what i've meant. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input name is stores because that is the data field that is expected when saving a page. The fact that the core afterLoad code is dumping the store IDs into store_id instead of stores is clearly a bug. There is no DB field for store_id of a page (directly). There is an additional link table that links pages to stores so a page has an array of store ids always.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it posible to add an alias to cms_page_store.store_id as stores?

(But then you need a fallback to store_id, at least for 3rd party code.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say adding stores in the after load is a good first step in the overall issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding $object->setData('stores', $stores); to _afterLoad solves only my problem (different getters for store ids), but it doesnt solve the problem @colinmollenhour describes.

To reproduce ...

  • add an observer to cms_page_save_before-
  • just throw an exeption to display an error message

Selected stores are still empty.

@LeeSaferite
Copy link
Contributor

@sreichel They tend to use stores when the value is a multi-select for multiple store selections.

@sreichel
Copy link
Contributor

sreichel commented Jan 11, 2018

Think this fix is good as it is. Fixing stores vs. store_id can be done later.

@sreichel sreichel added the bug label Jan 11, 2018
@colinmollenhour colinmollenhour merged commit 72a4503 into 1.9.3.x Jan 17, 2018
@sreichel sreichel deleted the fix-cms-form-reload branch January 17, 2018 19:11
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Jan 22, 2018
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Feb 28, 2018
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Mar 20, 2018
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Jul 17, 2018
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Sep 19, 2018
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Feb 14, 2019
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Apr 1, 2019
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Aug 22, 2019
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Oct 25, 2019
edannenberg pushed a commit to edannenberg/magento-lts that referenced this pull request Aug 20, 2020
@addison74
Copy link
Contributor

Think this fix is good as it is. Fixing stores vs. store_id can be done later.

... and what happened to this post? Has that problem been solved or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants