1

I'm a bit confused with creating a city dropdown with my custom cities. I have already imported the cities into a table and also created the Model, ResourceModel, Collection. Now I need to get those cities into a dropdown.

Below image is the table of my custom cities:

cities

Below is my code to implement the 'getAllOptions()' function:

namespace Talliance\SwyftShippingMethod\Model\Customer\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Talliance\SwyftShippingMethod\Model\ResourceModel\SrilankanCity\CollectionFactory as SrilankanCityCollectionFactory;

class SrilankanCity extends AbstractSource
{

   protected $_srilankanCityCollectionFactory;

   protected $_logger;

   public function __construct
   (
       SrilankanCityCollectionFactory $srilankanCityCollectionFactory,
       \Psr\Log\LoggerInterface $logger
   )
   {
       $this->_srilankanCityCollectionFactory = $srilankanCityCollectionFactory;
       $this->_logger = $logger;
   }

   public function getAllOptions()
   {
       if (!$this->_options) {
           $this->_options = $this->_createCitiesCollection()->toOptionArray();
           $this->_logger->log(100, print_r($this->_options, true));
       }
       return $this->_options;
   }

   /**
    * @return \Talliance\SwyftShippingMethod\Model\ResourceModel\SrilankanCity\Collection
    */
    protected function _createCitiesCollection()
    {
        return $this->_srilankanCityCollectionFactory->create();
    }
}

This is my output in my debug log file:

enter image description here

What am i doing wrong here? Thanks in advance.

  • Don't use toOptionArray for your custom collection. You need to create new array with lable, value structure and then return that array. – Sukumar Gorai Jan 9 at 13:56
0

I solved this problem for my dropdown list by using this code make changes according to your code

<?php


namespace Talliance\SwyftShippingMethod\Model\Customer\Source;

use Magento\Store\Ui\Component\Listing\Column\Store\Options as StoreOptions;

/**
 * Store Options for Cms Pages and Blocks
 */
class SrilankanCity extends StoreOptions
{

    /**
     * Get options
     *
     * @return array
     */
    public function toOptionArray()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $model = $objectManager->create('Vendor\Namespace\Model\SrilankanCity');
        $collection = $model->getCollection();

        foreach ($collection as $value) {

            $options[] = ['label' => $value->srilankan_cities(), 'value' => $value->srilankan_cities()];
        }

        return $options;


    }
}
  • Could you please tell me what did you implement in srilankan_cities() function and where did you put that? – Bernedette Perera Jan 10 at 4:17
0

Thank you. I found a way to get it done.

namespace ...Model\Customer\Source;
...
use Magento\Framework\Option\ArrayInterface;
...

public function toOptionArray()
{
    /** @var \Talliance\SwyftShippingMethod\Model\ResourceModel\SrilankanCity\Collection $collection */
    $collection = $this->_cityCollection->create();
    $collection->getCities();
    $options = $collection->toOptionArray();
    $this->_logger->log(100, print_r($options, true));
    return $options;
}

Then in collection.php

/**
 * Get sri lankan cities
 *
 * @return $this
 */
public function getCities()
{
    return $this->addFieldToSelect(array('srilankan_cities'));
}

/**
 * Retrieve option array
 *
 * @return array
 */
public function toOptionArray()
{
    return parent::_toOptionArray('id', 'srilankan_cities');
}

OUTPUT: enter image description here

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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