datocms / plugins

Repository providing samples using the Plugins SDK
MIT License
33 stars 47 forks source link

ConditionalField plugin doesn't work with dato migration #58

Closed joanpadolina closed 1 year ago

joanpadolina commented 2 years ago

I'm trying to run a migration with your plugin, the addon is being installed perfectly but somehow it won't implement the plugin to the specific targetFieldsApiKey.

  await client.fields.create(pageLinkBlock.id, {
    label: 'Render as button',
    apiKey: 'ti_render_as_button',
    fieldType: 'boolean',
    defaultValue: false,
    appearance: {
      editor: 'boolean',
      parameters: {},
      addons: [
        {
          id: conditionalFieldsPlugin.id,
          parameters: {
            invert: false,
            targetFieldsApiKey: ['ti_button_style'],
          },
          fieldExtension: 'conditionalFields',
        },
      ],
    },
  })

I've also tried manually adding it to dato and the addons was exactly the same as the code above. Thanks in advanced! ConditionalFields

stefanoverna commented 2 years ago

Could it be that the API client automatically transforms targetFieldsApiKey into target_fields_api_key (which won't work)?

arakoczy commented 2 years ago

We've faced the same issue today and I can confirm that eventually, it works with both targetFieldsApiKey and target_fields_api_key.

The returned object I got back for hideNavigationField.appeareance.addons[0].parameters looks like this:

{ invert: false, targetFieldsApiKey: [ 'banner_text' ] }

What happens after is really weird, going back to the UI and either refreshing the page or going to check (manually) in the settings if the plugin setup worked, I see the ctx.notice message "Plugin upgraded successfully!" and the targetFieldsApiKey will be suddenly erased:

{ invert: false, targetFieldsApiKey: [] }

souljuse commented 1 year ago

Hi @arakoczy,

it works with both targetFieldsApiKey and target_fields_api_key.

This is not possible with our node client. The creation of the plugin works only with the camelized key targetFieldsApiKey, and all the others keys in snake case.

Try using this code:

await client.fields.create(pageLinkBlock.id, {
    label: "Render as button",
    api_key: "ti_render_as_button",
    field_type: "boolean",
    default_value: false,
    appearance: {
        editor: "boolean",
        parameters: {},
        addons: [
            {
                id: conditionalFieldsPlugin.id,
                parameters: {
                    invert: false,
                    targetFieldsApiKey: ["ti_button_style"],
                },
                field_extension: "conditionalFields",
            },
        ],
    },
});

Let me know :) I will close this issue for now, and re-open if not solved.

Faxxiz commented 1 year ago

Hello @souljuse,

Couldn't find any other answer where this problem is discussed/solved. I got myself into the same kind of issue, I am scripting this in 50+ projects so I can't update everything manually.

This is what I did to make things work, pay attention to the "targetFieldsApiKey" written in camelCase.

appearance: {
      editor: "boolean",
      parameters: {},
      addons: [
        {
          id: conditionalBooleanPlugin.id,
          parameters: {
            invert: false,
            targetFieldsApiKey: ["internal_link"],
          },
          fieldExtension: "conditionalFields",
        },
        {
          id: conditionalBooleanPlugin.id,
          parameters: {
            invert: true,
            targetFieldsApiKey: ["external_link"],
          },
          fieldExtension: "conditionalFields",
        },
      ],
    },

and the surprise is on DatoCMS directly where I can't see nothing, except in the request that there is the field "target_fields_api_key" written in snake case.

image

Obviously, I'd like to point out that it works through the interface the field is transformed into "targetFieldsApiKey" and the behaviour is perfect after that, And to avoid wasting anymore time too, I tried changing my script with what you said about having all other fields in snake case too, tried all fields in camelCase, and all possible combinations of formatting

stefanoverna commented 1 year ago

@Faxxiz we need more info to be able to reproduce.

Faxxiz commented 1 year ago

@Faxxiz we need more info to be able to reproduce.

  • Which version of the DatoCMS client are you using in your scripts?

v3.5.21, I think it's the latest one

stefanoverna commented 1 year ago

The latest version of @datocms/cma-client-node is 2.0.0, are you using the deprecated datocms-client client?

Faxxiz commented 1 year ago

Oh okay the legacy client is still written in all documentation pages, I wasn't aware we needed to stop using it.

stefanoverna commented 1 year ago

Where did you find those links exactly? We should correct them.. thanks!

Faxxiz commented 1 year ago

I took it from the people talking about the plugin here : https://community.datocms.com/t/conditional-fields-plugin-does-not-work-with-migrations/3570. If I'm not mistaking there is no documentation in the plugin so this is all I've got, and the request when I do it manually from the interface.

And as they were using in the first line await client.plugins.all();, I assumed it was done with the legacy client like everywhere in this documentation https://www.datocms.com/docs/content-management-api/resources/plugin/instances (especially here it's a .all() and not a .list() like in the @datocms/cma-client-node)

And as the legacy JS client is part of every page of the documentation, I didn't think it could fail.

stefanoverna commented 1 year ago

Updated the discussion on the forum, thanks! And the Docs offers the new client by default, but we'll add a deprecation notice for the legacy one!

https://github.com/datocms/plugins/assets/51573/5d759299-8d76-4019-aaa1-e5f948e56731