1

I Want To fetch all customer name in multi select in my form using customer collection

Following is my form file

<?php

 namespace Magneto\AppNotification\Block\Adminhtml\Form\Edit\Tab;

 use Magento\Backend\Block\Widget\Form\Generic; 
 use Magento\Backend\Block\Widget\Tab\TabInterface;
 use Magento\Backend\Block\Template\Context;
 use Magento\Framework\Registry;
 use Magento\Framework\Data\FormFactory;
 use Magento\Cms\Model\Wysiwyg\Config;


 class Info extends Generic implements TabInterface
  {
  /**
 * @param Context $context
 * @param Registry $registry
 * @param FormFactory $formFactory
 * @param Config $wysiwygConfig
 * @param Status $newsStatus
 * @param array $data
 */


 public function __construct(
    Context $context,
    Registry $registry,
    FormFactory $formFactory,
    array $data = []
) {

    parent::__construct($context, $registry, $formFactory, $data);

}

/**
 * Prepare form fields
 *
 * @return \Magento\Backend\Block\Widget\Form
 */
protected function _prepareForm()
{


    $model = $this->_coreRegistry->registry('appnotification_appnotification');

    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create();

    $form->setHtmlIdPrefix('notification_');
    $form->setFieldNameSuffix('notification');


    $fieldset = $form->addFieldset(
        'base_fieldset',
        ['legend' => __('AppNotification')]
    );

    if ($model->getId()) {
        $fieldset->addField(
            'id',
            'hidden',
            ['name' => 'id']
        );
    }
    $fieldset->addField(
        'title',
        'text',
        [
            'name'        => 'title',
            'label'    => __('Title'),
            'required'     => true
        ]
    );
    $fieldset->addField(
        'subtitle',
        'text',
        [
            'name'        => 'subtitle',
            'label'    => __('Sub Title'),
            'required'     => true
        ]
    );
    $fieldset->addField(
        'image',
        'image',
        [
            'label' => __('Upload Image'),
            'name' => 'image',
            'required'=>true
        ]
    );
    $data = $model->getData();
    $form->setValues($data);
    $this->setForm($form);


    return parent::_prepareForm();
}

/**
 * Prepare label for tab
 *
 * @return string
 */
public function getTabLabel()
{
    return __('AppNotification Info');
}

/**
 * Prepare title for tab
 *
 * @return string
 */
public function getTabTitle()
{
    return __('AppNotification Info');
}

/**
 * {@inheritdoc}
 */
public function canShowTab()
{
    return true;
}

/**
 * {@inheritdoc}
 */
public function isHidden()
{
    return false;
}


}

Here I Want To add customer filed where i want to fetch all customer name

1 Answer 1

0

Add this in your module block

public function getCustomerdata()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customersdata = $objectManager->create('Magento\Customer\Model\Customer')
->getCollection()->getData();
return $customersdata;
}

Then add this in your module phtml file

<?php /*****call the block funtion in phtml*****/ ?>
$customersdata = $block->getCustomerdata();

<?php /*****It is multi select field for customer name*****/ ?>
<div class="admin__field">
    <label for="customer_name" class="admin__field-label"><?php /* @escapeNotVerified */ echo __('Customer Name') ?></label>
    <div class="admin__field-control">
        <select name="customernames[]" id="customernames" class="admin__control-multiselect" multiple>
            <?php
                foreach ($customersdata as $customerdata ):?>
                <option value="<?php echo $customerdata ['firstname'] ?>" ><?php echo $customerdata ['firstname'] ?></option>
            <?php endforeach; ?>
        </select>
    </div>
</div>

I hope this will help and I don't test this code if there is any syntax issue then tell me

1
  • I Want To Fetch it in block i am displaying form using block Mar 5, 2019 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.