web-dev-qa-db-ja.com

カスタムJFormFieldに新しいXML属性を設定する

こんにちは、カスタムコンポーネントに新しいフィールド属性を追加しようとしています。

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

次に、HTMLでレンダリングしたいと思います。誰かがそれを手伝ってくれる?

これは私のカスタムFormFieldコードです:

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

こんにちは私はそれを成し遂げることができました。新しい要素を設定して取得する必要がありました。次に、JFormFieldクラスの「セットアップ」関数をオーバーライドします。以下は私のコードです:

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