I tried display customer attribute on frontend from admin. But I dont know how to display it, because attribute is type "select" and when I got value of it I got value of option of source model.
My layout ArseniyInk/TaskTwo/view/frontend/layout/customer_account_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Customer\Block\Account\Dashboard\Info" name="customer_account_dashboard_info" as="info" template="Magento_Customer::account/dashboard/info.phtml" cacheable="false">
<container name="customer.account.dashboard.info.extra">
<block class="Magento\Customer\Block\Account\Dashboard\Info" name="customer.account.dashboard.info.extra.magento.username" template="ArseniyInk_TaskTwo::account/dashboard/info.phtml"/>
</container>
</block>
</referenceContainer>
</body>
My Source Model ArseniyInk/TaskTwo/Model/Customer/Attribute/Source/Employment.php
<?php
namespace ArseniyInk\TaskTwo\Model\Customer\Attribute\Source;
class Employment extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [
['label' => __('Unemployment'), 'value' => '1'],
['label' => __('Pupil'), 'value' => '2'],
['label' => __('Student'), 'value' => '3'],
['label' => __('Employee'), 'value' => '4']
];
}
return $this->_options;
}
}
My Attribute ArseniyInk/TaskTwo/Setup/Patch/Data/AddTestEmploymentAttribute.php
<?php declare(strict_types=1);
/**
* Patch to create Customer Attribute
*
*
*/
namespace ArseniyInk\TaskTwo\Setup\Patch\Data;
use Zend_Validate_Exception;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
class AddTestEmploymentAttribute implements DataPatchInterface
{
/**
* @var Config
*/
private $eavConfig;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
*
*
* @param Config $eavConfig
* @param EavSetupFactory $eavSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
Config $eavConfig,
EavSetupFactory $eavSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->eavConfig = $eavConfig;
$this->eavSetupFactory = $eavSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public static function getDependencies(): array
{
return [];
}
/**
* Create account customer attribute
* @return void
* @throws LocalizedException
* @throws Zend_Validate_Exception
*/
public function apply(): void
{
$eavSetup = $this->eavSetupFactory->create();
$customerEntity = $this->eavConfig->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$eavSetup->addAttribute('customer', 'new_test_employment', [
'type' => 'int',
'input' => 'select',
'source' => 'ArseniyInk\TaskTwo\Model\Customer\Attribute\Source\Employment',
'label' => 'NEmployment',
'visible' => false,
'required' => false,
'user_defined' => true,
'system' => false,
'global' => true,
'default' => 0,
'visible_on_front' => false,
'sort_order' => 52,
'position' => 52
]);
$customAttribute = $this->eavConfig->getAttribute('customer', 'new_test_employment');
$customAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']
]);
$customAttribute->save();
}
/**
* {@inheritdoc}
*/
public function getAliases(): array
{
return [];
}
}
My Template ArseniyInk/TaskTwo/view/frontend/templates/account/dashboard/info.phtml
<p>
Employment =
<?= $block->escapeHtml($block->getCustomer()->getCustomAttribute('new_test_employment')->getValue()) ?><br>
</p>