Magento 2 – Get Custom Attribute Dropdown/Multiselect Options

custom-attributesmagento2

I created customer custom attribute with input multiselect like this:

$code = 'multiselect_test';
$insertData = array(
        "type"     => "text",
        "backend"  => "",
        "label"    => 'Multiselect Input',
        "input"    => 'multiselect',
        "source"   => "",
        "visible"  => true,
        "required" => false,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""
      );
       $values = array('Briteny','James');
       $insertData['source'] = 'Magento\Eav\Model\Entity\Attribute\Source\Table';
       $insertData['backend'] = 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend';
       $insertData['option']['values'] = $values;


$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $code,  $insertData);
      $attribute   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);

      $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
      $used_in_forms[]="adminhtml_customer";
      $used_in_forms[]="checkout_register"; 
      $used_in_forms[]="customer_account_edit"; 
      $attribute->setData("used_in_forms", $used_in_forms)
      ->setData("is_used_for_customer_segment", true)
      ->setData("is_system", 0)
      ->setData("is_user_defined", 1) 
      ->setData("sort_order", $data['sorting_order']);

    try {
      $attribute->save();
    } catch (Exception $e) {
      $this->_logger->addError($e->getMEssage());
    }

i created a custom input for customer registration form in frontend for that custom attribute like this:

<select multiple name="multiselect_test">
  <option value="0">Britney</option>
  <option value="1">James</option>
</select>

when i looked in backend to edit the customer info in account information the custom attribute input value from frontend was not there, i guess it's because the value for this custom attribute is wrong (not 0 or 1), then i try to insert the value from backend and save it, and the input value save properly, when i try to retrieve the value like this:

$cattrValue = $customer->getCustomAttribute('multiselect_test')->getValue();

i got this result :

39,40

In conclusion i need to know how to retrieve this multiselect/dropdown custom attribute real value and label information, so i can use it to create custom attribute input for customer registration properly, or use it for other process in backend

Best Answer

you can use eav atrribute interface to retrieve available options for your dropdown/multiselect like this:

public function __construct(
        \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
    ) {
        $this->eavAttributeRepository = $eavAttributeRepository; 
    }

public function retrieveOptions($custom_attribute_code){
        $attributes = $this->eavAttributeRepository->get(\Magento\Customer\Model\Customer::ENTITY, $custom_attribute_code);
        $options = $attributes->getSource()->getAllOptions(false);
}