web-dev-qa-db-fra.com

Définir un nouvel attribut XML dans JFormField personnalisé

Bonjour, je cherche à ajouter un nouvel attribut de champ pour mon composant personnalisé.

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

Ensuite, je voudrais le rendre dans mon HTML. Quelqu'un pourrait aider avec cela?

Ceci est mon code FormField personnalisé:

<?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
Sahil Purav

Salut j'ai pu le faire. Je devais juste définir et obtenir un nouvel élément. Et ensuite, remplacez la fonction "setup" de la classe JFormField. Voici mon 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);
    }

}
3
Sahil Purav