Hi,
I want to extend SS 2.4 form fields to be able to add html5 attributes and some other stuff.
I don't want to override every form field, so I tried to adapt the createTag method from the FormField class.
Trying the following did not work:
File: MyHTML5FormField.php
<?php
class MyHTML5FormField extends FormField {
/**
* Construct and return HTML tag.
*
* @todo Transform to static helper method.
*/
public function createTag($tag, $attributes, $content = null) {
// my custom stuff here
$preparedAttributes = '';
foreach($attributes as $k => $v) {
// Note: as indicated by the $k == value item here; the decisions over what to include in the attributes can sometimes get finicky
if (!empty($v) || $v === '0' || $k == 'value') $preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\"";
}
if ($content || !in_array($tag, array('input', 'img'))) {
return "<$tag$preparedAttributes>$content</$tag>";
} else {
return "<$tag$preparedAttributes />";
}
}
}
File: _config.php
Object::useCustomClass('FormField', 'MyHTML5FormField');
Then I tried to use Extensions, which also did not work:
File: MyHTML5FormFieldExtensions.php
<?php
class MyHTML5FormFieldExtensions extends Extension {
/**
* Construct and return HTML tag.
*
* @todo Transform to static helper method.
*/
public function createTag($tag, $attributes, $content = null) {
// my custom stuff here
$preparedAttributes = '';
foreach($attributes as $k => $v) {
// Note: as indicated by the $k == value item here; the decisions over what to include in the attributes can sometimes get finicky
if (!empty($v) || $v === '0' || $k == 'value') $preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\"";
}
if ($content || !in_array($tag, array('input', 'img'))) {
return "<$tag$preparedAttributes>$content</$tag>";
} else {
return "<$tag$preparedAttributes />";
}
}
}
File: _config.php
Object::add_extension('FormField', 'MyHTML5FormFieldExtensions');
So, is it possible to do it as I tried? Or do I have to extend FormField with my own class and "re-write" every single form field and inherit it from my custom class?
thanks for your help!
Manuel