This should be relatively simple. I know how to create a form and hide it and show it if a specific option is selected
I want to have a Dropdown populated with a specific DataObject. That much is simple. But I need to add an option that says 'Add new' to this Dropdown, which is in a form
I can handle the submission (like if the value is something like 'new' instead of an ID), but I do not know how to add this option into the select with Silverstripe, or if it is even possible. Any help is appreciated!
You can use a DropdownField and populate it with whatever key value pairs you want. For example:
$itemsField = DropdownField::create(
'Items',
'Items',
['add-new' => 'Add new'] + $this->Items()->map()->toArray()
);
The third argument is an array that gets turned into the option html elements for the drop down.
Then you can add it to your form's FieldList:
$form = Form::create(
$this,
'MyForm',
FieldList::create(
[
$someField,
$someOtherField,
$itemsField
]
),
$actions,
$requiredFields
);
http://api.silverstripe.org/3.1/class-DropdownField.html
A Dropdown for selecting a $has_one is fine. E.g. if you have a list of Addresses and want to select the Country.
Sometimes a new Country has to be added. For this i used the quickaddnew extension which adds a nice button "add new" beneath the dropdown and opens a form in a modal for adding the new item. You can use it in your code like this:
public function getCMSFields() {
/**
* #FieldList
*/
$fields = parent::getCMSFields();
$countries = function() {
return Country::get()->map('ID','Title')->toArray();
};
$country = new DropdownField('CountryID', 'Country', $countries());
$country->setEmptyString('-- please select --');
$country->useAddNew('Country', $countries);
$fields->insertAfter($country, 'Telephone');
$fields->removeByName('SortOrder');
return $fields;
Related
I would like to ask how to refresh Table and Grid components with an updated set of database conditions. Lets say we have a filter form with 'customer name' and we would like to filter and refresh Table or Grid with an other customer name triggered by a button.
$grid = Grid::addTo($app);
$model = new Model_Db($db, ['table' => 'saleheader']);
$grid->setModel($model);
$grid->menu->addItem('FilterGrid', function ($grid,$model) {
filterGrid($grid,$model,'acme');
});
.
function filterGrid($table,$model,$value)
{
$model->addCondition('customer', 'like' ,'%'.$value.'%');
Thank you
For a standard Varchar (or Text) db field I'd like to replace this with a set of fixed options - but still allow "other" to be selected and for the user to enter a string of their choice.
class MyDataObject extends DataObject {
static $db = array(
'MyVarchar' => "Varchar",
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->replaceField(
'MyVarchar',
SomeField(
'MyVarchar',
'Choose an option or enter text',
array(1 => 'Option1',2 => 'Option2')
)
);
return $fields;
}
}
So I'm looking for something to use as SomeField and the user will see a drop down with the values Option1, Option2 and Other. If Option1 or 2 is selected then this is saved into MyVarchar. If Other is selected then it would allow specifying the text directly of MyVarchar as normal.
I'm hopeful there is, as in the case of other answers I've received, a pre-existing module for this.
And...
I've found basically what I was after...
https://github.com/Leapfrognz/alternative-field
Thank you Leapfrognz!
I used the below code to create two radio buttons(approved messages and rejected messages) in Yii framework
<?php echo CHtml::activeRadioButtonList($model, 'Approved', array('Approved Messages', 'Rejected Messages'), array('labelOptions'=>array('style'=>'display:inline'),'separator'=>'')) ?>
Now I have to filter and display all the rows in CGridView of the table where column 'approved' has value=1 when I click on "approved messages" radio button and all the rows in CGridView of the table where column 'Approved' has value=0 when I click on "rejected messages" radio button. How can I do this
I used a drop down for this, the values are Yes and No. Just translate the approved column into text using the following code:
array(
'name' => 'approved',
'value' => '($data->approved ? "Yes" : "No")',
'filter' = >CHtml::dropDownList('Approved', '',
array(
' '=>'All',
'1'=>'On',
'0'=>'Off',
)
),
)
This link is where I got this info: http://www.yiiframework.com/forum/index.php/topic/30694-cgridview-filter-dropdown-from-array/
I googled using cgridview filter example
Alright, lets put in the radio buttons instead of all the dropdowns haha.
I assume you have your view set up something like this:
// view/index.php (or similar)
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>Message::model(),
'columns'=>
[
'id',
'username',
'email:email',
'approved'=>[
'name'=>'approved',
'filter'=>$this->approvedFilter(),
// I like moving stuff like this out of the way.
// But maybe it's smarter to put it in your model instead?
]
]
));
Next for the controller.
// MessageController.php (or similar)
public function actionIndex()
{
$model = Message::model();
// All we need to do is to assign the incoming value to the model we are using...
if ( isset( $_GET['Message']['Approved'] )){
$model->approved = $_GET['Message']['Approved'];
}
$this->render('index', ['model'=>$model]);
}
// Oh yeah the filter. I just copied your code.
public function approvedFilter(){
return CHtml::activeRadioButtonList(
Message::model(), 'approved', array(0,1),
array(
'labelOptions'=>array('style'=>'display:inline'),
'separator'=>''
)
);
}
This code has been tested, but I made some last minute changes, so sorry if it blows up!
And I still think a simple 'approved:boolean' is much cleaner. ;)
Does anyone know how can I add a custom product attribute with a widget renderer?
You can see this in Promo rules if you select SKU you'll got an Ajax popup with product selection.
so how would I go about it?
in :
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY...
In other words, how can I use a widget to select custom attribute values?
EDIT:
The scenario is as follows:
I would like to create a product attribute that will, upon a button click, open a product selection widget.
After the selection, the selected SKU's will go in in a comma delimited format.
This behavior can be seen in the catalog and shopping cart price rules.
If you filter the rule by SKU (SKU attribute must be enabled to "apply to rules"), you'll get a field and a button that will open the product selection widget.
Here is some thoughts that should get you going on the right track:
First, in a setup script, create your entity:
$installer->addAttribute('catalog_product', 'frontend_display', array(
'label' => 'Display Test',
'type' => 'varchar',
'frontend_model' => 'Test_Module/Entity_Attribute_Frontend_CsvExport',
'input' => 'select',
'required' => 0,
'user_defined' => false,
'group' => 'General'
));
Make sure to set the frontend_model to the model that you are going to use. The frontend model affects the display of the attribute (both in the frontend and the adminhtml sections).
Next, create yourself the class, and override one or both of the following functions:
public function getInputType()
{
return parent::getInputType();
}
public function getInputRendererClass()
{
return "Test_Module_Block_Adminhtml_Entity_Renderer_CsvExport";
}
The first (getInputType()) is used to change the input type to a baked in input type (see Varien_Data_Form_Element_* for the options). However, to set your own renderer class, use the latter function - getInputRendererClass(). That is what I am going to demonstrate below:
public function getElementHtml()
{
return Mage::app()->getLayout()->createBlock('Test_Module/Adminhtml_ExportCsv', 'export')->toHtml();
}
Here, to clean things up, I am instantiating another block, as the element itself doesn't have the extra functions to display buttons and the like.
Then finally, create this file:
class Test_Module_Block_Adminhtml_ExportCsv extends Mage_Adminhtml_Block_Widget
{
protected function _prepareLayout()
{
$button = $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => $this->__('Generate CSV'),
'onclick' => '',
'class' => 'ajax',
));
$this->setChild('generate', $button);
}
protected function _toHtml()
{
return $this->getChildHtml();
}
}
This doesn't cover the AJAX part, but will get you very close to getting the rest to work.
Background: In Drupal 7, I have created a form with CCK (aka the Field UI). I used the Field group module to create a fieldgroup, but I need it to be conditional, meaning it will only display depending on a previous answer.
Previous research: To create a conditional field, you can use hook_form_alter() to edit the #states attribute like so:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'person_info_node_form') {
// Display 'field_maiden_name' only if married
$form['field_maiden_name']['#states'] = array(
'visible' => array(
':input[name="field_married[und]"]' => array('value' => 'Yes'),
),
);
}
}
However, there seems to be no way to use the States API for fieldgroups. One thing to note is that, while fields are stored in $form, fieldgroups are stored in $form['#groups'] as well as in $form['#fieldgroups']. I don't know how to distinguish between these, and with this in mind, I have tried to apply a #states attribute to a fieldgroup in the same manner as above. However, it only produces server errors.
Question: Is there a way to make a fieldgroup display conditionally using the States API or some alternative approach?
you have to use the hook_field_group_build_pre_render_alter()
Simply :
function your_module_field_group_build_pre_render_alter(&$element) {
$element['your_field_group']['#states'] = array(
'visible' => array(
':input[name="field_checkbox"]' => array('checked' => TRUE),
),
);
}
This works perfecly. If the group A is in an another group, do this
$element['groupA']['groupB']['#states'] etc....
You may need to add an id attribute if none exists:
$element['your_field_group']['#attributes']['id'] = 'some-id';
$element['yout_field_group']['#id'] = 'some-id';
Here's the simplest solution I came up with. There are essentially 2 parts to this: (1.) programmatically alter the display of the form, and (2.) use the GUI to alter the display of the content.
(1.) First, I used hook_form_alter() to programmatically create the conditional fieldset and add existing fields to it. The code is shown below.
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'FORM_ID_node_form') {
// programmatically create a conditional fieldset
$form['MYFIELDSET'] = array( // do NOT name the same as a 'Field group' fieldset or problems will occur
'#type' => 'fieldset',
'#title' => t('Conditional fieldset'),
'#weight' => intval($form['field_PARENT']['#weight'])+1, // put this fieldset right after it's "parent" field
'#states' => array(
'visible' => array(
':input[name="field_PARENT[und]"]' => array('value' => 'Yes'), // only show if field_PARENT == 'Yes'
),
),
);
// add existing fields (created with the Field UI) to the
// conditional fieldset
$fields = array('field_MYFIELD1', 'field_MYFIELD2', 'field_MYFIELD3');
$form = MYMODULE_addToFieldset($form, 'MYFIELDSET', $fields);
}
}
/**
* Adds existing fields to the specified fieldset.
*
* #param array $form Nested array of form elements that comprise the form.
* #param string $fieldset The machine name of the fieldset.
* #param array $fields An array of the machine names of all fields to
* be included in the fieldset.
* #return array $form The updated form.
*/
function MYMODULE_addToFieldSet($form, $fieldset, $fields) {
foreach($fields as $field) {
$form[$fieldset][$field] = $form[$field]; // copy existing field into fieldset
unset($form[$field]); // destroy the original field or duplication will occur
}
return $form;
}
(2.) Then I used the Field group module to alter the display of the content. I did this by going to my content type and using the 'Manage display' tab to create a field group and add my fields to it. This way, the fields will appear to be apart of the same group on both the form and the saved content.
Maybe you can try to look at the code of this module to help you find an idea.