Magento 2 Show customer attribute in ui_component form












11















I've created ui_component form.



Where I need to show customer details, Same as Customer Edit.



But, I can able to show their data from customer_entity table.




DataProvider.php




public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

// {Vendor}{Module}ModelGridFactory
// Returns Customer Resource Model
$items = $this->gridFactory->create()->getCollection();

$items->getSelect()->join('customer_entity_text as second', 'main_table.entity_id = second.entity_id');
//print_r($items->getData()); exit;
foreach($items as $contact){
$this->loadedData[$contact->getEntityId()]['contact'] = $contact->getData();
}

return $this->loadedData;
}



I've joined customer_entity_text table with my Factory in order to display status (Customer Attribute).



Now My Second attribute is file type. It's in customer_entity_varchar, Firstly I thought that to add another join but I think it's not the good way.




So, Is there any solution for this? I need to display both Customer Attribute in my form.




ui_component




<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Status</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">contact</item>
</item>
</argument>
</field>


1). Above component is working good for Status but not for Profile Image that is Image type.



<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Profile Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="path_controller"/>
</item>
</item>
</argument>
</field>


Even if I remove one field with same name form element it doesn't seems to be work.



Take a look at field name that is value for Status.



If I use the same thing for image field than image component is disappeared.



Note: I've no idea why Magento doesn't allow to use the name as value.




Cause I've joined in the collection so i am getting value as the array key.




**Question: How can I get customer attributes in this form without join in the collection ?



Also if you have other solution than most also most welcome.**










share|improve this question

























  • Can you check if the new attribute(s) you're using are in the Default attribute set of customer entity ?

    – obscure
    Jun 9 '18 at 7:34











  • Could you read your own question again: the question does not make sense to me when reading it. And therefore it does not help us to resolve your issue?

    – Herve Tribouilloy
    Jun 14 '18 at 8:42











  • Forget rest of things, If you can than answer for how can i display customer attributes in my custom ui form ? one is with Image and other one is text.

    – TBS Mage
    Jun 14 '18 at 9:54













  • is your question to build a form in the frontend or backend?

    – Herve Tribouilloy
    Nov 17 '18 at 15:29
















11















I've created ui_component form.



Where I need to show customer details, Same as Customer Edit.



But, I can able to show their data from customer_entity table.




DataProvider.php




public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

// {Vendor}{Module}ModelGridFactory
// Returns Customer Resource Model
$items = $this->gridFactory->create()->getCollection();

$items->getSelect()->join('customer_entity_text as second', 'main_table.entity_id = second.entity_id');
//print_r($items->getData()); exit;
foreach($items as $contact){
$this->loadedData[$contact->getEntityId()]['contact'] = $contact->getData();
}

return $this->loadedData;
}



I've joined customer_entity_text table with my Factory in order to display status (Customer Attribute).



Now My Second attribute is file type. It's in customer_entity_varchar, Firstly I thought that to add another join but I think it's not the good way.




So, Is there any solution for this? I need to display both Customer Attribute in my form.




ui_component




<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Status</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">contact</item>
</item>
</argument>
</field>


1). Above component is working good for Status but not for Profile Image that is Image type.



<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Profile Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="path_controller"/>
</item>
</item>
</argument>
</field>


Even if I remove one field with same name form element it doesn't seems to be work.



Take a look at field name that is value for Status.



If I use the same thing for image field than image component is disappeared.



Note: I've no idea why Magento doesn't allow to use the name as value.




Cause I've joined in the collection so i am getting value as the array key.




**Question: How can I get customer attributes in this form without join in the collection ?



Also if you have other solution than most also most welcome.**










share|improve this question

























  • Can you check if the new attribute(s) you're using are in the Default attribute set of customer entity ?

    – obscure
    Jun 9 '18 at 7:34











  • Could you read your own question again: the question does not make sense to me when reading it. And therefore it does not help us to resolve your issue?

    – Herve Tribouilloy
    Jun 14 '18 at 8:42











  • Forget rest of things, If you can than answer for how can i display customer attributes in my custom ui form ? one is with Image and other one is text.

    – TBS Mage
    Jun 14 '18 at 9:54













  • is your question to build a form in the frontend or backend?

    – Herve Tribouilloy
    Nov 17 '18 at 15:29














