Skip to main content
Solved

How to pass a collection node to set a variable mode?

  • February 20, 2024
  • 4 replies
  • 679 views

Martyn_Bowis

Documentation says:

setExplicitVariableModeForCollection(collectionId: string, modeId: string): void

How to resolve following error?

“Calling setExplicitVariableModeForCollection with a collection id is deprecated. Please pass the collection node instead.”

How do you pass a collection node?

Best answer by tank666

Let’s start in order:

figma.com

The argument to this method must be collection.id, not collection.key.

So, you need to follow the following logic:

  1. Get a LibraryVariableCollection;
  2. Get a LibraryVariable;
  3. Import this LibraryVariable by key;
  4. Get a variableCollectionId in imported Variable;
  5. Get a VariableCollection by id.

After completing the fifth step, you will have a VariableCollection object, which you must use in the setExplicitVariableModeForCollection method.

Collection modes will be available in the following path: VariableCollection.modes.

View original
This topic has been closed for replies.

4 replies

tank666
  • 4873 replies
  • February 20, 2024

Instead of the collectionId string, pass a VariableCollection object.

figma.com

Martyn_Bowis
  • Author
  • Active Member
  • 29 replies
  • February 20, 2024

I cannot get getVariableCollectionById to work for a team library collection.

I have tried to get the variable collection, for a team library, using the collectionId as follows:

  // get available library collections
  var collections = await figma.teamLibrary.getAvailableLibraryVariableCollectionsAsync(); 
  console.log(collections);

  // get first library collection
  var collection = collections[0];
  console.log(collection);

  // get collection id
  var collectionId = collection.key;
  console.log(collectionId);

  // get same collection using collectionId
  var collectionById = figma.variables.getVariableCollectionById(collectionId);
  console.log(collectionById);

Why does the console output show that getting the collection using the collection id returns null? Note that the collection was returned successfully, the collectionId obtained successfully via the collection key, but null is returned when trying to use that collectionId to get the variable collection by id with Figma.variables.getVariableCollectionById(collectionId).

So, how to do the following?

  • Get team library variable collection using Figma.variables.getVariableCollectionById or other means
  • Get modes of that team library variable collection
  • use setExplicitVariableModeForCollection


tank666
  • 4873 replies
  • Answer
  • February 21, 2024

Let’s start in order:

figma.com

The argument to this method must be collection.id, not collection.key.

So, you need to follow the following logic:

  1. Get a LibraryVariableCollection;
  2. Get a LibraryVariable;
  3. Import this LibraryVariable by key;
  4. Get a variableCollectionId in imported Variable;
  5. Get a VariableCollection by id.

After completing the fifth step, you will have a VariableCollection object, which you must use in the setExplicitVariableModeForCollection method.

Collection modes will be available in the following path: VariableCollection.modes.


Ienaka_Atsushi

This clarification for the logic is extremely helpful.
Just wanted to say thank you! 🙂


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings