Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SS 2.4.7: Is it possible to override a class method in an extension?


Go to End


2 Posts   2712 Views

Avatar
memdev

Community Member, 6 Posts

5 October 2012 at 10:02pm

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

Avatar
Martin D.

Community Member, 21 Posts

25 October 2012 at 2:02am

I am having the same issue at the moment so I found this article online:
http://www.webbower.com/blog/the-power-of-the-silverstripe-extension-class/

The bad news is:
"you can ADD functionality in the form of new methods, but you can't overwrite existing methods on the class you're extending."