$page->increment($field) in multilang environment

How can I use $page->increment($field) in a multilingual site so that the value of $field is language agnostic?
I have tried adding translate: false in the blueprint, but that didn’t help.

Thx

As far as I can see, there is, unfortunately, no way of passing a language to the increment() method. I’d try to use the underlying $page->update()method instead.

Pity.

For future reference; this works ->

try {
    $page->update(array("subscriptionscount" => $page->content("nl")->subscriptionscount()->value() + 1), "nl");
} catch(Exception $e) {
    echo $e->getMessage();
}

Thx for clarifying, @texnixe

Or as a custom method:

page::$methods['plusOne'] = function($page, $field, $by = 1, $max = null, $lang = null) {
  $page->update([
    $field => function($value) use($by, $max) {
      $new = (int)$value + $by;
      return ($max and $new >= $max) ? $max : $new;
    }
  ], $lang);
  return $page;
};

Use in template:

$page->plusOne('subscriptionscount', 1, null, 'nl');
1 Like

@texnixe, Brilliant. This should be the $page->increment() behaviour. :stuck_out_tongue_winking_eye:

I added it to the custom method collection and created an issue on GitHub:

1 Like