Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectionGroup not adding extra attributes

I have the PHP code:

SelectionGroup::create(
//...
SelectionGroup_Item::create(/*...*/),
SelectionGroup_Item::create(/*...*/),
//...
)
->addExtraClass("some-extra-class")
->setAttribute('ng-change','log(myModel)')
->setAttribute('ng-model','myModel')

and the rendered html looks like:

...
<ul class="SelectionGroup field CompositeField selectiongroup some-extra-class nolabel">
    ...
</ul>
...

My extra class is being added, why aren't my extra attributes being added?


This SelectionGroup is part of a FieldList, other Fields allow attributes to be set, and Fields within the SelectionGroup_Item can have their attributes set eg:

FieldList::create([
  HiddenField::create(...)->setAttribute("does","this work"),
  SelectionGroup::create(
    //...
    SelectionGroup_Item::create('name',
      FieldGroup::create(null,[
        HiddenField::create(...)->setAttribute("maybe","it does")
      ])
    ),
    SelectionGroup_Item::create(...),
    //...
  )
  ->addExtraClass("some-extra-class")
  ->setAttribute('ng-change','log(myModel)')
  ->setAttribute('ng-model','myModel')
])

Renders the following HTML:

...
<input type="hidden" ... does="this work" />
<ul class="SelectionGroup field CompositeField selectiongroup some-extra-class nolabel">
  ...
  <input type="hidden" ... maybe="it does" />
  ...
</ul>
...
like image 435
Isaac Avatar asked Jan 20 '26 19:01

Isaac


1 Answers

In SilverStripe 3.4 the $AttributesHTML variable is not called in the default template that SelectionGroup_Item uses.

SelectionGroup_Item uses the CompositeField template (as it extends CompositeField and it does not have it's own template set in framework).

The current CompositeField template in framework does not include $AttributesHTML in the opening tag:

<$Tag class="CompositeField $extraClass <% if ColumnCount %>multicolumn<% end_if %>">
    <% if $Tag == 'fieldset' && $Legend %>
        <legend>$Legend</legend>
    <% end_if %>

    <% loop $FieldList %>
        <% if $ColumnCount %>
            <div class="column-{$ColumnCount} $FirstLast">
                $Field
            </div>
        <% else %>
            $Field
        <% end_if %>
    <% end_loop %>
</$Tag>

We can create our own SelectionGroup_Item template or CompositeField to add the $AttributesHTML variable in.

To do this we create a SelectionGroup_Item.ss file in our mysite/templates/includes directory.

mysite/templates/includes/SelectionGroup_Item.ss

<$Tag $AttributesHTML class="CompositeField $extraClass <% if ColumnCount %>multicolumn<% end_if %>">
    <% if $Tag == 'fieldset' && $Legend %>
        <legend>$Legend</legend>
    <% end_if %>

    <% loop $FieldList %>
        <% if $ColumnCount %>
            <div class="column-{$ColumnCount} $FirstLast">
                $Field
            </div>
        <% else %>
            $Field
        <% end_if %>
    <% end_loop %>
</$Tag>

After creating this template we need to call ?flush=all in the page URL for the system to clear it's cache and find this new template.

like image 91
3dgoo Avatar answered Jan 23 '26 09:01

3dgoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!