Use variable to find field?

This is a result of a larger problem / discussion, but, I need to access some fields on a page from an array of “problem fields”, using a hook.

I suppose it’s more of a general PHP question, but, how do I make this work?

kirby()->hook('panel.page.update', function($page) {
  $problem_fields = array('text', 'feild_two', 'feild_three');
  foreach ($problem_fields as $field) {
    if (isset($page->{$field}) {
      $text = strip_tags($page->{$field});
      $page->update(array($field => $text));
    }
  }
}

Try this:

kirby()->hook('panel.page.update', function($page) {
  $problem_fields = array('text', 'field_one', 'field_two');
    foreach ($problem_fields as $field) {
      if ($page->$field()) {
        $text = strip_tags($page->$field());
        $page->update(array($field => $text));
      }
    }
});
2 Likes

so simple, slapping my forehead

Thanks, @texnixe, quick and smart as always!!