statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.05k stars 530 forks source link

3.3 — `Statamic\Fields\Values` object renders Term field type as `null` #5472

Closed johncarter- closed 2 years ago

johncarter- commented 2 years ago

Bug description

It seems that a Term field inside a Statamic\Fields\Values object doesn't properly work with property access.

The term has data as seen in the line retrieving the raw() product_size.

The other field types work: ✅ Text ✅ Toggle ✅ Integer ❌ Term

How to reproduce

$variations = $page->variations;

collect($variations)->each(function (Statamic\Fields\Values $variation) {

            ray($variation); // Statamic\Fields\Values

            ray($variation->toArray()['product_size']->raw()); // ✅ 'jum-8'
            ray($variation->product_size); // 🚨 null

            ray($variation->sku); // 009710446001
            ray($variation->stock); // 1
            ray($variation->enabled); // true
});

Product blueprint (relevant section):

title: Product
sections:
  main:
    display: Main
    fields:
      -
        handle: title
        field:
          type: text
          required: true
          read_only: true
          validate:
            - required
      -
        handle: variations
        field:
          fields:
            -
              handle: product_size
              field:
                max_items: 1
                mode: typeahead
                display: 'Variation Size'
                type: terms
                icon: taxonomy
                width: 50
                listable: hidden
                taxonomies:
                  - product_size
            -
              handle: sku
              field:
                input_type: text
                display: SKU
                type: text
                icon: text
                listable: hidden
                width: 50
            -
              handle: stock
              field:
                display: Stock
                type: integer
                icon: integer
                listable: hidden
                width: 33
            -
              handle: enabled
              field:
                display: Enabled
                type: toggle
                icon: toggle
                width: 25
                listable: hidden
                default: true
          mode: table
          reorderable: true
          display: Variations
          type: grid
          icon: grid
          listable: hidden

Product entry markdown

variations:
  -
    sku: '009710446001'
    stock: 1
    product_size: jum-8
    enabled: true
  -
    sku: '009710446002'
    stock: 1
    product_size: jum-10
    enabled: true
  -
    sku: '009710446003'
    stock: 0
    product_size: jum-12
    enabled: true
  -
    sku: '009710446004'
    stock: 1
    product_size: jum-14
    enabled: true
  -
    sku: '009710446005'
    stock: 1
    product_size: jum-16
    enabled: true
  -
    sku: '009710446006'
    stock: 0
    product_size: jum-18
    enabled: true
  -
    sku: '009710446007'
    stock: 0
    product_size: jum-20
    enabled: false
  -
    sku: '009710446008'
    stock: 0
    product_size: jum-22
    enabled: false
  -
    sku: '009710446009'
    stock: 0
    product_size: jum-6
    enabled: false

Logs

No response

Versions

Statamic 3.3.0-beta.6 Pro Laravel 8.83.4 PHP 8.1.1 spatie/statamic-responsive-images 2.9.1

Installation

Other (please explain)

Additional details

Can't remember how I installed it.

jasonvarga commented 2 years ago

This line:

ray($variation->product_size); // 🚨 null

outputs a LocalizedTerm instance for me.

Are you sure there's a term named jum-8 ?

If I adjust my product_size value from jum-8 to definitely-doesnt-exist... I get null like you're showing.

johncarter- commented 2 years ago

Yeah it's there. I'll double check that I can get the LocalizedTerm instance using the Term Facade in the morning, just to make sure it's working properly.

jasonvarga commented 2 years ago

I've found another issue with taxonomy terms fields though, so it might be related. I'll let you know.

jasonvarga commented 2 years ago

Yup it has to do with the field being the same name as the taxonomy.

If you rename your product_size field to the_product_size, it should work. (But obviously you shouldn't need to rename it)

This is the "taxonomizing" logic kicking in. But it shouldn't happen on nested fields (ie. inside a grid).