Установите новый атрибут XML в пользовательский JFormField

Привет. Я хочу добавить новый атрибут поля для моего настраиваемого компонента.

<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 21 PMpTue, 21 Apr 2015 16:33:14 +030033Tuesday 2015, 16:33:14

1 ответ


3

Привет, я смог это сделать. Мне просто нужно было установить и получить новый элемент. И затем переопределить функцию «настройки» класса 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);
    }

}
ответил Sahil Purav 21 PMpTue, 21 Apr 2015 17:20:05 +030020Tuesday 2015, 17:20:05

Похожие вопросы

Популярные теги

security × 330linux × 316macos × 2827 × 268performance × 244command-line × 241sql-server × 235joomla-3.x × 222java × 189c++ × 186windows × 180cisco × 168bash × 158c# × 142gmail × 139arduino-uno × 139javascript × 134ssh × 133seo × 132mysql × 132