cms icon indicating copy to clipboard operation
cms copied to clipboard

Computed Fields in Replicator Sets Not Included in Front-End Augmentation

Open joshdaugherty opened this issue 10 months ago • 0 comments

Bug description

What Happened?

When using a Replicator field with a computed field (e.g., visibility: computed) defined via Collection::computed(), the computed value is correctly displayed in the Statamic control panel but is absent from the front-end output (e.g., {{ select_publications | to_json | raw }}). This appears to occur because performAugmentation() in src/Fieldtypes/Replicator.php does not include visibility: computed fields in its default augmentation pipeline, unlike the control panel's preProcess() method, which uses withComputedValues().

For example, in a pages collection with a select_publications Replicator field, I compute pubmed_information based on pmid. In the control panel, pubmed_information shows as expected (e.g., 'foo' or an API-derived citation). On the front end, it's missing from the augmented output, showing only stored fields like pmid.

What Did I Expect to Happen?

I expected computed fields within Replicator sets to be included in both the control panel and front-end output by default, consistent with their visibility: computed intent (calculated dynamically, not stored). The current behavior creates an inconsistency between the control panel and front-end rendering, requiring workarounds like setting visibility: read_only (and stripping on save) or custom preprocessing.

Proposed Fix

I recommend modifying line 210 in src/Fieldtypes/Replicator.php (link to line) from:

$values = $this->fields($set['type'], $index)->addValues($set)->{$augmentMethod}()->values();

to:

$values = $this->fields($set['type'], $index)->addValues($set)->{$augmentMethod}()->withComputedValues()->values();

This change calls withComputedValues() before values(), ensuring computed fields are included in the augmentation output, aligning front-end behavior with the control panel.

How to reproduce

1. Set Up a Collection

Add a Replicator field to the pages collection blueprint:

fields:
  -
    handle: select_publications
    field:
      type: replicator
      display: 'Selected Publications'
      sets:
        new_set:
          display: 'New Set'
          fields:
            -
              handle: pmid
              field:
                type: text
                display: 'PubMed ID'
            -
              handle: pubmed_information
              field:
                type: textarea
                display: 'PubMed Info'
                visibility: computed

2. Define a Computed Callback

In app/Providers/AppServiceProvider.php:

<?php

namespace App\Providers;

// ...
use Statamic\Facades\Collection;
// ...

class AppServiceProvider extends ServiceProvider
{
    // ...

    public function boot()
    {
        // ...

        Collection::computed('pages', 'select_publications', function ($entry) {
            $sets = $entry->get('select_publications', []);
            return array_map(function ($set) {
                $set['pubmed_information'] = 'foo';
                return $set;
            }, $sets);
        });
        
        // ...
    }

    // ...

}

3. Create an Entry

Add an entry to pages with a select_publications Replicator set, with the pmid field containing a value of 12345.

4. Test Control Panel

View the entry in the control panel and see that pubmed_information displays foo.

5. Test Front End

Use a template (e.g., resources/views/pages/show.antlers.html):

{{ select_publications }}
  <p>PMID: {{ pmid ?? 'no value' }}<br>
  PubMed Information: {{ pubmed_information ?? 'no value' }}</p>
{{ /select_publications }}

Visit the page and see that the expected value for pubmed_information is missing:

<p>PMID: 12345<br>
PubMed Information: no value</p>

6. Apply Workaround

Change visibility: computed to visibility: read_only and see that foo appears in the front end:

<p>PMID: 12345<br>
PubMed Information: foo</p>

Logs


Environment

Environment
Application Name: UAMS Statamic Sandbox
Laravel Version: 11.42.1
PHP Version: 8.2.26
Composer Version: 2.8.6
Environment: local
Debug Mode: ENABLED
URL: uams-statamic-sandbox.test
Maintenance Mode: OFF
Timezone: America/Chicago
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: NOT CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 8
Sites: 26 (UAMS Statamic Sandbox, College of Medicine, Department of Anesthesiology, and 23 more)
Stache Watcher: Enabled (auto)
Static Caching: Disabled
Version: 5.49.1 PRO

Statamic Addons
el-schneider/statamic-admin-bar: 0.2.0
jacksleight/statamic-bard-mutator: 3.0.3
statamic/importer: 1.7.3
statamic/ssg: 3.1.1
studio1902/statamic-peak-browser-appearance: 3.6.1
studio1902/statamic-peak-commands: 8.16.1
studio1902/statamic-peak-seo: 8.19.1
studio1902/statamic-peak-tools: 7.1.0

Installation

Starter Kit using via CLI

Additional details

No response

joshdaugherty avatar Mar 06 '25 16:03 joshdaugherty