Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SS 2.4.7: Is it possible to override a class method in an extension?

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

Page: 1
Go to End
Author Topic: 610 Views
  • memdev
    Avatar
    Community Member
    3 Posts

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

    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

  • Martin D.
    Avatar
    Community Member
    20 Posts

    Re: SS 2.4.7: Is it possible to override a class method in an extension? Link to this post

    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."

    610 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.