Problem when adding a plugin from migration scripts

I am trying to install a addon to a field with migration scripts and content management api. I am not sure if the problem comes from the plugin itself or that i am doing something wrong.

I am using this plugin: Conditional Disabled Fields - Plugins โ€” DatoCMS

The problem is with the targetFieldsApiKey. It does not seem to apply correctly as it doesnโ€™t work as intended and in the admin panel its not set. It does however go to the right field.

Here is the code in question.

module.exports = async (client) => {
  const plugin = await client.plugins.create({
    packageName: 'datocms-plugin-conditional-disabled-field',
  });

  const apiKey = 'wide_cta::headline';
  const headline = await client.field.find(apiKey);
  const newAppearance = headline.appearance;

  newAppearance.addons.push({
    id: plugin.id,
    parameters: {
      targetFieldsApiKey: ['headline_image'],
    },
    field_extension: plugin.name,
  });

  await client.field.update(apiKey, {
    appearance: newAppearance,
  });
};

Also if i set up it manually and check what the object should look like it returns this. I think this i identical to the setup im doing in the migration script.

     appearance: {
      addons: [
        {
          id: '108777',
          parameters: {
            targetFieldsApiKey: ['headline_image'],
          },
          field_extension: 'conditionalDisabledFields',
        },
      ],
    },
2 Likes

Hello @tomas.valkendorff

The problem seems to be that you are using the Legacy plugin syntax, while using a plugin that uses the new SDK: GitHub - thebuilder/datocms-plugin-conditional-disabled-field

You can read the documentation on the syntax for field addons on the new SDK here: Plugin SDK - Field extensions - DatoCMS Docs and migrate your syntax to be compatible with the new SDK

It no longer supports client.plugins.create and it is based around hooks and custom components

2 Likes

I tried following the Docs link you send but i couldnโ€™t get the migrations work with the datocms-plugin-sdk since i got a reference error regarding โ€œwindowโ€ in the connect function.

After some trial and error i got my script working with almost the same syntax with the new TypeScript CLI.

import { Client } from '@datocms/cma-client';

export default async function pluginToWideCta(client: Client) {
  const plugin = await client.plugins.create({
    package_name: 'datocms-plugin-conditional-disabled-field',
  });

  const apiKey = 'wide_cta::headline';
  const headline = await client.fields.find(apiKey);
  const newAppearance = headline.appearance;

  newAppearance.addons.push({
    id: plugin.id,
    parameters: {
      targetFieldsApiKey: ['headline_image'],
    },
    field_extension: plugin.name,
  });

  await client.fields.update(apiKey, {
    appearance: newAppearance,
  });
}
1 Like