Code Monkey home page Code Monkey logo

Comments (4)

velomovies avatar velomovies commented on June 30, 2024

Hi @rothnic, thanks for using the plugin and raising this issue. Luckily there is a way around your problem.

It's a difficult problem because the fields are inside a modular content block. This is not initialized by the plugin because you could potentially have more than one block and thus have for example more than one custom_shipping_text.

However if you console.log(shipping) (shipping is the name of the modular content block in your example) it should return an array [{ ...shipping items }]. So in your case you could do:

const [shippingItem] = shipping  // or do const shippingItem = shipping[0] which returns the same value
return `${shippingItem.custom_shipping_text} | ${shippingItem.shipping_price}`

This solution is only working correctly if you have only one block in the modular content block. (Which is true in your case)

But when you have more items in a modular content block it becomes a bit more difficult, because you have more than one item in the shipping array. You could use the datoCmsPlugin.fieldPath variable for that which in your case will be shipping.0.shipping_text. Then you have to do some string manipulation and after that return the correct values. You can use the helper function getFieldValue for this.

See here my example:

const fieldPath = datoCmsPlugin.fieldPath // Field path of current field ("shipping.0.shipping_text")
const indexOfDot = fieldPath.lastIndexOf('.') // Gets index of last dot (.)
const fieldPathBefore = fieldPath.slice(0, indexOfDot) // Gets fieldPath without the current field (so "shipping.0")

// Get value of item of the field you want to return 
const customShippingText = getFieldValue(datoCmsPlugin.formValues, `${fieldPathBefore}.custom_shipping_text`)
const shippingPrice = getFieldValue(datoCmsPlugin.formValues, `${fieldPathBefore}.shipping_price`)

return `${customShippingText} | ${shippingPrice}`

This is maybe not the most elegant, but unfortunately there is (at the moment) no easier way.

I hope this solves you issue. If you have any more questions feel free to open a new issue

from datocms-plugin-computed-fields.

rothnic avatar rothnic commented on June 30, 2024

@velomovies thanks a bunch for the reply. I was hoping there would be a workaround, but didn't quite connect these dots. This might be worth linking to from the readme.

from datocms-plugin-computed-fields.

rothnic avatar rothnic commented on June 30, 2024

@velomovies now that I played around with it a bit, there was one thing I'm not quite following. It seems like the solution you described above the template portion (const fieldPath = ..... const shippingPrice) is generalizable isn't it? In other words, could the plugin not automatically take this approach to make each field in a block available to the calculated field(s) in the block?

from datocms-plugin-computed-fields.

velomovies avatar velomovies commented on June 30, 2024

@rothnic Good point! Now I think about it, that should indeed be doable.

So we will do this when the field is in a block. We make a new variable i.e. thisBlock and then you can do thisBlock.[dataYouNeed]. (thisBlock.custom_shipping_text)

from datocms-plugin-computed-fields.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.