11












11








11


4






I've created ui_component form.



Where I need to show customer details, Same as Customer Edit.



But, I can able to show their data from customer_entity table.




DataProvider.php




public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

// {Vendor}{Module}ModelGridFactory
// Returns Customer Resource Model
$items = $this->gridFactory->create()->getCollection();

$items->getSelect()->join('customer_entity_text as second', 'main_table.entity_id = second.entity_id');
//print_r($items->getData()); exit;
foreach($items as $contact){
$this->loadedData[$contact->getEntityId()]['contact'] = $contact->getData();
}

return $this->loadedData;
}



I've joined customer_entity_text table with my Factory in order to display status (Customer Attribute).



Now My Second attribute is file type. It's in customer_entity_varchar, Firstly I thought that to add another join but I think it's not the good way.




So, Is there any solution for this? I need to display both Customer Attribute in my form.




ui_component




<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Status</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">contact</item>
</item>
</argument>
</field>


1). Above component is working good for Status but not for Profile Image that is Image type.



<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Profile Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="path_controller"/>
</item>
</item>
</argument>
</field>


Even if I remove one field with same name form element it doesn't seems to be work.



Take a look at field name that is value for Status.



If I use the same thing for image field than image component is disappeared.



Note: I've no idea why Magento doesn't allow to use the name as value.




Cause I've joined in the collection so i am getting value as the array key.




**Question: How can I get customer attributes in this form without join in the collection ?



Also if you have other solution than most also most welcome.**










share|improve this question
















I've created ui_component form.



Where I need to show customer details, Same as Customer Edit.



But, I can able to show their data from customer_entity table.




DataProvider.php




public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

// {Vendor}{Module}ModelGridFactory
// Returns Customer Resource Model
$items = $this->gridFactory->create()->getCollection();

$items->getSelect()->join('customer_entity_text as second', 'main_table.entity_id = second.entity_id');
//print_r($items->getData()); exit;
foreach($items as $contact){
$this->loadedData[$contact->getEntityId()]['contact'] = $contact->getData();
}

return $this->loadedData;
}



I've joined customer_entity_text table with my Factory in order to display status (Customer Attribute).



Now My Second attribute is file type. It's in customer_entity_varchar, Firstly I thought that to add another join but I think it's not the good way.




So, Is there any solution for this? I need to display both Customer Attribute in my form.




ui_component




<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Status</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">contact</item>
</item>
</argument>
</field>


1). Above component is working good for Status but not for Profile Image that is Image type.



<field name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Profile Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="path_controller"/>
</item>
</item>
</argument>
</field>


Even if I remove one field with same name form element it doesn't seems to be work.



Take a look at field name that is value for Status.



If I use the same thing for image field than image component is disappeared.



Note: I've no idea why Magento doesn't allow to use the name as value.




Cause I've joined in the collection so i am getting value as the array key.




**Question: How can I get customer attributes in this form without join in the collection ?



Also if you have other solution than most also most welcome.**







magento2 attributes uicomponent customer-account ui-form






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 9:35









Himanshu

827521




827521










asked May 23 '18 at 13:13









TBS MageTBS Mage

10715




