2013-03-25 4 views
0

Как сделать так, чтобы при редактировании записи было выбрано правильное значение для моего настраиваемого типа поля? У меня это до сих пор:Joomla custom field type при редактировании

class JFormFieldCustom extends JFormField { 

    protected $type = 'Custom'; 

    // getLabel() left out 

    public function getInput() { 

      return '<select id="'.$this->id.'" name="'.$this->name.'">'. 
         '<option value="1" >1</option>'. 
         '<option value="2" >2</option>'. 
        '</select>'; 
    } 

}

Как передать выбранное значение этому классу, так что я могу сделать:

<option value="1"SELECTED>1</option> 

или

<option value="2" SELECTED>2</option> 

Спасибо!

+0

Где вы используете это поле (например, в вашем компонентах пункт PARAMATERS, в меню .. и т.д.) – Craig

ответ

0

Используйте $this->value, чтобы получить выбранного value.Try this-

class JFormFieldCustom extends JFormField { 

     protected $type = 'Custom'; 

     // getLabel() left out 

     public function getInput() { 

       return '<select id="'.$this->id.'" name="'.$this->name.'">'. 
          '<option value="1" <?php echo ($this->value==1)?'selected':''?>>1</option>'. 
          '<option value="2" <?php echo ($this->value==2)?'selected':''?>>2</option>'. 
         '</select>'; 
     } 
    } 

Надеется, что это поможет.

3

Это проще использовать то, что уже есть, то есть расширить JFormFieldList вместо JFormField, то все, что вам нужно сделать, это вернуть option's для вашего списка. Унаследовал функциональность сделает все остальное за вас - в том числе выбрать опцию, которая соответствует $this->value

<?php 
/** 
* Do the Joomla! security check and get the FormHelper to load the class 
*/ 
defined('_JEXEC') or die('Restricted Access'); 

JFormHelper::loadFieldClass('list'); 

class JFormFieldMyCustomField extends JFormFieldList 
{ 
    /** 
    * Element name 
    * 
    * @var  string 
    */ 
    public $type = 'MyCustomField'; 

    /** 
    * getOptions() provides the options for the select 
    * 
    * @return array 
    */ 
    protected function getOptions() 
    { 
     // Create an array for our options 
     $options = array(); 
     // Add our options to the array 
     $options[] = array("value" => 1, "text" => "1); 
     $options[] = array("value" => 1, "text" => "1); 
     return $options; 
    } 
} 
0

Выберите для Joomla http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL * @copyright (c) 2017 Компания YouTech. Все права защищены. * @author macasin */ определено ('_ JEXEC') или умереть;

JFormHelper :: loadFieldClass ('list');

класс JFormFieldSelect расширяет JFormFieldList { protected $ type = 'select';

protected function getInput() 
{ 
    $html = array(); 
    $attr = ''; 

    // Initialize some field attributes. 
    $attr .= !empty($this->class) ? ' class=select ' . $this->class . '"' : ' class=select '; 
    $attr .= $this->readonly ? ' readonly' : ''; 
    $attr .= $this->disabled ? ' disabled' : ''; 
    $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : ''; 
    $attr .= $this->required ? ' required aria-required="true"' : ''; 

    // Initialize JavaScript field attributes. 
    $attr .= $this->onchange ? ' onchange="' . $this->onchange . '"' : ''; 

    // Get the field options. 
    $options = $this->getOptions(); 

    // Load the combobox behavior. 
    JHtml::_('behavior.combobox'); 

    $html[] = '<div class="select input-append">'; 

    // Build the input for the combo box. 
    $html[] = '<select name="' . $this->name . '" id="' . $this->id . '" value="' 
     . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $attr . ' autocomplete="off" >'; 

    foreach ($options as $option) 
    { 
     $html[] = '<option '.($option->value == $this->value ? "selected" : "").' value='.$option->value.'>' . $option->text . '</option>'; 

    } 
    $html[] = '</select></div>'; 

    return implode($html); 
} 

}