Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-LD RecipeIngredient does not support lists #35

Closed
mgburns opened this issue Mar 20, 2018 · 6 comments
Closed

JSON-LD RecipeIngredient does not support lists #35

mgburns opened this issue Mar 20, 2018 · 6 comments
Labels
need info Need more information on the issue

Comments

@mgburns
Copy link

mgburns commented Mar 20, 2018

I'm attempting to add JSON-LD meta for recipes and am unable to recreate the structure listed in the schema.org example:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Recipe",
  "author": "John Smith",
  "cookTime": "PT1H",
  "datePublished": "2009-05-08",
  "description": "This classic banana bread recipe comes from my mom -- the walnuts add a nice texture and flavor to the banana bread.",
  "image": "bananabread.jpg",
  "recipeIngredient": [
    "3 or 4 ripe bananas, smashed",
    "1 egg",
    "3/4 cup of sugar"
  ],
  "interactionStatistic": {
    "@type": "InteractionCounter",
    "interactionType": "http://schema.org/Comment",
    "userInteractionCount": "140"
  },
  "name": "Mom's World Famous Banana Bread",
  "nutrition": {
    "@type": "NutritionInformation",
    "calories": "240 calories",
    "fatContent": "9 grams fat"
  },
  "prepTime": "PT15M",
  "recipeInstructions": "Preheat the oven to 350 degrees. Mix in the ingredients in a bowl. Add the flour last. Pour the mixture into a loaf pan and bake for one hour.",
  "recipeYield": "1 loaf",
  "suitableForDiet": "http://schema.org/LowFatDiet"
}
</script>

Here's my code:

{% do seomatic.jsonLd.create({
    'type': 'Recipe',
    'name': entry.title,
    'url': entry.url,
    'description': entry.body,
    'recipeIngredient': ingredients|filterByValue('type', 'ingredient')|column('text'),
    'recipeInstructions': directions|filterByValue('type', 'direction')|column('text')|join(' ')|striptags
}) %}

The instructions render as expected, the ingredients are omitted, presumedly because it's an array and not a string.

@khalwat khalwat added the bug label Mar 21, 2018
@khalwat
Copy link
Collaborator

khalwat commented Mar 21, 2018

What is the filterByValue filter in this case? Also column?

@khalwat
Copy link
Collaborator

khalwat commented Mar 21, 2018

So as a quick test, I did this:

{% set myRecipe = seomatic.jsonLd.create({
    'type': 'Recipe',
    'name': entry.title,
    'url': entry.url,
    'description': "some description",
    'recipeIngredient': ["one", "two"],
    'recipeInstructions': ["three", "four"]
}) %}

<pre>
{{ myRecipe.render() }}
</pre>

...and it rendered:

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "Recipe",
    "description": "some description",
    "name": "Homepage",
    "recipeIngredient": [
        "one",
        "two"
    ],
    "recipeInstructions": [
        "three",
        "four"
    ],
    "url": "http://craft3.test/"
}
</script>

...which is what I'd expect (it's handling the arrays without an issue)...?

@khalwat khalwat added need info Need more information on the issue and removed bug labels Mar 21, 2018
@mgburns
Copy link
Author

mgburns commented Mar 21, 2018

Gah, you are correct -- should have done a minimal test case first, my apologies!

I was using filterByValue / column to extract a field from a matrix field block. In this case that field was a Redactor field, which returns a craft\redactor\FieldData instance rather than a string.

@mgburns mgburns closed this as completed Mar 21, 2018
@khalwat
Copy link
Collaborator

khalwat commented Mar 21, 2018

I was just wondering where filterByValue was coming from... a custom Twig filter or something?

@mgburns
Copy link
Author

mgburns commented Mar 21, 2018

It's from craft\helpers\ArrayHelper -- https://github.com/craftcms/cms/blob/develop/src/web/twig/Extension.php#L205.

Not sure when the Craft team snuck that in there but it saves me some gnarly {% for %} loops! :)

@khalwat
Copy link
Collaborator

khalwat commented Mar 21, 2018

ooooh, very handy, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need info Need more information on the issue
Projects
None yet
Development

No branches or pull requests

2 participants