marknotton / craft-plugin-colours

Query colours or colours in images to do all manner of useful things
MIT License
0 stars 0 forks source link

Fieldtype overwrites all hidden values when multiple fields are used on one matrix #1

Open marknotton opened 7 years ago

jonXmack commented 7 years ago

I'm also having this issue. When using the fieldtype in a matrix upon saving the fieldtype saves all values for the value in the last row. IE I have three rows and pick #f00 for the first, #f0f for the second and #ff0 for the first, when viewing the entry all rows have #ff0 selected.

marknotton commented 7 years ago

Thanks Jon, I think I have a fix for this but haven't tested it thoroughly enough to commit it yet. I'll take a closer look in the next day or so and see about getting this resolved.

jonXmack commented 7 years ago

No problem, I was able to fix it on my local copy by wrapping the js in input.twig in an each function and then using $(this).find on the $colourTheme and $colourThemeItem selectors.

$('.field[id*={{name}}]').each(function(){
    var $colourTheme = $(this).find("input[name='fields[{{name}}]'], input[name*='[fields][{{name}}]']");
    var $colourThemeItem = $(this).find("ul[id*=fields-theme-colours].{{name}} li");

    // Add 'selected' to a colour that has been previously saved
    if ($colourTheme.val() != null) {
        $colourThemeItem.each(function() {
            if ($colourTheme.val() == $(this).find('button').data('colour')) {
                $(this).addClass('selected');
            }
        });
    }

    // Use NTC to define a name to each hex colour.
    $colourThemeItem.find(".hex").each(function(i) {
    $(this).next().text((ntc.name($(this).text())[1]));
    });

    $colourThemeItem.find("button").click(function(event) {
        event.preventDefault();
        $(this).parent().siblings().removeClass('selected');
        $(this).parent().addClass('selected');
        $colourTheme.val($(this).data('colour'));
    });
});
marknotton commented 7 years ago

Thats a far more elegant solution than I was going for. I was attempting all-manor of convoluted methods to target the correct id's. Thank you.

I'll test and incorporate this in the coming days. This plugin has had some significant additions since my last commit, so I'll need to run through and document everything first.

You are the first person in the "real world" I've known to use one of my plugins. Pressure is on. Thanks.