Comments (4)
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.
@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.
@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.
@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)
- Not published correctly? HOT 3
- Adding computed fields for existing content HOT 1
- Missing environment ID causes issues in non-primary environments HOT 1
- Computing a link field HOT 1
- Allow getFieldValue to return object HOT 2
- Properly hide or disable the field HOT 4
- Where does console.log show up? HOT 1
- How to access values within multi-link field? HOT 3
- Script not firing on field value change. HOT 3
- Computed field is not recalculated when modifying fields of sibling (block) HOT 2
- Usage on an Single Asset field causes Display Editor to become locked; unable to switch back to default HOT 4
- Problem with supporting multiple locales.
- Not returning anything HOT 5
- How to access Local based content? HOT 2
- RTL behaviour of the field
- Get ID of current record HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datocms-plugin-computed-fields.