10715













  • Can you check if the new attribute(s) you're using are in the Default attribute set of customer entity ?

    – obscure
    Jun 9 '18 at 7:34











  • Could you read your own question again: the question does not make sense to me when reading it. And therefore it does not help us to resolve your issue?

    – Herve Tribouilloy
    Jun 14 '18 at 8:42











  • Forget rest of things, If you can than answer for how can i display customer attributes in my custom ui form ? one is with Image and other one is text.

    – TBS Mage
    Jun 14 '18 at 9:54













  • is your question to build a form in the frontend or backend?

    – Herve Tribouilloy
    Nov 17 '18 at 15:29



















  • Can you check if the new attribute(s) you're using are in the Default attribute set of customer entity ?

    – obscure
    Jun 9 '18 at 7:34











  • Could you read your own question again: the question does not make sense to me when reading it. And therefore it does not help us to resolve your issue?

    – Herve Tribouilloy
    Jun 14 '18 at 8:42











  • Forget rest of things, If you can than answer for how can i display customer attributes in my custom ui form ? one is with Image and other one is text.

    – TBS Mage
    Jun 14 '18 at 9:54













  • is your question to build a form in the frontend or backend?

    – Herve Tribouilloy
    Nov 17 '18 at 15:29

















Can you check if the new attribute(s) you're using are in the Default attribute set of customer entity ?

– obscure
Jun 9 '18 at 7:34





Can you check if the new attribute(s) you're using are in the Default attribute set of customer entity ?

– obscure
Jun 9 '18 at 7:34













Could you read your own question again: the question does not make sense to me when reading it. And therefore it does not help us to resolve your issue?

– Herve Tribouilloy
Jun 14 '18 at 8:42





Could you read your own question again: the question does not make sense to me when reading it. And therefore it does not help us to resolve your issue?

– Herve Tribouilloy
Jun 14 '18 at 8:42













Forget rest of things, If you can than answer for how can i display customer attributes in my custom ui form ? one is with Image and other one is text.

– TBS Mage
Jun 14 '18 at 9:54







Forget rest of things, If you can than answer for how can i display customer attributes in my custom ui form ? one is with Image and other one is text.

– TBS Mage
Jun 14 '18 at 9:54















is your question to build a form in the frontend or backend?

– Herve Tribouilloy
Nov 17 '18 at 15:29





is your question to build a form in the frontend or backend?

– Herve Tribouilloy
Nov 17 '18 at 15:29










1 Answer
1






active

oldest

votes


















0














You need to create your custom table with the relationship of customer_entity table using setup script as follows:



$relationalTable = 'custom_table';  
$table = $setup->getConnection()
->newTable($setup->getTable($relationalTable))
// --- Add your other columns here ---
->addColumn('customer_id', Table::TYPE_INTEGER, 10, ['nullable' => false, 'unsigned' => true],
'Customer Id')
->addForeignKey(
$setup->getFkName(
$relationalTable, // priTableName
'customer_id', // priColumnName
'customer_entity', // refTableName
'entity_id' // refColumnName
),
'customer_id', // column
$setup->getTable('customer_entity'),
'entity_id', // refColumn
Table::ACTION_CASCADE // onDelete
)
->setComment('Customer relation table');

$setup->getConnection()->createTable($table);


Then you need to load customer model and join your custom table in getData() function of DataProvider.php as follows:



protected $_customerModel;

public function __construct(
MagentoCustomerModelCustomerFactory $customerModel
) {
$this->_customerModel = $customerModel;
}

public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

$customer = $this->_customerModel->create();
$collection = $customer->getCollection();
$collection->getSelect()->join(
['custom' => $this->_resource->getTableName('custom_table')],
'e.entity_id = custom.customer_id'
);

foreach($collection as $item){
$this->loadedData[$item->getId()]['contact'] = $item->getData();
// Using $item->getData(), you can get customer object with custom attributes as $item->getStatus() or $item->getProfileImage()
}

return $this->loadedData;
}


Now you can use field names in ui_component as follows:



<field name="status"> <!-- your custom attribute code as field name -->
...
</field>

<field name="profile_image"> <!-- your custom attribute code as field name -->
...
</field>


Hope this solution may resolve your issue.





