VirtualSelect ============= * Published: 2019-07-25 * Author: Nickolas Burr .. contents:: Table of Contents :local: Related ------- * :doc:`DataObjectFactory` * :doc:`VirtualGroupedSelect` Description ----------- The use of dependency injection in Magento makes it especially easy to work with data from ``di.xml``. However, it is still so commonplace to see hardcoded data in backend ``source_model`` classes, which amounts to a bunch of duplicated classes containing disparate data. Unifying the data in ``di.xml`` makes it easier to maintain and update throughout the lifetime of the module. An elegant approach to creating select ``source_model`` classes with populated data is to create a generic class that implements ``ArrayInterface``, and use the power of the ```` to set the options dynamically. Usage ----- .. code-block:: xml
separator-top vendor Vendor\Package\Model\Source\Select\Status
Source ------ .. code-block:: php options[] = [ 'label' => __($key), 'value' => $value, ]; } /** * @return array */ public function toOptionArray() { return $this->options; } } .. code-block:: xml Pending Closed Open On Hold