VirtualGroupedSelect ==================== * Published: 2019-07-25 * Author: Nickolas Burr .. contents:: Table of Contents :local: Related ------- * :doc:`DataObjectFactory` * :doc:`VirtualSelect` Description ----------- The `VirtualSelect `_ example illustrates how to create a ``select``, ``multiselect`` source model with data populated from ``di.xml``. That works well for flat arrays, but doesn't work for multidimensional arrays. In order to populate a ``select`` or ``multiselect`` in the same fashion, but with a multidimensional array, we'll need to adjust the ``setOption`` method to recurse through the given subarrays and create the desired ``optgroup`` set. Usage ----- .. code-block:: xml
separator-top vendor Vendor\Package\Model\Source\Select\Grouped\ProductTypes
Source ------ .. code-block:: php $value) { if (is_array($value)) { $optgroup[] = $this->getOptGroup($value); } else { $optgroup[] = [ 'label' => $value, 'value' => $key, ]; } } return $optgroup; } /** * @param int|string|null $value * @param int|string $key * @return void */ protected function setOption($value, $key): void { if (is_array($value)) { $value = $this->getOptGroup($value); } $this->options[] = [ 'label' => __($key), 'value' => $value, ]; } /** * @return array */ public function toOptionArray() { return $this->options; } } .. code-block:: xml T-shirt Long Sleeve Shirt Slacks Track Pants Loafers