Magento addField()没有给你默认复选框

时间:2013-05-31 13:34:37

标签: magento magento-1.x

好的,模块im扩展使用addField()将字段添加到管理表单。我复制了这种行为,因为我认为我会尝试坚持他们的设置。但是,对于我的生活,我无法弄清楚如何在任何这些字段的右侧添加“使用默认”复选框。这是一个问题,因为我需要添加一个需要特定于站点的字段。

任何帮助都将不胜感激。

后代的代码:

$fieldset->addField('enable_coupon', 'select', array(
            'label' => Mage::helper('affiliatepluscoupon')->__('Enable Coupon'),
            'name' => 'enable_coupon',
            'note' => Mage::helper('affiliatepluscoupon')->__('If yes then it will create a magento salesrule for this store.'),
            'values' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray(),
        ));

澄清我正在寻找由管理字段输入的动态复选框,该字段根据您所在的视图而变化。这在通过XML创建字段时自动显示,但在使用addField()

添加字段时似乎省略了

更新:

我越是深入研究它,我越发现与xml一起使用的系统是一个非常深入的系统,复制所有这些系统只是为了坚持一些糟糕的编程实践,这有点荒谬。我将简单地添加到xml。谢谢你的帮助。

结论:

对于那些想知道如何使用addField()进行操作的人,我想出来了。这是我的最终代码:

 $inStore = Mage::app()->getRequest()->getParam('store');
        $defaultLabel = Mage::helper('affiliateplusprogram')->__('Use Default');
        $defaultTitle = Mage::helper('affiliateplusprogram')->__('-- Please Select --');
        $scopeLabel = Mage::helper('affiliateplusprogram')->__('STORE VIEW');
        $fieldset->addField('enable_coupon', 'select', array(
            'label' => Mage::helper('affiliatepluscoupon')->__('Enable Coupon'),
            'name' => 'enable_coupon',
            'note' => Mage::helper('affiliatepluscoupon')->__('If yes then it will create a magento salesrule for this store.'),
            'values' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray(),
            'disabled' => ($inStore && !$data['name_in_store']),
            'after_element_html' => $inStore ? '</td><td class="use-default">
            <input id="name_default" name="name_default" type="checkbox" value="1" class="checkbox config-inherit" ' . ($data['name_in_store'] ? '' : 'checked="checked"') . ' onclick="toggleValueElements(this, Element.previous(this.parentNode))" />
            <label for="name_default" class="inherit" title="' . $defaultTitle . '">' . $defaultLabel . '</label>
          </td><td class="scope-label">
            [' . $scopeLabel . ']
          ' : '</td><td class="scope-label">
            [' . $scopeLabel . ']',
        ));

希望有人觉得这很有用:)

4 个答案:

答案 0 :(得分:1)

您可以添加复选框的一种方法是

