Skip to content

Inability to directly set and save changes to multi-value page reference field via a string of IDs #1900

Open

Description

@adrianbj

Short description of the issue

$page->of(false);
$page->field_name = '1234|1235';
$page->save('field_name');

won't overwrite existing entries for the field - the newly set values will be appended to existing ones. You need to do:

$page->of(false);
$page->field_name = null;
$page->field_name = '1234|1235';
$page->save('field_name');

or

$page->set('field_name', null);
$page->setAndSave('field_name', '1234|1235');

Expected behavior

Setting the field explicitly, rather than ->remove() and ->add() should remove all existing entries and replace with provided IDs.

Actual behavior

Existing selected page IDs are not automatically removed.

Activity

  1. adrianbj commented on Jun 14, 2026

    @adrianbj
    Author

    Root cause

    The asymmetry is in FieldtypePage::sanitizeValuePageArray() (wire/modules/Fieldtype/FieldtypePage/FieldtypePage.module).

    • When you assign an actual PageArray, it's returned as-is → replaces the existing value.
    • When you assign a string like '1234|1235', it's first converted by sanitizeValueString(), which returns a PageArray (via getById()). But that converted PageArray then falls into the block that merges each page into the existing value rather than replacing it:
    // if it's a new PageArray, combine it with the existing PageArray
    if($value instanceof PageArray) {
        foreach($value as $pg) {
            if(!$pg->id || $pg->id == $page->id) continue; 
            $pageArray->add($pg);   // <-- appends to existing, never clears
        }
        return $pageArray; 
    }

    $pageArray here is the field's current value, so a string of IDs always appends. That's why setting to null first (or using setAndSave()) is needed as a workaround.

    Suggested fix

    Treat a string that resolves to a PageArray (multiple values, e.g. "123|456") as an explicit replacement — the same as assigning a real PageArray. Single-value strings ('1234', '-1234' removal marker) keep their existing add/remove behavior, so BC for incremental adds is preserved.

    In sanitizeValuePageArray(), change:

    // if it's a string, see if we can convert it to a Page or PageArray
    if(is_string($value)) $value = $this->sanitizeValueString($page, $field, $value); 

    to:

    // if it's a string, see if we can convert it to a Page or PageArray
    if(is_string($value)) {
        $value = $this->sanitizeValueString($page, $field, $value); 
        // a string that resolves to multiple pages (e.g. "123|456") is an explicit
        // replacement of the whole value rather than an addition to it
        if($value instanceof PageArray) {
            foreach($value as $v) if($v->id == $page->id) $value->remove($v); 
            return $value;
        }
    }

    This makes $page->field_name = '1234|1235'; $page->save('field_name'); overwrite as expected, while leaving direct-PageArray assignment, single-page adds, and the -id removal syntax untouched.

  2. matjazpotocnik commented on Sep 11, 2026

    @matjazpotocnik
    Collaborator

    Note: issue #1959 is not related to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions