0

I've created an adminhtml grid which is supposed to map Block Positions to Services. For example: The block "Sidebar right Bottom" could be linked to Facebook, Twitter, Instagram and then that block would have those 3 services linked in it.

The issue I'm having, is that the column "Services" is displaying the IDs of the services, instead of their Titles. I've tried to rectify this issue by modifying my Grid.php file:

protected function _prepareColumns()
{
    // Add the columns that should appear in the grid
    $this->addColumn('id',
        array(
            'header'=> $this->__('ID'),
            'align' =>'right',
            'width' => '50px',
            'index' => 'id'
        )
    );

    // $this->addColumn('name',
    //     array(
    //         'header'=> $this->__('Name'),
    //         'index' => 'name'
    //     )
    // );

    $this->addColumn('name', array(
        'header'    => Mage::helper('sociable')->__('Name'),
        'index'     => 'name',
        'type'      => 'options',
        'options'   => array(
            'sociable.product_page' => Mage::helper('sociable')->__('Inside product page'),
            'sociable.left_top' => Mage::helper('sociable')->__('Sidebar left top'),
            'sociable.left_bottom' => Mage::helper('sociable')->__('Sidebar left bottom'),
            'sociable.right_top' => Mage::helper('sociable')->__('Sidebar right top'),
            'sociable.right_bottom' => Mage::helper('sociable')->__('Sidebar right bottom'),
            'sociable.content_top' => Mage::helper('sociable')->__('Content top'),
            'sociable.content_bottom' =>  Mage::helper('sociable')->__('Content bottom')
        ),
    ));
    $serviceTitle = [];
    $serviceId = [];
    $service = Mage::getModel('sociable/service')->getCollection();
    foreach($service as $data){
        array_push($serviceTitle, $data->getTitle());
        array_push($serviceId, $data->getServicesId());
    }
    $counter = count($serviceTitle);
    $serviceArray = [];
    for($i = 0 ; $i < $counter ; $i++){
        $serviceArray[$serviceId[$i]] = $serviceTitle[$i];
    }

    $this->addColumn('services',
        array(
            'header'=> $this->__('Services'),
            'index' => 'services',
            'type'  => 'options',
            'options' => $serviceArray,
            )
    );

    return parent::_prepareColumns();
}

I've tried providing the services field with "options" and passing in the array of services which is created by mapping the ID of the service to the Title of the service. The Array looks like so:

Array(
    [1] => Facebook
    [2] => Twitter
    etc...
)

When the multi-select field is set with only 1 value, the grid will display the value as needed. However if multiple values are selected, the grid will display nothing. Here is a screenshot to illustrate:

enter image description here

Here is how it looks when I remove the "options" from the services field:

enter image description here

I'm hoping that when multiple values are saved, it will be stored in the Grid like so: "Diigo, Twitter, Facebook" ... etc. Similar to how the IDs are stored in the second image.

Any ideas on how I can resolve this issue?

Thanks so much!

  • I've just realized it's probably because the IDs are comma separated, so instead of looking for "3" and "4" and "5" it looks for "3, 4, 5" and can't map it to anything. I can't think of a solution though. – Jack Perry Apr 26 '18 at 6:23
1

I found out the solution:

In Grid.php file you can use a custom column renderer like so:

$this->addColumn('services',
        array(
            'header'=> $this->__('Services'),
            'index' => 'services',
            'renderer' => 'AW_Sociable_Block_Adminhtml_Page_Grid_Renderer_Page',
    ));

Now you create the file Page.php in the directory shown in the "renderer" section of Grid.php. Loop through the multiselect IDs and return the values you need. This is how I did it:

<?php

class AW_Sociable_Block_Adminhtml_Page_Grid_Renderer_Page extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row) 
    {   
        $services = $row->getData();
        $i = 0;
        $serviceArray = [];

        foreach($services as $service){
            array_push($serviceArray, $service);
            $i++;
        }

        $serviceList = str_replace( ',', '', $serviceArray);
        $serviceList = $serviceList[2];
        error_log(print_r("Before " . $serviceList, true), 3, '/var/www/html/error1.log');
        $serviceList = str_split($serviceList);
        error_log(print_r("After " . $serviceList, true), 3, '/var/www/html/error1.log');
        $returnValue = [];

        foreach($serviceList as $id){
            array_push($returnValue, Mage::getModel('sociable/service')->load($id)->getTitle());
        }
        $returnValue = implode(", ", $returnValue);

        return $returnValue;

    }
}

Not as hard as I thought it would be :)

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.