$fieldset->addField('enable_coupon', 'select', array(
         ....
  ))->setAfterElementHtml("
                  <span id='span_use_default'>
                     <input type='checkbox' value='1' name='use_default' id='use_default' /> 
                     Use Default
                   </span>
                 ");

你也检查了他们在模块中的表现方式吗?

答案 1 :(得分:0)

查看Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Inventory 还有catalog/product/tab/inventory.phtml

这看起来很有希望。

<legend><?php echo Mage::helper('catalog')->__('Inventory') ?></legend>
        <table cellspacing="0" class="form-list" id="table_cataloginventory">
        <tr>
            <td class="label"><label for="inventory_manage_stock"><?php echo Mage::helper('catalog')->__('Manage Stock') ?></label></td>
            <td class="value"><select id="inventory_manage_stock" name="<?php echo $this->getFieldSuffix() ?>[stock_data][manage_stock]" class="select" <?php echo $_readonly;?>>
                <option value="1"><?php echo Mage::helper('catalog')->__('Yes') ?></option>
                <option value="0"<?php if ($this->getConfigFieldValue('manage_stock') == 0): ?> selected="selected"<?php endif; ?>><?php echo Mage::helper('catalog')->__('No') ?></option>
            </select>
            <input type="hidden" id="inventory_manage_stock_default" value="<?php echo $this->getDefaultConfigValue('manage_stock'); ?>" />

            <?php $_checked = ($this->getFieldValue('use_config_manage_stock') || $this->IsNew()) ? 'checked="checked"' : '' ?>
            <input type="checkbox" id="inventory_use_config_manage_stock" name="<?php echo $this->getFieldSuffix() ?>[stock_data][use_config_manage_stock]" value="1" <?php echo $_checked ?> onclick="toggleValueElements(this, this.parentNode);" class="checkbox" <?php echo $_readonly;?>/>
            <label for="inventory_use_config_manage_stock" class="normal"><?php echo Mage::helper('catalog')->__('Use Config Settings') ?></label>
            <?php if (!$this->isReadonly()):?><script type="text/javascript">toggleValueElements($('inventory_use_config_manage_stock'), $('inventory_use_config_manage_stock').parentNode);</script><?php endif; ?></td>
            <td class="value scope-label"><?php echo Mage::helper('adminhtml')->__('[GLOBAL]') ?></td>
        </tr>

答案 2 :(得分:0)

嗯,这是一个非常丑陋的解决方案,但也许它会对你有用。首先,在产品页面上,每个元素都有一个自定义渲染器,这就是它显示在那里的原因。所以,如果您有以下元素:

$name = $fieldset->addField('name', 'text', array(
    'name' => 'name',
    'required' => true,
    'class' => 'required-entry',
    'label' => Mage::helper('some_helper')->__('Name'),
));

您必须使用自定义渲染器渲染它:

if ($name)
{
    $name->setRenderer(
            $this->getLayout()->createBlock('adminhtml/catalog_form_renderer_fieldset_element')
    );
}

此时,您应该拥有scope-label类的第三列。但它附近的复选框仍然无法显示。为此,我们必须为表单设置以下内容:

$storeObj = new Varien_Object();
$storeId = $this->getRequest()->getParam("store");
$storeObj->setId($storeId);
$storeObj->setStoreId($storeId);
$form->setDataObject($storeObj);

现在您还应该看到复选框。

此解决方案来自:

http://code007.wordpress.com/2014/03/20/how-to-show-the-default-checkbox-near-a-magento-attribute/

答案 3 :(得分:0)

我知道这有点晚了,但我只想发布我的解决方案,或许会引发其他一些想法。 (以及对我工作方式的反馈)

我的初始来源:http://marius-strajeru.blogspot.be/2013/02/create-system-config-section-with.html

所以要添加一个字段(例如:文本字段):

$field = $element->addField( 'myFieldID', 'text',
  array(
    'name'  => 'groups[model_name][fields][var_name][value]', // this value will be saved to the database as module_name/model_name/var_name and you can get it by Mage::getStoreConfig(..)
    'label' => 'Title', // This is the human readable label up front 
    // See how to get the saved value and define inherit in the link above, this is not in scope for this question. Like this you can't ever see the value that you saved.
    'value' => 'My Title', // The initial value
    'inherit' => true, // Checks the inherit cb after the field
    'can_use_default_value' => true, // Can inherit from default level
    'can_use_website_value' => true, // Can inherit from website level
  ))->setRenderer(Mage::getBlockSingleton('adminhtml/system_config_form_field')); // Use the same renderer as for the system fields (this adds the cb at the end)

您只需在字段中添加复选框即可。 如果你想添加那些灰色范围文本(例如:[网站]):

$field['scope'] = true; // Display scope label
$field['scope_label'] = '[WEBSITE]';

这可以完成,因为基本的Varien对象被定义为实现ArrayAccess

class Varien_Object implements ArrayAccess

现在要渲染该字段:

echo $field->toHtml();