share























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f227203%2fmagento-2-show-customer-attribute-in-ui-component-form%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You need to create your custom table with the relationship of customer_entity table using setup script as follows:



    $relationalTable = 'custom_table';  
    $table = $setup->getConnection()
    ->newTable($setup->getTable($relationalTable))
    // --- Add your other columns here ---
    ->addColumn('customer_id', Table::TYPE_INTEGER, 10, ['nullable' => false, 'unsigned' => true],
    'Customer Id')
    ->addForeignKey(
    $setup->getFkName(
    $relationalTable, // priTableName
    'customer_id', // priColumnName
    'customer_entity', // refTableName
    'entity_id' // refColumnName
    ),
    'customer_id', // column
    $setup->getTable('customer_entity'),
    'entity_id', // refColumn
    Table::ACTION_CASCADE // onDelete
    )
    ->setComment('Customer relation table');

    $setup->getConnection()->createTable($table);


    Then you need to load customer model and join your custom table in getData() function of DataProvider.php as follows:



    protected $_customerModel;

    public function __construct(
    MagentoCustomerModelCustomerFactory $customerModel
    ) {
    $this->_customerModel = $customerModel;
    }

    public function getData()
    {
    if (isset($this->loadedData)) {
    return $this->loadedData;
    }

    $customer = $this->_customerModel->create();
    $collection = $customer->getCollection();
    $collection->getSelect()->join(
    ['custom' => $this->_resource->getTableName('custom_table')],
    'e.entity_id = custom.customer_id'
    );

    foreach($collection as $item){
    $this->loadedData[$item->getId()]['contact'] = $item->getData();
    // Using $item->getData(), you can get customer object with custom attributes as $item->getStatus() or $item->getProfileImage()
    }

    return $this->loadedData;
    }


    Now you can use field names in ui_component as follows:



    <field name="status"> <!-- your custom attribute code as field name -->
    ...
    </field>

    <field name="profile_image"> <!-- your custom attribute code as field name -->
    ...
    </field>


    Hope this solution may resolve your issue.





    share




























      0














      You need to create your custom table with the relationship of customer_entity table using setup script as follows:



      $relationalTable = 'custom_table';  
      $table = $setup->getConnection()
      ->newTable($setup->getTable($relationalTable))
      // --- Add your other columns here ---
      ->addColumn('customer_id', Table::TYPE_INTEGER, 10, ['nullable' => false, 'unsigned' => true],
      'Customer Id')
      ->addForeignKey(
      $setup->getFkName(
      $relationalTable, // priTableName
      'customer_id', // priColumnName
      'customer_entity', // refTableName
      'entity_id' // refColumnName
      ),
      'customer_id', // column
      $setup->getTable('customer_entity'),
      'entity_id', // refColumn
      Table::ACTION_CASCADE // onDelete
      )
      ->setComment('Customer relation table');

      $setup->getConnection()->createTable($table);


      Then you need to load customer model and join your custom table in getData() function of DataProvider.php as follows:



      protected $_customerModel;

      public function __construct(
      MagentoCustomerModelCustomerFactory $customerModel
      ) {
      $this->_customerModel = $customerModel;
      }

      public function getData()
      {
      if (isset($this->loadedData)) {
      return $this->loadedData;
      }

      $customer = $this->_customerModel->create();
      $collection = $customer->getCollection();
      $collection->getSelect()->join(
      ['custom' => $this->_resource->getTableName('custom_table')],
      'e.entity_id = custom.customer_id'
      );

      foreach($collection as $item){
      $this->loadedData[$item->getId()]['contact'] = $item->getData();
      // Using $item->getData(), you can get customer object with custom attributes as $item->getStatus() or $item->getProfileImage()
      }

      return $this->loadedData;
      }


      Now you can use field names in ui_component as follows:



      <field name="status"> <!-- your custom attribute code as field name -->
      ...
      </field>

      <field name="profile_image"> <!-- your custom attribute code as field name -->
      ...
      </field>


      Hope this solution may resolve your issue.





      share


























        0












        0








        0







        You need to create your custom table with the relationship of customer_entity table using setup script as follows:



        $relationalTable = 'custom_table';  
        $table = $setup->getConnection()
        ->newTable($setup->getTable($relationalTable))
        // --- Add your other columns here ---
        ->addColumn('customer_id', Table::TYPE_INTEGER, 10, ['nullable' => false, 'unsigned' => true],
        'Customer Id')
        ->addForeignKey(
        $setup->getFkName(
        $relationalTable, // priTableName
        'customer_id', // priColumnName
        'customer_entity', // refTableName
        'entity_id' // refColumnName
        ),
        'customer_id', // column
        $setup->getTable('customer_entity'),
        'entity_id', // refColumn
        Table::ACTION_CASCADE // onDelete
        )
        ->setComment('Customer relation table');

        $setup->getConnection()->createTable($table);


        Then you need to load customer model and join your custom table in getData() function of DataProvider.php as follows:



        protected $_customerModel;

        public function __construct(
        MagentoCustomerModelCustomerFactory $customerModel
        ) {
        $this->_customerModel = $customerModel;
        }

        public function getData()
        {
        if (isset($this->loadedData)) {
        return $this->loadedData;
        }

        $customer = $this->_customerModel->create();
        $collection = $customer->getCollection();
        $collection->getSelect()->join(
        ['custom' => $this->_resource->getTableName('custom_table')],
        'e.entity_id = custom.customer_id'
        );

        foreach($collection as $item){
        $this->loadedData[$item->getId()]['contact'] = $item->getData();
        // Using $item->getData(), you can get customer object with custom attributes as $item->getStatus() or $item->getProfileImage()
        }

        return $this->loadedData;
        }


        Now you can use field names in ui_component as follows:



        <field name="status"> <!-- your custom attribute code as field name -->
        ...
        </field>

        <field name="profile_image"> <!-- your custom attribute code as field name -->
        ...
        </field>


        Hope this solution may resolve your issue.





        share













        You need to create your custom table with the relationship of customer_entity table using setup script as follows:



        $relationalTable = 'custom_table';  
        $table = $setup->getConnection()
        ->newTable($setup->getTable($relationalTable))
        // --- Add your other columns here ---
        ->addColumn('customer_id', Table::TYPE_INTEGER, 10, ['nullable' => false, 'unsigned' => true],
        'Customer Id')
        ->addForeignKey(
        $setup->getFkName(
        $relationalTable, // priTableName
        'customer_id', // priColumnName
        'customer_entity', // refTableName
        'entity_id' // refColumnName
        ),
        'customer_id', // column
        $setup->getTable('customer_entity'),
        'entity_id', // refColumn
        Table::ACTION_CASCADE // onDelete
        )
        ->setComment('Customer relation table');

        $setup->getConnection()->createTable($table);


        Then you need to load customer model and join your custom table in getData() function of DataProvider.php as follows:



        protected $_customerModel;

        public function __construct(
        MagentoCustomerModelCustomerFactory $customerModel
        ) {
        $this->_customerModel = $customerModel;
        }

        public function getData()
        {
        if (isset($this->loadedData)) {
        return $this->loadedData;
        }

        $customer = $this->_customerModel->create();
        $collection = $customer->getCollection();
        $collection->getSelect()->join(
        ['custom' => $this->_resource->getTableName('custom_table')],
        'e.entity_id = custom.customer_id'
        );

        foreach($collection as $item){
        $this->loadedData[$item->getId()]['contact'] = $item->getData();
        // Using $item->getData(), you can get customer object with custom attributes as $item->getStatus() or $item->getProfileImage()
        }

        return $this->loadedData;
        }


        Now you can use field names in ui_component as follows:



        <field name="status"> <!-- your custom attribute code as field name -->
        ...
        </field>

        <field name="profile_image"> <!-- your custom attribute code as field name -->
        ...
        </field>


        Hope this solution may resolve your issue.






        share











        share


        share










        answered 8 mins ago









        Dhara BhattiDhara Bhatti

        10819




        10819






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f227203%2fmagento-2-show-customer-attribute-in-ui-component-form%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Magento 2 controller redirect on button click in phtml file

            Polycentropodidae