4

Can anyone explain what is a source Model in Magento.

various time it was asked by interviewer ...but it was confusing for me...can any body explain..... I need to know deeply about source model.. in Magento and its task..... I know about resource model which is responsible for collection and model for basic logic...but source model I don not know.....pls help

8

A source model is the source of possible values for dropdown or multiselect form fields. Each value is a pair of actual value and displayed label. Source models are used for, but not limited to:

  • system configuration values
  • attribute values
  • grid column filters
  • custom Varien_Form instances

There are a few methods a source model should implement, depending on the context. Magento 1 was not very keen on interfaces, so this is basically just a halfway consistent convention1. If you want to create a generic source model that can be used anywhere, make sure to implement them all:

  • toOptionArray() returns an array of arrays, each must have the keys label and value. You can nest the arrays one level deeper to define <optgroup> groups
  • toOptionHash() returns an associative array in the form value => label
  • getAllOptions($withEmpty) same as toOptionArray(), but you can specify if an empty value with 'Please choose...' label is added.
  • getOptionText($value) returns the label for a given value

1) There is Mage_Eav_Model_Entity_Attribute_Source_Interface but it is only sufficient for EAV attributes and not type hinted anywhere.

| improve this answer | |
  • How can I override/rewrite the source models? e.g. Mage_Paygate_Model_Authorizenet_Source_PaymentAction. I'm not able to do it like other simple models. – saiid Oct 21 '16 at 11:41
  • But it is a normal model that should be referenced by its alias, so there is nothing special regarding rewriting it. – Fabian Schmengler Oct 21 '16 at 12:40
1

Basically its just a list of options.

Source Model – a model class that serves to get existing values (stored in the db or somewhere else) for further displaying inside the setting’s field.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.