1
votes

I'm trying to pull out enum values from a dataobject to act as a menu/filter. I have not been able to find any documentation on how to do this and my attempts have all failed.

For example I have:

class Specification extends DataObject {
    private static $db = array (
        'Standard' => 'Enum("BS 1400,AS 1565")'
    );
}

I'm trying to do something like:

public function Standards() {
    $stnds = Specification::get()->dbObject('Standard')->enumValues();
    $list = ArrayList::create();

    foreach ($stnds as $stnd) {
        $list->push($stnd);
    }
    return $list;
}

I want to be able to loop the resulting values in the template, but can't access the labels - if I do:

<% loop Standards %>
    $Pos
<% end_loop %>

This gives me 1 2, which suggests it is working, but cannot access the enum value labels in the template.

How do I correctly return these values so they can be looped through in the template?

1

1 Answers

1
votes

When you push items into an ArrayList object, they are just stored inside a php array. The SS template parser does not deal with php arrays, therefore one solution to your issue is to wrap your item inside an ArrayData before pushing it, like below:

public function Standards(){
  $stnds = Specification::get()->dbObject('Standard')->enumValues();
  $list = ArrayList::create();

  foreach ($stnds as $stnd) {
      $list->push(new ArrayData(array('Standard' => $stnd)));
  }
  return $list;
}   

Then, in your template:

<% loop Standards %>
  <h1>$Pos $Standard</h1>
<% end_loop %>