Book Image

Magento 2 Development Cookbook

Book Image

Magento 2 Development Cookbook

Overview of this book

With the challenges of growing an online business, Magento 2 is an open source e-commerce platform with innumerable functionalities that gives you the freedom to make on-the-fly decisions. It allows you to customize multiple levels of security permissions and enhance the look and feel of your website, and thus gives you a personalized experience in promoting your business.
Table of Contents (18 chapters)
Magento 2 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with source models


Magento works with a lot of dropdown fields that you can select in the forms of the application. We can see dropdowns in the configuration, product, customer, and many more pages.

Magento has a system to set the options of the dropdown and multiselect fields. Magento uses a model that returns the values and labels to render the options of a dropdown or multiselect field. These models are called source models.

In this recipe, we will see which source models Magento uses and how we can create a custom source model for a custom configuration field.

Getting ready

In this recipe, we will extend the Packt_HelloWorld module that we created in the previous recipes. Make sure you have the right version installed for this recipe.

How to do it...

The following steps describe how you can create your custom source models for your custom form fields:

  1. First we will create an extra field in the system configuration to run some tests. The following code adds a new field to the HelloWorld...