Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Field of type toggle is empty in table when using fieldMethods #35

Closed
felixbridell opened this issue Aug 30, 2019 · 2 comments
Closed
Assignees
Labels
question Further information is requested

Comments

@felixbridell
Copy link

In my blueprint I have:

merchant_categories:
    type: pagetable
    headline: Handlare
    create: merchant
    status: all
    limit: 100
    columns:
        local_image_save_to_s3:
        label: S3-bucket
        text: '{{page.local_image_save_to_s3.statusLabel}}'
        type: text
        searchable: false

local_image_save_to_s3 is a type toggle in the subpage blueprint

local_image_save_to_s3:
    label: Spara produktbild till S3-bucket?
    type: toggle
    text: 
        - Nej
        - Ja
        default: false

Inside plugins/my-methods/index.php I have this fieldMethod:

Kirby::plugin('mashplan/StatuslabelField', [
  'fieldMethods' => [
      'statusLabel' => function($field) {
        $value = $field->value;
        return '<span class="status-label" data-status-type="'. $value .'">' . $value . '</span>';
      },
  ]
]);

This fieldMethod works fine on another field which is of type text but on this one of type toggle it is empty

@sylvainjule sylvainjule self-assigned this Sep 2, 2019
@sylvainjule sylvainjule added the question Further information is requested label Sep 2, 2019
@sylvainjule
Copy link
Owner

sylvainjule commented Sep 4, 2019

It is not specific to pagetable, if you try outputing this statusLabel in the info option of a Pages section, the issue will still occur.

The issue you're having definitely comes from the field name, I don't know what's going wrong with underscores and numbers in field names (I never use any), but switching local_image_save_to_s3 to localImage fixed the issue.

Also, the value stored in a toggle won't be your displayed terms. To display them, you can change your field method to:

'statusLabel' => function($field) {
    $value = $field->isTrue() ? 'Ja' : 'Nej'; 
    return '<span class="status-label" data-status-type="'. $value .'">' . $value . '</span>';
},

@sylvainjule
Copy link
Owner

Closing this issue, please reopen if the answer above doesn't help. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants