I have a admin back-end options section/module to control few things like upload slider images, site background, banners etc.
That is simple cause it just requires xml code in system.xml.
I want to add different drop-downs, radio buttons, multiple select.
I have the following code in Options.php
Its giving me options to select one, two or three for every drop-down, radio or multi select.
What I have to define here to get different options? not just one, two and Three.
<?php
class Options_CustomConfig_Model_Options
{
/**
* Provide available options as a value/label array
*
* #return array
*/
public function toOptionArray()
{
return array(
array('value'=>1, 'label'=>'One'),
array('value'=>2, 'label'=>'Two'),
array('value'=>3, 'label'=>'Three')
);
}
}
You can create individual models for individual drop down and specify relevant model in the system.xml while creating field.
Hope this will help you.
You need to create Different Source model if options are different in configuration field.
here is the some useful links for creating source model:
http://magento.ikantam.com/qa/source-model
http://www.atwix.com/magento/frontend-backend-source/
Ok so this is what needs to be done to get more options: I created another folder in Model called Optiontwo and inside Option.php. Class for options.php have to have folded in it like this: class CustomConfig_Model_Optiontwo_Options. And then in system.xml for source_model have to be declared like this: customconfig/Optiontwo_options
Thanks to Adrsh Khatri for pointing in the right direction.
Related
On SS 4.0.3 following this guide and the official doc, I successfully created a custom permission role.
Now I would create a custom group and add the default admin to it, as action performed by default in order to maintain those user/group/role settings either if the DB being dropped. I googled around many times but I didn't found any detailed tutorial to achieve this (using the Group class, the right place to implement this logic and so on).
Could anyone show me the way?
Thanks in advance.
If you want to enforce a data structure you can use DataObject::requireDefaultRecords. See Group::requireDefaultRecords for an example of this, it runs on dev/build every time.
You will want to check whether the data you're creating exists before doing so though, to ensure you don't create the group every time.
I successfully setted up the panorama described on my question, thanks to #robbie and other sources that I combined all in one, in order to achieve my goal (I'll list them here below). I wanna share my approach to those could be face this logic in the future.
In the first place I created a brand new standard/global permission with providePermissions() in the PageController (see balbuss.com source).
Next, as #robbie suggested, I created a Group DataExtension in order to build up a new group and setted my previously created permission on it (see #Barry's solution source).
To add the default admin to this group, I had to create a new Permission DataExtension in which I assigned the group to the proper member (see #StefGuev suggestion source):
// Definizione Namespace
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\DefaultAdminService;
use SilverStripe\Security\Member;
use SilverStripe\Security\Group;
class PermessoExtension extends DataExtension
{
/**
* Metodo gestione inizializzazione records di default
* Setter
* #return void
*/
public function requireDefaultRecords()
{
parent::requireDefaultRecords();
$userAdmin = DefaultAdminService::getDefaultAdminUsername();
$admin = Member::get()->filter('Email', $userAdmin)->first();
$gruppo = Group::get()->filter('Code', 'negozianti')->first();
// Controllo gruppo
if (!$admin->inGroup($gruppo->ID)) {
$admin->Groups()->add($gruppo);
$admin->write();
}
}
}
I made this choice because during dev/build, Permission being built after Member table, so I could get the default admin without errors.
Special thanks to these sources:
balbuss.com
Bereusei's solution
Barry's solution
StefGuev suggestion
I recently started a project using Symfony, and as a part of it, there is a Sonata (modeled) backoffice. I love the way Sonata make that lists of items where you can search / filter or wathever.. So, the question is:
Can I reuse this lists as a popup for example to select a user of my DB?
The example is easy:
I have a user table on my DB
I have a form where you can select a user in one input but, there is 10.000 in the DB so a select is not an option.
I'd love to use the user list of my sonata project in a popup and when the client click on a user of the list I would put it on the base form.
Is that possible? Is there a way for that?
Yes, you can do that using sonata_type_model_list as your field type.
You can see some examples of it here
Note that for a large dataset this is pretty useless unless you add some filters to it as well, otherwise it's just a huge paginated list.
Finally I did it creating new Admins with this fields inside:
protected $baseRouteName = 'admin_product_clean_list';
protected $baseRoutePattern = 'product-clean';
I also override the getTemplate method to use my clean custom layout like this:
public function getTemplate($name)
{
switch ($name) {
case 'layout':
return 'DemoBundle:admin:onlytable_layout.html.twig';
break;
default:
return parent::getTemplate($name);
break;
}
}
An then just use it with URLs like this:
"{{ app.request.getBaseURL() }}/admin/product-clean/list"
Not sure if it is the best way but it works like a charm.. I can use my custom layout copied of sonata's project (vendor/sonata-project/admin-bundle/Resources/views/standard_layout.html.twig) and remove or add whatever I want. You can also use the sonata empty or ajax layouts instead.
In Yii, I have a view belonging to class A, and in a view corresponding to this class, I want to add a form to create a model of another class.
So, in protected/views/pictures/myview.php, I have:
<?php
/* #var $this PicturesController */
/* #var $model Pictures */
$objectForm = new Objects();
$newForm = ObjectsController::renderPartial('create',array('model'=>$objectForm),true);
?>
And I am trying to add a form to render protected/views/objects/create.php. But the above code doesn't work since the view is still trying to load the create form from the Pictures class. Since I am obtaining errors saying that same properties of Objects are not defined, because the system is loading the Pictures create form.
How can I add the create form of the Objects model?
Note: I added "applications.controllers.*" to the import array in main.php, but I understand this is a bad practice. Is there any possible solutions that doesn't involve me to do this?
Thanks.
In this case you just needed to link the correct view in the renderPartial:
$objectForm = new Objects();
$newForm = ObjectsController::renderPartial('/objects/create',array('model'=>$objectForm),true); // "/objects/create", and not "create".
Those errors were giving because Yii understood you were rendering to the create view of pictures, and you gave it the model of Objects (rather than the model of Pictures).
If we were talking about a render call in the controller, and you want to render a view from another action that belongs to another controller, you do not render to this view directly. You have to "redirect" to the action of this other controller (that belongs to the other class).
try like this this
$this->renderPartial('application.staff.views.default.create',array());
this is the format
$this->renderPartial('context_text.views.display._display_text');
I have a custom multi select attribute which I'd like to take part in filtering of products. The attribute is set as used in Layered Navigation however doesn't appear in the list of available filters. Could be due to custom model implementation?
Anyone have some tips where to check why it doesn't appear? Attribute is set for several products
Magento version used is EE 1.11
Thanks
For those who will struggle with this in the future: the problem is in Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source file on line 191. By default multi select attribute values are being pulled from eav_attribute_option and if your custom attribute uses custom source model the attribute will not be indexed.
I don't know as of yet if it's intended but I couldn't find a better solution than overriding that model in local pull and adding required values in $options array.
Hope this helps someone, someday
What is the backend_type. i.e. are the values stored in the catalog_product_entity_varchar or catalog_product_entity_text table?
The backend_type has to match the checks in Mage_Catalog_Model_Resource_Eav_Attribute::isIndexable(), so text wouldn't work without rewriting the attribute model.
Is the is_filterable and/or is_filterable_in_search attribute property set?
The Mage_Catalog_Model_Product_Indexer_Eav::_registerCatalogAttributeSaveEvent() checks for those when updating the index for the layered navigation.
Are the methods getFlatColums(), getFlatIndexes() and getFlatUpdateSelect() implemented in the custom source model?
This actually is only required for building and updating the flat catalog product tables (so the used_in_product_listing or is_filterable property needs to be set in order for Magento to pick up the attribute).
Check the class Mage_Eav_Model_Entity_Attribute_Source_Table as a reference on what these there methods are supposed to return.
NOTE: I'm adding this in a new answer to use the code format.
How it was said, the problem is with multiselect attributes using a custom source model.
Solution:
Rewrite the class
Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source
Override the method:
_prepareMultiselectIndex
add this code after the $options array is filled with the default code (check line 200 in original file)
foreach($attrIds as $attId){
if( ! isset($options[$attId])){
$options[$attId] = $this->_getOptionsFromSourceModel($attId);
}
}
add this method too:
protected function _getOptionsFromSourceModel($attId)
{
$options = array();
/** #var Mage_Eav_Model_Entity_Attribute_Abstract $attribute */
$attribute = Mage::getResourceSingleton('catalog/product')->getAttribute($attId);
/** #var Mage_Eav_Model_Entity_Attribute_Source_Abstract $source */
$source = $attribute->getSource();
$sourceOptions = $source->getAllOptions();
if($sourceOptions){
foreach($sourceOptions as $sourceOption){
if(isset($sourceOption['value'])){
$options[$sourceOption['value']] = true;
}
}
}
return $options;
}
I couldn't find a less intrusive way to fix this.
I just wandering on how I can get the configuration data for my custom module. The configuration can be set from the admin system->configuration and how to pull it in frontend?
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName');
sectionName, groupName and fieldName are present in etc/system.xml file of your module.
The above code will automatically fetch config value of currently viewed store.
If you want to fetch config value of any other store than the currently viewed store then you can specify store ID as the second parameter to the getStoreConfig function as below:
$store = Mage::app()->getStore(); // store info
$configValue = Mage::getStoreConfig('sectionName/groupName/fieldName', $store);
you should you use following code
$configValue = Mage::getStoreConfig(
'sectionName/groupName/fieldName',
Mage::app()->getStore()
);
Mage::app()->getStore() this will add store code in fetch values so that you can get correct configuration values for current store this will avoid incorrect store's values because magento is also use for multiple store/views so must add store code to fetch anything in magento.
if we have more then one store or multiple views configured then this will insure that we are getting values for current store
Magento 1.x
(magento 2 example provided below)
sectionName, groupName and fieldName are present in etc/system.xml file of the module.
PHP Syntax:
Mage::getStoreConfig('sectionName/groupName/fieldName');
From within an editor in the admin, such as the content of a CMS Page or Static Block; the description/short description of a Catalog Category, Catalog Product, etc.
{{config path="sectionName/groupName/fieldName"}}
For the "Within an editor" approach to work, the field value must be passed through a filter for the {{ ... }} contents to be parsed out. Out of the box, Magento will do this for Category and Product descriptions, as well as CMS Pages and Static Blocks. However, if you are outputting the content within your own custom view script and want these variables to be parsed out, you can do so like this:
<?php
$example = Mage::getModel('identifier/name')->load(1);
$filter = Mage::getModel('cms/template_filter');
echo $filter->filter($example->getData('field'));
?>
Replacing identifier/name with the a appropriate values for the model you are loading, and field with the name of the attribute you want to output, which may contain {{ ... }} occurrences that need to be parsed out.
Magento 2.x
From any Block class that extends \Magento\Framework\View\Element\AbstractBlock
$this->_scopeConfig->getValue('sectionName/groupName/fieldName');
Any other PHP class:
If the class (and none of it's parent's) does not inject \Magento\Framework\App\Config\ScopeConfigInterface via the constructor, you'll have to add it to your class.
// ... Remaining class definition above...
/**
* #var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* Constructor
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
// ...any other injected classes the class depends on...
) {
$this->_scopeConfig = $scopeConfig;
// Remaining constructor logic...
}
// ...remaining class definition below...
Once you have injected it into your class, you can now fetch store configuration values with the same syntax example given above for block classes.
Note that after modifying any class's __construct() parameter list, you may have to clear your generated classes as well as dependency injection directory: var/generation & var/di
for example if you want to get EMAIL ADDRESS from config->store email addresses.
You can specify from wich store you will want the address:
$store=Mage::app()->getStore()->getStoreId();
/* Sender Name */
Mage::getStoreConfig('trans_email/ident_general/name',$store);
/* Sender Email */
Mage::getStoreConfig('trans_email/ident_general/email',$store);