It's impressively simple when used inside a JS file loaded via Require JS (All/most your scripts should be so I won't go into detail about that):
$('body').trigger('processStart');
And to remove it
$('body').trigger('processStop');
Result:
You have to keep below code inside your UpgradeData.php
file,
<?php
/**
* Copyright © 2015 Clounce. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace {Vendor}\{Module}\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Model\Customer;
class UpgradeData implements UpgradeDataInterface
{
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* Installs data for a module
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$attributes = [
'test'=> [
"type" => "varchar",
"label" => "Your custom label",
"input" => "text",
'sort_order' => 100,
"required" => false,
'position' => 100,
'system' => false,
//'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
],
];
foreach ($attributes as $code => $options) {
$eavSetup->addAttribute(
Customer::ENTITY,
$code,
$options
);
}
$this->installCustomerForms($eavSetup);
}
/**
* Add customer attributes to customer forms
*/
public function installCustomerForms(EavSetup $eavSetup)
{
$customer = (int)$eavSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
/**
* @var ModuleDataSetupInterface $setup
*/
$setup = $eavSetup->getSetup();
$attributeIds = [];
$select = $setup->getConnection()->select()->from(
['ea' => $setup->getTable('eav_attribute')],
['entity_type_id', 'attribute_code', 'attribute_id']
)->where(
'ea.entity_type_id IN(?)',
[$customer]
);
foreach ($eavSetup->getSetup()->getConnection()->fetchAll($select) as $row) {
$attributeIds[$row['entity_type_id']][$row['attribute_code']] = $row['attribute_id'];
}
$data = [];
$attributes = [
'test'=> [
"type" => "varchar",
"label" => "Your custom label",
"input" => "text",
'sort_order' => 100,
"required" => false,
'position' => 100,
'system' => false,
//'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
],
];
foreach ($attributes as $attributeCode => $attribute) {
$attributeId = $attributeIds[$customer][$attributeCode];
$attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
$attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
if ($attribute['system'] != true || $attribute['visible'] != false) {
$usedInForms = ['customer_account_create','adminhtml_customer','checkout_register','customer_account_edit','adminhtml_checkout'];
foreach ($usedInForms as $formCode) {
$data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
}
}
}
if ($data) {
$setup->getConnection()->insertOnDuplicate($setup->getTable('customer_form_attribute'), $data);
}
}
}
Remove var folder from root and run command,
php bin/magento setup:upgrade
and
php bin/magento indexer:reindex
Check again. Its working.
Best Answer
Apparently, I have no direct answer to your question as it seems to be an issue with Magento (see Issue #5132 of Magento2 on GitHub).
Workaround
Just utf8_decode your variables before echoing them. For example: