1

I would like to set default store value to empty value or 0 in stores dropdown list near field "Send Welcome Email From", in customer creation page.

Send Welcome Email From I like also to receive an error when user keeps the empty value and when he don't choose a store value.

In cutomer_form.xml in module-customer, i found

<field name="sendemail_store_id" formElement="select">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">customer</item>
            </item>
        </argument>
        <settings>
            <dataType>number</dataType>
            <label translate="true">Send Welcome Email From</label>
            <imports>
                <link name="value">${ $.provider }:data.customer.store_id</link>
            </imports>
        </settings>
        <formElements>
            <select>
                <settings>
                    <options class="Magento\Store\Model\System\Store"/>
                </settings>
            </select>
        </formElements>
    </field>

And in /vendor/magento/module-customer/Model/Customer/Attribute/Source/Store.php i found this function getAllOptions, is there the correct function to change it?

 public function getAllOptions($withEmpty = true, $defaultValues = false)
 {
    if (!$this->_options) {
        $collection = $this->_createStoresCollection();
        if ('store_id' == $this->getAttribute()->getAttributeCode()) {
            $collection->setWithoutDefaultFilter();
        }
        $this->_options = $this->_store->getStoreValuesForForm();
        if ('created_in' == $this->getAttribute()->getAttributeCode()) {
            array_unshift($this->_options, ['value' => '0', 'label' => __('Admin')]);
        }
    }
    return $this->_options;
}

EDIT:

Following this

<options class="Magento\Store\Model\System\Store"/>

I searched in class /vendor/magento/module-store/Model/System/Store.php, and found this function

public function getStoreValuesForForm($empty = false, $all = false)
{

    $options = [];
    if ($empty) {
        $options[] = ['label' => '', 'value' => ''];
    }
    ***$options[] = ['label' => '', 'value' => ''];*** // I added this, but how to set it as default?

    if ($all && $this->_isAdminScopeAllowed) {
        $options[] = ['label' => __('All Store Views'), 'value' => 0];
    }

    $nonEscapableNbspChar = html_entity_decode('&#160;', ENT_NOQUOTES, 'UTF-8');

    foreach ($this->_websiteCollection as $website) {
        $websiteShow = false;
        foreach ($this->_groupCollection as $group) {
            if ($website->getId() != $group->getWebsiteId()) {
                continue;
            }
            $groupShow = false;
            foreach ($this->_storeCollection as $store) {
                if ($group->getId() != $store->getGroupId()) {
                    continue;
                }
                if (!$websiteShow) {
                    $options[] = ['label' => $website->getName(), 'value' => []];
                    $websiteShow = true;
                }
                if (!$groupShow) {
                    $groupShow = true;
                    $values = [];
                }
                $values[] = [
                    'label' => str_repeat($nonEscapableNbspChar, 4) . $store->getName(),
                    'value' => $store->getId(),
                ];
            }
            if ($groupShow) {
                $options[] = [
                    'label' => str_repeat($nonEscapableNbspChar, 4) . $group->getName(),
                    'value' => $values,
                ];
            }
        }
    }

    return $options;
}

I added this

$options[] = ['label' => '', 'value' => ''];

The empty value is added but how to set it as default?

Please help.

Your Answer

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

Browse other questions tagged or ask your own question.