0

My code on a custom delivery time module works all fine but it shows the given text outside the dropdown box , I racked my brain trying to fix this and I still couldn't , please do help!!

WorkingOff.php

<?php
 namespace Shl\DeliveryTime\Block;
 use Exception;
 use Magento\Backend\Block\Template\Context;
 use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
 use Magento\Framework\Data\Form\Element\Factory;
 use Shl\DeliveryTime\Helper\Data;
 use Shl\DeliveryTime\Model\Api\Data\DeliveryTime;

 class WorkingOff extends AbstractFieldArray
 {   
protected $_template = 'Shl_DeliveryTime::system/config/form/field/working-off.phtml';
protected $elementFactory;
public $_tslotList;

public function __construct(
    Context $context,
    Factory $elementFactory,
    array $data = []
) {
    $this->elementFactory = $elementFactory;

    parent::__construct($context, $data);
}

/**
 * Initialise form fields
 *
 * @return void
 */
public function _construct()
{
    $this->addColumn('working_off', [
        'label' => __('Day'),
        'size' => __('40'),]);
    $this->addColumn('tslot1', [
                'label' => __('Time Slot'),
                'size' => __('40'),
                'renderer' => $this->getTslotList(),
                ]);

    $this->_addAfter = false;

    parent::_construct();
}

public function getTslotList()
{   
    $db =$this->db1('core_config_data');
    $sql = "SELECT value FROM core_config_data where path='mpdeliverytime/general/delivery_time'";
    $result = ($db['connection']->fetchAll($sql));
    if (empty($result)) {
 
        }
        else{
    $base = json_decode($result[0]['value'],true);
    $output = array_chunk($base,1);       
    $from = array();
    $bri = array();


    foreach ($output as $key => $value) {    
    //$from[] = $value ['0'];
    //foreach ($from as $key => $value) {
        //for($i=0;$i<=3; $i++){
    $timeArray[] = $value['0']['from']['0'].":".$value['0']['from']['1']."-".$value['0']['to']['0'].":".$value['0']['to']['1'] ;              
    }
}
}

public  function db1($tbl_name){

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $tableName = $resource->getTableName($tbl_name);
    return $db = [
        'connection' => $connection,
        'tableName' => $tableName,
    ];
}
  

/**
 * Render array cell for prototypeJS template
 *
 * @param string $columnName
 *
 * @return mixed|string
 * @throws Exception
 */
public function renderCellTemplate($columnName)
{   
    if (!empty($this->_columns[$columnName]) && in_array($columnName, ['working_off', 'tslot1'])) {
        switch ($columnName) {
            case 'working_off':
                $element = $this->elementFactory->create('select');
                $element->setForm($this->getForm())
                    ->setName($this->_getCellInputElementName($columnName))
                    ->setHtmlId($this->_getCellInputElementId('<%- _id %>', $columnName));
                return str_replace("\n", "<option value=Sunday>Sunday</option><option value=Monday>Monday</option><option value=Tuesday>Tuesday</option><option value=Wednesday>Wednesday</option><option value=Thursday>Thursday</option><option value=Friday>Friday</option><option value=Saturday>Saturday</option>", $element->getElementHtml());

            case 'tslot1':
                
                $db =$this->db1('core_config_data');
                $sql = "SELECT value FROM core_config_data where path='mpdeliverytime/general/delivery_time'";
                $result = ($db['connection']->fetchAll($sql));
                if (empty($result)) {
                    $count=1;
                    $timeArray[]=0;
                    $newstring="";
                }
                else{
                $base = json_decode($result[0]['value'],true);
                $output = array_chunk($base,1);       
                $from = array();
                $option[] = array();
                $c = array();     
                $count = 0;
                $newstring="";        
               

                foreach ($output as $key => $value) {   
                //$c = $value[$key];
                
                $timeArray[] = $value['0']['from']['0'].":".$value['0']['from']['1']."-".$value['0']['to']['0'].":".$value['0']['to']['1'] ; 
                $option[] = "<option value=$timeArray[$key]>$timeArray[$key]</option>";


                $count++;          
                }
            }
                 $element = $this->elementFactory->create('select');
                               
               
                $element->setForm($this->getForm())
                        ->setName($this->_getCellInputElementName($columnName))
                        ->setHtmlId($this->_getCellInputElementId('<%- _id %>', $columnName));

                for($i = 0; $i < $count; $i++) {
                $newstring .= "<option value=$timeArray[$i]>$timeArray[$i]</option>";}                                            
                
                return str_replace("\n", $newstring , $element->getElementHtml());
                break;
            default:
                break;
        }
    }

    return parent::renderCellTemplate($columnName);
}

/**
 * @return string
 */
// public function getAddButtonLabel()
// {
//     return $this->_addButtonLabel;
// }
}?>

Day Off DropdownDay Off Dropdown

5
  • First Inspect and see what html is generated or you can update your post with that.. Bro no one going to answer your question unless more data not mention. Its not a common stuff to happen – Nilesh Dubey Dec 23 '20 at 12:08
  • Hey I have check your post twice a day and no one answered that's why I have ask you to provide more data on same so that I can figure out what is going wrong. All I mean to help you. Don't take it in other direction. – Nilesh Dubey Dec 23 '20 at 13:34
  • I did understand that brother and I had confirmed with a couple of Magento 2 Co-Developers that this is the only code that needed that is required after inspecting. – weirdedout23 Dec 23 '20 at 13:49
  • But after that I found that your question was even more vague than mine and I just wanted to point at the hypocrisy – weirdedout23 Dec 23 '20 at 13:50
  • hypocrisy - How? can you tell me what you want in that post brother and I will add more info into it so you can understand it properly. – Nilesh Dubey Dec 23 '20 at 16:41

Your Answer

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

Browse other questions tagged or ask your own question.