Вопрос

I'd like to extend AddCommentForm so I can output field labels with custom classes. I want to remove 'left' class from label as it conflicts with Foundation but ideally I want to have full control over outputted HTML.

I tried to extend CommentingController in mysite/code but no luck there...

Can I set it up so every field type has it's own .ss template?

How do I do this?

Это было полезно?

Решение

In SilverStripe you can overwrite templates just by creating a file of the same name in your mysite or theme folder.

so, lets say you have a Form of the class SomeForm all you need to do is create a file called SomeForm.ss and SilverStripe will use that instead of the default Form.ss.

However, the css class 'left' is not added in Form.ss, in that template there is only the form html, the fields are added in a loop.
(Also, the comment module does not use a class for the Form, so that won't work here anyway.)

Each field has its own template (actually 2 templates). MyField.ss and MyField_holder.ss.
If one of the 2 templates does not exist, it will fall back to the default files FormField.ss and FormField_holder.ss.

so lets take for example the class TextField, it will use:

  • FormField_holder.ss because there is no TextField_holder.ss
  • TextField.ss

the <label> you seek is inside FormField_holder.ss.
You could of course just create a FormField_holder.ss and copy the Content from the original file, but this would also effect the CMS. So you need some way to only effect the frontend.

Unfortunately, I don't really have nice and clean a solution for this problem. Usually I would recommend sub classing that form and overwriting the template for the Fields in a loop.
But I think you are using this module here: https://github.com/silverstripe/silverstripe-comments/ which does no use a class for that form, so we have to use a work around and hook into that controller.


File MyCommentControllerExntesion:

class MyCommentControllerExntesion extends Extension {
    public function alterCommentForm($form) {
        foreach($form->Fields() as $field) {
            if (!$field->is_a('HiddenField') {
                // skip hidden fields
                $field->setFieldHolderTemplate('MyFrontEndField_holder');
            }
        } 
    }
}

File config.yml:

CommentingController:
  extensions:
    - 'MyCommentControllerExntesion'

File MyFrontEndField_holder.ss:

<div id="$Name" class="field<% if $extraClass %> $extraClass<% end_if %>">
    <% if $Title %><label class="not-left" for="$ID">$Title</label><% end_if %>
    <div class="middleColumn">
        $Field
    </div>
    <% if $RightTitle %><label class="right" for="$ID">$RightTitle</label><% end_if %>
    <% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
    <% if $Description %><span class="description">$Description</span><% end_if %>
</div>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top