2

Hi I'm looking to add new field attribute for my custom component.

<field type="myfield" mycustomattribute="somevalue" />

Then, I would like to render it in my HTML. Could someone help with it?

This is my custom FormField Code:

<?php

/*
 * @package     Joomla.Administrator
 * @subpackage  com_casestudy
 * 
 * @Author      Sahil Purav
 * @contact     [email protected]
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

class JFormFieldUpload extends JFormFieldFile {

    /**
     * The form field type.
     *
     * @var    string
     *
     * @since  11.1
     */
    protected $type = "upload";

    /**
     * The button text of the field.
     *
     * @var    string
     */
    protected $btnString;

    public function getInput() {
        // Initialize some field attributes.
        $accept = !empty($this->accept) ? ' accept="' . $this->accept . '"' : '';
        $size = !empty($this->size) ? ' size="' . $this->size . '"' : '';
        $class = !empty($this->class) ? ' class="' . $this->class . '"' : '';
        $disabled = $this->disabled ? ' disabled' : '';
        $required = $this->required ? ' required aria-required="true"' : '';
        $autofocus = $this->autofocus ? ' autofocus' : '';
        $btnString = !empty($this->btnString) ? $this->btnString : JText::_('COM_HELPDESK_BROWSE_BUTTON');

        $html = array();

        $html[] = '<div id="file-upload">';
        $html[] = '<button>' . $btnString . '</button>';
        $html[] = '</div>';

        return implode($html);
    }

}
2
  • You can't just create custom attributes on their own. You need to create a custom form field, to be allowed to access a custom attribute. See this example: stackoverflow.com/questions/11161640/…
    – Lodder
    Commented Apr 21, 2015 at 13:37
  • Thanks for your reply Lodder. I've already created custom field. Look at $btnString This will be decided with <field type="upload" btnString="Browse Files" /> Here "btnString" is custom attribute Commented Apr 21, 2015 at 14:00

1 Answer 1

3

Hi I was able to get it done. I just had to set and get new element. And then, override "setup" function of JFormField class. Following is my code:

<?php

/*
 * @package     Joomla.Administrator
 * @subpackage  com_casestudy
 * 
 * @Author      Sahil Purav
 * @contact     [email protected]
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

class JFormFieldUpload extends JFormField {

    /**
     * The form field type.
     *
     * @var    string
     *
     * @since  11.1
     */
    protected $type = "upload";

    /**
     * The button text of the field.
     *
     * @var    string
     */
    protected $btnString;

    /**
     * Method to get certain otherwise inaccessible properties from the form field object.
     *
     * @param   string  $name  The property name for which to the the value.
     *
     * @return  mixed  The property value or null.
     *
     */
    public function __get($name) {

        switch ($name) {
            case 'btnString':
                return $this->$name;
        }
        return parent::__get($name);
    }

    /**
     * Method to set certain otherwise inaccessible properties of the form field object.
     *
     * @param   string  $name   The property name for which to the the value.
     * @param   mixed   $value  The value of the property.
     *
     * @return  void
     *
     */
    public function __set($name, $value) {

        switch ($name) {
            case 'btnString':
                $this->btnString = (string) $value;
                break;
            default:
                parent::__set($name, $value);
        }
    }

    /**
     * Method to attach a JForm object to the field.
     *
     * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
     * @param   mixed             $value    The form field value to validate.
     * @param   string            $group    The field name group control value. This acts as as an array container for the field.
     *                                      For example if the field has name="foo" and the group value is set to "bar" then the
     *                                      full field name would end up being "bar[foo]".
     *
     * @return  boolean  True on success.
     *
     * @see     JFormField::setup()
     * @since   3.2
     */
    public function setup(\SimpleXMLElement $element, $value, $group = null) {

        $result = parent::setup($element, $value, $group);

        if ($result == TRUE) {
            $this->btnString = (string) $this->element['btnString'];
        }

        return $result;
    }

    /**
     * Method to get the field input markup.
     *
     * @return  string  The field input markup.
     *
     * @since   11.1
     */
    public function getInput() {
        // Initialize some field attributes.
        $accept = !empty($this->accept) ? ' accept="' . $this->accept . '"' : '';
        $size = !empty($this->size) ? ' size="' . $this->size . '"' : '';
        $class = !empty($this->class) ? ' class="' . $this->class . '"' : '';
        $disabled = $this->disabled ? ' disabled' : '';
        $required = $this->required ? ' required aria-required="true"' : '';
        $autofocus = $this->autofocus ? ' autofocus' : '';
        $btnString = !empty($this->btnString) ? $this->btnString : JText::_('COM_HELPDESK_BROWSE_BUTTON');

        $html = array();

        $html[] = '<div id="file-upload">';
        $html[] = '<button type="button" id="' . $this->id . '"' . $class . $disabled . $required . $autofocus . '>' . $btnString . '</button>';
        $html[] = '</div>';

        return implode($html);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.