verbb / icon-picker

A Craft CMS field to select SVG or font icons from a folder for use in your content.
Other
17 stars 8 forks source link

FeedMe and IconPicker || array/object expected but string given #93

Closed DynamiteGoesBoom closed 2 months ago

DynamiteGoesBoom commented 5 months ago

Describe the bug

Hey Crawf,

I was hoping to pick your brain on this weird icon picker import issue. I asked Tommy at P&T about it already but he mentioned this may be more up your alley:

FeedMe is whining about the iconSelect not being an array or object, but isn’t the image below showing that?

Screenshot 2024-06-25 at 2 14 30 PM

My mapping looks like:

"iconSelect": {
  "field": "verbb\\iconpicker\\fields\\IconPickerField",
  "node": "contentBlocks\/cta\/iconSelect",
  "default": ""
},

Screenshot 2024-06-25 at 2 15 25 PM

I’m not sure what else it’s looking for, do you have any thoughts?

Thanks again 🎉

Steps to reproduce

  1. setup icon picker field
  2. import it in via another feed like this one

Craft CMS version

5.2.3

Plugin version

3.0

Multi-site?

No

Additional context

FeedMe is at version 6.1.0

engram-design commented 5 months ago

I've just realised that there's no actual documentation for the format required for Feed Me and icons, sorry about that!

Firstly, there's a bug with the integration, which is fixed for the next release. To get this early, run composer require verbb/icon-picker:"dev-craft-5 as 3.0.0".

Secondly, the value that you provide for the field will depend on what sort of icon your icon sets contain:

[
    {
        "entry": "Icon Picker Feed Me",
        "iconPickerFont": "dropbox:61803",
        "iconPickerSprite": "angle-right",
        "iconPickerSvg": "/brands/black-tie.svg",
        "iconPickerFa5Free": "fas fa-ad"
    }
]
DynamiteGoesBoom commented 5 months ago

Nice @engram-design! Thanks for this. I'll let you know if I run into any snags.

DynamiteGoesBoom commented 5 months ago

@engram-design I have this for my fieldMapping data for FeedMe:

"iconSelect": {
            "field": "verbb\\iconpicker\\fields\\IconPickerField",
            "node": "contentBlocks\/cta\/iconSelect\/value",
            "default": ""
          },

If you look at this feed under About > contentBlocks > cta you'll see the iconSelect and value, but when I run the import that icon never gets added.

Screenshot 2024-06-28 at 12 01 37 PM

Am I missing something?

engram-design commented 4 months ago

Note the value has to match exactly.

Font Awesome 5: far fa-thumbs-up link Font Awesome 6: fa-regular fa-thumbs-up link

DynamiteGoesBoom commented 4 months ago

It's too bad the values changed so much. There's no easy way to migrate that content over now :/

engram-design commented 4 months ago

You could use of the the Feed Me events to modify the data at each step, if that’s helpful? You could check against fa and switch to far or the corresponding family

DynamiteGoesBoom commented 4 months ago

@engram-design hmm never did that before...do you have any examples by chance?

engram-design commented 4 months ago

Sure, you basically modify the feed content before it's used in importing. So it might look something like:

use craft\feedme\events\FeedProcessEvent;
use craft\feedme\services\Process;
use yii\base\Event;

Event::on(Process::class, Process::EVENT_BEFORE_PROCESS_FEED, function(FeedProcessEvent $event) {
    if (is_array($event->feedData)) {
        foreach ($event->feedData as $dataKey => $data) {
            foreach ($data as $itemKey => $item) {
                if (is_string($item) && str_starts_with($item, 'fa ')) {
                    $event->feedData[$dataKey][$itemKey] = str_replace('fa ', 'far ', $item);
                }
            }
        }
    }
});

Which is going to look for every item in your provided feed data, for every string that starts with fa and replace that with far. Of course you can get as complicated with this as you like, and I'm sure there's going to be some edge-cases to deal with. Depending on how many icons there are in your feed, it might be easier to just replace fa fa-thumbs-up with far fa-thumbs-up specifically for example.

Hope that helps?

DynamiteGoesBoom commented 4 months ago

@engram-design thanks as always. The icons aren't used a whole lot for most Fido projects but I'll keep this in mind for one project in particular.

Thanks again as always 🎉

engram-design commented 2 months ago

Fixed in 3.0.1