How to generate slug for component element?

Hi,
I am new to squidex. I need support for slug. I have main schema called Instrument with normal slug field which generates it’s slug from its title field.

In that schema, I have a component called Image and Caption which contains images and captions. I want to give slug name for images using captions and I named it prettyUrl.

This is my code which I wrote in the Scripts under component (not in Instrument schema)

var data = ctx.data;
data.prettyUrl = { iv: slugify(data.heading.iv) };
replace(data);

This is not working as expected. Also I have doubt about localizable schema component. Is that ok to use iv in the code above for even localizable field?

As I see, the Image and Caption component is not localizable. So I think you can use iv in this case. Can you try this?

var data = ctx.data;
data.prettyUrl.iv = slugify(data.heading.iv);
replace(data);

If it’s localizable, you will need to define a specific language.
For example, you would like to create a slug based on the heading field (this field is localizable), the script will look like this:

        var data = ctx.data;
        if (data.heading) {
            if(data.heading.en){
                data.prettyUrl.iv = slugify(data.heading.en);
            }
            replace(data);
        }
1 Like

Thanks. This is clear and good. What if I am using component array?
if heading component happens multiple times in the content. It has to generate multiple slug for each name.

Also is there any option for console logging in the schema to understand more clearer?

I’m doubting that the script might not work with the component type field. I think you will need to wait for @Sebastian to make it clear.

1 Like

Oh… Then fine. I can wait.

Hi,
@haihninit is right, scripts do you now work on components (I should hide them). You have to solve in on the root level schema.