【问题标题】:Magento addField() does not give you default checkboxMagento addField() 没有给你默认复选框
【发布时间】:2013-05-27 08:25:30
【问题描述】:

我正在扩展的模块使用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() 添加字段时似乎被遗漏了。

【问题讨论】:

  • 是的,我在商店视图中。我正在研究现在发布的两个答案。

标签: magento


【解决方案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>
                 ");

您还检查了他们在模块中的操作方式吗?

【讨论】:

  • 我正在寻找自动的“使用默认值”复选框,该复选框会根据您在哪个视图中更改....如果您的默认值则不会显示。当您使用 xml 创建管理字段时,这一切都会自动发生,但是当您使用 addField 时,它不会显示出来。此外,他们以这种方式添加的字段也没有此复选框。老实说,我可能只是覆盖并添加到 xml,但我已经覆盖了这方面,所以我想知道他们是否是继续使用这种方法的一种方式。
  • 我将此标记为正确答案只是因为它是最基本的答案....这不是我要找的,而是您的权利:)
【解决方案2】:

查看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>

【讨论】:

  • 这看起来是为了获取默认信息,但不是我想要的。我专门根据您的默认/站点或站点视图来寻找动态的复选框。这会在通过 xml 添加字段时自动出现...
  • 据我所知,system.xml 字段是通过不同的方法加载的。他们可能使用类似于上述 .phtml 的东西
  • 实际上,我研究得越多,我就越意识到它是一个非常深入的系统,如果只是为了坚持一些糟糕的编程实践而复制所有这些系统是有点荒谬的。这一切都从 Varien_Data_Form_Element_Abstract 开始。
【解决方案3】:

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

$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/

【讨论】:

    【解决方案4】:

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

    我的初始来源: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)
    

    这就是在字段中添加复选框所需要做的一切。 如果您想添加那些灰色范围的文本(例如:[WEBSITE]):

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

    可以这样做是因为定义了基本的 Varien 对象来实现 ArrayAccess

    class Varien_Object implements ArrayAccess
    

    现在渲染字段就行了:

    echo $field->toHtml();
    

    【讨论】:

      【解决方案5】:

      OP 的解决方案从问题变成了答案:

      我研究得越多,我就越意识到该系统与 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 . ']',
              ));
      

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2020-05-08
        • 1970-01-01
        • 2012-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多