The module I'm extending adds fields to an admin form using addField(). I copied this behavior as I thought I would try to stick to their setup. However, I can not figure out how to add the "use default" checkbox to the right of any of these fields. This is an issue as I have a field to add that needs to be site-specific.
Code for posterity sake:
$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(),
));
To clarify, I am looking for the dynamic checkbox that gets put by admin fields that changes based on what view you are on. This shows up automatically when creating fields through the XML but seems to be left out when adding fields with addField().
One way you could add a checkbox is
$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>
");
Also did you check the way they do it in their module?
I know this is a bit late, but I just wanted to post my solution to this and perhaps spark some other ideas. (and feedback on my way of working)
My initial source: http://marius-strajeru.blogspot.be/2013/02/create-system-config-section-with.html
So to add a field (eg: text field):
$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)
This is all you need to do to add a checkbox to your fields.
if you wish to add those gray scope texts (eg: [WEBSITE]):
$field['scope'] = true; // Display scope label
$field['scope_label'] = '[WEBSITE]';
This can be done because the basic Varien Object is defined to implement ArrayAccess
class Varien_Object implements ArrayAccess
now to render the field just go:
echo $field->toHtml();
Look in Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Inventory
and also catalog/product/tab/inventory.phtml
This looks promising.
<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>
Well, this is quite an ugly solution, but maybe it will work for you. First of all, on product pages there is a custom renderer for each element, that is why it is shown there. So, if you have the following element:
$name = $fieldset->addField('name', 'text', array(
'name' => 'name',
'required' => true,
'class' => 'required-entry',
'label' => Mage::helper('some_helper')->__('Name'),
));
you will have to render it with a custom renderer:
if ($name)
{
$name->setRenderer(
$this->getLayout()->createBlock('adminhtml/catalog_form_renderer_fieldset_element')
);
}
At this point you should have the third column with the scope-label class. But the checkbox near it still won't show up. For that we have to set the following for the form:
$storeObj = new Varien_Object();
$storeId = $this->getRequest()->getParam("store");
$storeObj->setId($storeId);
$storeObj->setStoreId($storeId);
$form->setDataObject($storeObj);
Now you should see also the checkbox.
This solution is from:
http://code007.wordpress.com/2014/03/20/how-to-show-the-default-checkbox-near-a-magento-attribute/
OP's solution moved from question to an answer:
The more I look into it the more I realize that the system used with
the XML is a pretty in-depth system that would be kind of ridiculous
to replicate all just to stick with some bad programming practices. I
am going to simply add to the XML.
For those wondering how to do it using addField(), I did figure it
out. Here is my final code:
$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 . ']',
));
Related
I need to set an input range on a form to create and update. In the October CMS documentation, I found a list solution, but in the register there is no "range" field.
<input type="range" min="0" max="100" step="1">
I'm using the "Builder Plugin".The closest thing to the solution was the "macros" feature, but the documentation about the feature didn't help much. Has anyone found a solution for creating their own input types or range?
October CMS is very extendable platform. You can extend each and every aspect of it.
Same goes for builder plugin you can extend it as per your needs.
Please hold on this answer will be long but You will find it lot of useful.
Final Results
It will add control to control list so you can easily add it and reuse it for other fields as well.
Configurable - you don't need to edit any file/partial to change its values. its all inside builder plugin. your values [min, max, step] field-name etc.. all you can edit/update from builder plugin.
Its automatic. means labels and field-name all will work like other controls you don't need to specify anything else. all will be dynamic.
So lets start extending builder plugin :)
add this code to your plugin boot method plugin.php, it will basically add the control to builder plugin control list. [1st image]
public function boot() {
\Backend\Widgets\Form::extend(function($widget) {
$widget->addViewPath(\File::symbolizePath('~/plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider/field_partials'));
});
\Event::listen('pages.builder.registerControls', function($controlLibrary) {
$properties = [
'min' => [
'title' => 'Min',
'type' => 'string',
'default' => '0',
'ignoreIfEmpty' => false,
'sortOrder' => 81
],
'max' => [
'title' => 'Max',
'type' => 'string',
'default' => '100',
'ignoreIfEmpty' => false,
'sortOrder' => 82
],
'step' => [
'title' => 'Step',
'type' => 'string',
'default' => '10',
'ignoreIfEmpty' => false,
'sortOrder' => 83,
]
];
$controlLibrary->registerControl(
'my_range',
'Range Field',
'Custom Range Field',
\RainLab\Builder\Classes\ControlLibrary::GROUP_STANDARD,
'icon-arrows-h',
$controlLibrary->getStandardProperties(['stretch'], $properties),
\HardikSatasiya\SoTest\Classes\CustomDesignTimeProvider::class
);
});
.... your extra code ...
now you need to create/add required dependent files plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider.php , plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider/partials/_control-my_range.htm and plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider/field_partials/_field_my_range.htm'
plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider.php
<?php namespace HardikSatasiya\SoTest\Classes;
use File;
use RainLab\Builder\Classes\ControlDesignTimeProviderBase;
class CustomDesignTimeProvider extends ControlDesignTimeProviderBase {
public function renderControlBody($type, $properties, $formBuilder)
{
return $this->makePartial('control-'.$type, [
'properties'=>$properties,
'formBuilder' => $formBuilder
]);
}
public function renderControlStaticBody($type, $properties, $controlConfiguration, $formBuilder)
{
$partialName = 'control-static-'.$type;
$partialPath = $this->getViewPath('_'.$partialName.'.htm');
if (!File::exists($partialPath)) {
return null;
}
return $this->makePartial($partialName, [
'properties'=>$properties,
'controlConfiguration' => $controlConfiguration,
'formBuilder' => $formBuilder
]);
}
public function controlHasLabels($type)
{
return true;
}
}
plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider/partials/_control-my_range.htm
<div class="builder-blueprint-control-text">
<i class="icon-arrows-h"></i> Range Field
</div>
while above steps will add our custom control to the plugin builder list, next step will be adding form field partial. [3rd image]
plugins/hardiksatasiya/sotest/classes/CustomDesignTimeProvider/field_partials/_field_my_range.htm
<!-- Range -->
<?php if ($this->previewMode): ?>
<span class="form-control"><?= $field->value ? e($field->value) : ' ' ?></span>
<?php else: ?>
<div style="display: flex;">
<span style="width: 30px; margin-right: 20px;" id="<?= $field->getId() ?>_val">
<?= $field->value ?>
</span>
<span>
[<?= $field->getConfig('min') ?>]
</span>
<input
type="range"
name="<?= $field->getName() ?>"
id="<?= $field->getId() ?>"
value="<?= e($field->value) ?>"
min="<?= $field->getConfig('min') ?>"
max="<?= $field->getConfig('max') ?>"
step="<?= $field->getConfig('step') ?>"
oninput="(function(input) { document.getElementById('<?= $field->getId() ?>_val').innerText = input.value; })(this)"
<?= $field->getAttributes() ?>
/>
<span>
[<?= $field->getConfig('max') ?>]
</span>
</div>
<?php endif ?>
This html files are just html markup so can edit them and add css/style according to your need.
Once you did following steps you will able to see your custom range control in form builder's control list. now you can add it update it just like any other default control.
Its fully dynamic you can choose field-name, min, max, step and it will be applied.
Note: Just make sure you replace author-name and plugin-name according to your setup in provided code.
if you have any doubts please comment.
Well this was unique and after some research and testing I came up with a way to do this. I will remark that there isn't any reason that I have personally found to justify a range slider in a form. So I understand why OctoberCMS doesn't have one natively.
Inside the builder plugin you need to add the input field of the value you want to be stored and change the setting to be read only.
Inside the builder plugin you will want to add a partial. I called my partial something.htm for testing purposes. The whole path to the partial is: $/dle/test/models/products/something.htm Note the $ is just to evoke the starting point of the search.
Not inside the something.htm partial I have this: The label and input of the range. Natively the range element doesn't show the amount but with javascript and jquery we can connect this range to the price field.
<label for="priceRange">Price Range</label>
<input id="priceRange" type="range" min="0" max="10" step=".25" onchange="updateTextInput(this.value);">
Now you have to go to your create.htm and update.htm pages under controller. IE: author/plugin/controllers/controller/create.htm. Here I have entered the javascript / jquery to connect the range to the input field.
<script>
function updateTextInput(val) {
document.getElementById('Form-field-Products-price').value=val;
}
var value = $('#Form-field-Products-price').val();
$('#priceRange').val(value);
</script>
I'm using CodeIgniter and the MVC architecture to build a form on ExpressionEngine.
My problem is that CodeIgniter doesn't have the input date that I can use with HTML. My code is something like:
$data['cycle_begin'] = form_input('cycle_begin');
$data['cycle_month'] = form_input('cycle_month');
if(isset($_POST['submit']))
{
ee()->db->insert('exp_credit_tracker_assoc',
array(
'cycle_begin' => $_POST['cycle_begin'],
'cycle_months' => $_POST['cycle_month']
)
);
}
The view is something in the line of:
<div class="form-group">
<label for="cycle_type">cycle_type</label><?= $cycle_type ?>
</div>
<div class="form-group">
<label for="cycle_begin">cycle_begin</label><?= $cycle_begin ?>
</div>
<div class="form-group">
<label for="cycle_month">cycle_month</label><?= $cycle_month ?>
</div>
Is there any form_date which I could use or my only option is to change the input in my view file and grab the value in some way in my control file?
No there is no any form_date input type which you could use.
you can do some thing like this :
$data['cycle_begin'] = form_input('date_name',['type' =>'date']);
You can modify form_input as date input like this
<?php
$data = array(
'name' => 'mydate',
'id' => 'mydate',
'value' => '03-04-2012',
'type' => 'date',
'format' =>'m-d-Y'
);
echo form_input($data);
?>
For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date
I'm having problems getting the image field from Advanced Custom Field plugin. I'm using it to set an image for custom taxonomy category named "aktuelni_ponudi_category". I have displayed the name of the category and its link, but can't fix the problem with the image. I also want to create a shortcode, so here is my code:
function my_vc_shortcode( $atts ) {
$categories = get_categories( array(
'taxonomy' => 'aktuelni_ponudi_category',
'hide_empty' => '0',
'order' => 'DESC'
)); ?>
<div class="row">
<?php
foreach($categories as $category) { ?>
<div class="col-md-4">
<a href="<?php echo get_category_link($category->cat_ID); ?>">
<?php echo $category->name; ?>
</a>
<?php echo '<img src="' . the_field('acf_image') . '">'; ?>
</div>
<?php }
?>
</div>
<?php }
add_shortcode( 'my_vc_php_output', 'my_vc_shortcode');
I'm hoping for an answer...
If you use custom fields on a taxonomy term there is a special syntax to get that data: you should call the field function with a parameter composed of the taxonomy name and the term id, like here:
the_field('acf_field','aktuelni_ponudi_category_' . $category->cat_ID);
You can read more about it here: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
I am having a bit of trouble with my form and radio buttons, with input text I do this:
<?php echo form_input('lastname', set_value('lastname'), 'id=lastname'); ?>
<?php echo form_error('lastname'); ?>
and when the validation runs and that input field that is filled out gets the valued returned...what I am looking for is a way to do this with radio buttons
<tr><td><?php echo form_label('Gender: ', 'gender'); ?></td><td><?php echo form_label('Male', 'male') . form_radio('gender', 'M', '', 'id=male'); ?><br><?php echo form_label('Female', 'female') . form_radio('gender', 'F', '', 'id=female'); ?></td><td><?php echo form_error('gender'); ?></td></tr>
as you can see both my radio buttons have values already F or M.....how do I have the button that is selected returned selected?
From the user guide: https://www.codeigniter.com/user_guide/helpers/form_helper.html
form_radio()
This function is identical in all respects to the form_checkbox()
function above except that is sets it as a "radio" type.
So reading further:
form_checkbox()
Lets you generate a checkbox field. Simple example:
echo form_checkbox('newsletter', 'accept', TRUE);
Would produce:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />
The third parameter contains a boolean TRUE/FALSE to determine whether
the box should be checked or not.
So in your case, it might be something like this:
// Pass boolean value to third param
// Example:
$radio_is_checked = $this->input->post('gender') === 'F';
echo form_radio('gender', 'F', $radio_is_checked, 'id=female');
Since set_radio() just returns a string checked="checked", you could wedge it in to the fourth paramter if you really wanted to but it makes for some ugly looking code:
echo form_radio('gender', 'F', NULL, 'id="female" '.set_radio('gender', 'F'));
I use Ternary Operator for this.
Suppose you got return value of "M" (Male). I am making $selected variable for example and assume that it has value of M.
<tr>
<td>
<?php echo form_label('Gender: ', 'gender'); ?>
</td>
<td>
<?php echo form_label('Male', 'male') . form_radio(array('name' => 'gender', 'value' => 'M', 'checked' => ('M' == $selected) ? TRUE : FALSE, 'id' => 'male')); ?><br>
<?php echo form_label('Female', 'female') . form_radio(array('name' => 'gender', 'value' => 'F', 'checked' => ('F' == $selected) ? TRUE : FALSE, 'id' => 'female')); ?>
</td>
<td>
<?php echo form_error('gender'); ?>
</td>
</tr>
This worked well for me. Even though I didnt use form_helper, this was much easier way. Everyone face issue to take input for gender field :)
<input id="gender" name="gender" type="radio" class="" <?php if($gender=='0') echo "checked='checked'"; ?> value="0" <?php echo $this->form_validation->set_radio('gender', 0); ?> />
<label for="gender" class="">Male</label>
<input id="gender" name="gender" type="radio" class="" <?php if($gender=='1') echo "checked='checked'"; ?> value="1" <?php echo $this->form_validation->set_radio('gender', 1); ?> />
<label for="gender" class="">Female</label>
<input id="gender" name="gender" type="radio" class="" <?php if($gender=='2') echo "checked='checked'"; ?> value="2" <?php echo $this->form_validation->set_radio('gender', 2); ?> />
<label for="gender" class="">Others</label>
I hope it will help someone :)
This works forsure....
<tr><td><?php echo form_label('Gender: ', 'gender'); ?></td><td><?php echo form_label('Male', 'male') . form_radio(array("name"=>"gender","id"=>"male","value"=>"M", 'checked'=>set_radio('gender', 'M', FALSE))); ?><br><?php echo form_label('Female', 'female') . form_radio(array("name"=>"gender","id"=>"female","value"=>"F", 'checked'=>set_radio('gender', 'F', FALSE))); ?></td><td><?php echo form_error('gender'); ?></td></tr>
wrote my radio buttons like so
<?php form_radio(array("name"=>"gender","id"=>"female","value"=>"F", 'checked'=>set_radio('gender', 'F', FALSE))); ?>
$_fldProyectoMostrar = array(
"id" => "_FLDPROYDER",
"name" => "_FLDPROYDER"
);
<?php echo form_radio($_fldProyectoMostrar, "FALSE", set_radio("_FLDPROYDER", "FALSE", TRUE)); ?>NO
<?php echo form_radio($_fldProyectoMostrar, "TRUE", set_radio("_FLDPROYDER", "TRUE", FALSE )); ?>SI
Donde el el segundo valor del set radio, debe ser = al string value del form_radio
My form validation isn’t using the rules in a config file in CodeIgniter 2. I’m accessing the form at /admin/people/add and /admin/people/edit/id and submitting to /admin/people/save. When I submit the add form it just reloads add without reporting any validation errors (my form views will display validation errors if $this->form_validation->_error_array is not empty; this is working on my login form). When I submit the edit form I get a 404 error at /admin/people/save even though that URI works when I initially go to the edit page. I'm loading the form validation library in the constructor.
application/config/form_validation.php:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'people/save' => array(
array(
'field' => 'first_name',
'label' => 'first name',
'rules' => 'trim|required'
)
) // people/save
);
/* End of file form_validation.php */
/* Location: /application/config/form_validation.php */
application/controllers/admin/people.php:
public function add() {
$fields = $this->db->list_fields('people');
/* set_defaults makes an object with null values for the fields in the database */
$person = $this->form_validation->set_defaults($fields);
$data = array(
'action' => 'add',
'person' => $person,
'button_text' => 'Add Person'
);
$data['page_type'] = 'form';
$this->layouts->set_title('Add a Person');
$this->layouts->view('people/add_edit_person_form',$data);
} // add
public function save(){
if($this->form_validation->run('people/save') == FALSE){
if(is_numeric($this->input->post('person_id'))){
$this->edit();
} else {
$this->add();
}
} else {
redirect('people'); // test to see if it's passing validation
}
} // save
application/views/add_edit_person_form.php:
<?php
$attributes = array(
'class' => 'block',
'id' => 'add_edit_person'
);
echo form_open('admin/people/save');
?>
<div class="required<?php echo form_error('first_name')?' error':'';?>">
<label for="first_name">First name:</label>
<input name="first_name" id="first_name" type="text" value="<?php echo set_value('first_name',$person->first_name); ?>" size="75" maxlength="255" />
</div>
<input name="person_id" id="person_id" type="hidden" value="<?php echo set_value('id',$person->id); ?>" />
<button><?php echo $button_text; ?></button>
</form>
After you've defined your $config array in application/config/form_validation.php, you'll need to call the following function in order to set the rules:
$this->form_validation->set_rules($config);
Reference: Setting Rules Using an Array