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

Table fields not remembering their Default Values row order #3947

Closed
olivierbon opened this issue Mar 5, 2019 · 10 comments

Comments

Projects
None yet
3 participants
@olivierbon
Copy link
Member

commented Mar 5, 2019

Description

Table fields don’t remember changes to the row order in their Default Values setting.

Steps to reproduce

  1. Create a Table field
  2. Add multiple rows to the Default Values setting
  3. Change the order of the Default Values rows
  4. Save the field

The rows will be saved in the order they were created in, rather than the custom-defined row order.

Additional info

  • Craft version: 3.1.15
  • PHP version: 7.2.1

@brandonkelly brandonkelly changed the title Table field ordering inside Matrix block not saving Table fields not remembering their Default Values row order Mar 5, 2019

@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

@brandonkelly Brandon, I'm still seeing this problem, on dev-develop -- just checked again this evening.

Nothing's changed in my source definition, and I don't see any settings to alter ordering from the physical order rows are placed on (in Craft CP - My Plugin Settings)

If I move a row in the usual way (ordering is critical for this plugin), and save, it flips back to its original position.

Old rows do stay in their original positions, so I noticed this first on an added row. But if I move old rows, they also are 'stuck' to their pre-move, and pre-fault, positions.

Should be simple, I think -- needed for a beta next week...thanks.

@brandonkelly

This comment has been minimized.

Copy link
Member

commented Mar 8, 2019

@narration-sd this bug was specifically for the Default Values setting on Table fields. Is that where you’re seeing it too? I verified that the fix worked. Maybe Composer‘s cache was out of date. Rather than being on dev-develop you should try setting the craftcms/cms requirement to 3.1.16 or ^3.1.16, then run composer update.

@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

Fair enough, and thanks for the quick attention. I'll run through your scenarios and come back...

@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

Ok, I've ditched /vendor and composer.lock, cleared composer caches, set Craft to ^3.1.16 (appropriate actually towards the delivery this is for), cleaned out non-essential plugins, and composer install'd.

Moving table rows in my plugin settings still reverts on save. Value changes seem to save fine.

Brandon, is there anything I could do that you would imagine to break ordering?

I did add some items to this plugin/settings recently, if none of them were in the table. I'm going to try reverting to some earlier version to see if re-ordering starts functioning again...

What happens ever when you promise something soonish, and I still have to write that code ;)

@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

Ok! It's Craft latest or so causing this.

  • I ran my own plugin back to a stone-age version where it's sure ordering has worked afterwards
    -- result, no change
  • I ran back Craft as far as a locked Yii update allowed, 3.1.13
    -- result: success! Manual ordering and saving settings from the table functions as expected

Maybe the fix you put in on this original issue made the breakage?

Happy to enter this as a separate issue if not/or if you prefer...and I will go back to using craftcms/cms:dev-master to validate your fix towards release...

noting @brandonkelly as never sure how seeing post-closed issue comments works for you

brandonkelly added a commit that referenced this issue Mar 8, 2019

ArrayHelper::ensureNonAssociative()
Implements a better fix for #3947
@brandonkelly

This comment has been minimized.

Copy link
Member

commented Mar 8, 2019

Moving table rows in my plugin settings still reverts on save. Value changes seem to save fine.

Once again this issue was specifically for the Default Values setting of Table fields (the custom field type, not to be confused with the _includes/forms/editableTable template that powers it). If you’re seeing it anywhere else then it’s unrelated, though the fix will likely be similar.

It happens because associative arrays are sorted by their keys when saved to the project config, in an effort to reduce merge conflicts / unexpected changes in project.yaml.

So when saving an array where the order matters, it should be numerically indexed, rather than associative. You can do that by adding this to your component/model’s init() method:

if (!empty($this->settingName)) {
    $this->settingName = array_values($this->settingName);
}

Where settingName is the property name holding the editable table row data.

@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

Ok, I see what you're getting at, but this isn't working, as the Settings model isn't filled in at its init() time, even though I've called parent::init() prior to invoking this code.

Should I be doing something in the plugin's own init() that I'm overlooking?

(smiling because apparently I got bitten by project config, even while waiting on it here)

@brandonkelly

@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

also reverted to 3.1.17 after noticing that, in case influenced though didn't think so. It doesn't.

feeling is I need to call something within SettingsModel::init() besides parent::init() to get the values filled in. Rather than probe, guess this time, asking.

As this is playing hob with needed gross refactoring of a complex React part ;)

Thanks again. Saved by Mexican music, very accomplished, with wit. Like one?
@brandonkelly

@brandonkelly

This comment has been minimized.

Copy link
Member

commented Mar 8, 2019

For plugin settings you could do it from setAttributes().

public function setAttributes($values, $safeOnly = true)
{
    parent::setAttributes($values, $safeOnly);
    if (!empty($this->settingName)) {
        $this->settingName = array_values($this->settingName);
    }
}
@narration-sd

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

...makes sense, and does the trick. Thanks very kindly, Brandon - the day is saved. Rest of it anyway, but you know what I mean with an exposure coming up.

Here's the lady, considerably intelligent, creative, thoughtful, in case you like it. For the wit, besides the song delivery, just watch the video, right up to the last moment :)

Well, two. The second one is kind of a Roma treatment...if the listening is the thing.

You might guess I like her. Not least for the great variety of music across the stages of her building. From the German, a true bildungsroman.

Cheers, Brandon, and on your good deed for the day.
Clive

@brandonkelly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.