Push HTML to grid field

Discussion in 'Customization & add-ons' started by yoursuite, Dec 27, 2019.

  1. yoursuite

    yoursuite New Member

    Joined:
    May 16, 2019
    Messages:
    4
    Dear all.
    I'm trying to push HTML code into a grid field editing site.php file. It works but the code is escaped when rendered.
    This is the method I used:

    $newColumn = $e->getGrid()->addField(new Am_Grid_Field("Usage", "Usage", false))
    ->setGetFunction(function($record, $grid, $fieldname, $field){
    $columnCode = "Some code...";
    return $columnCode;
    });

    I also tried to replace some arbitrary placeholders, like this...

    Am_Di::getInstance()->hook->add(Am_Event::AFTER_RENDER, function (Am_Event_AfterRender $event) {
    if (preg_match('#/URL.*#i', $_SERVER['REQUEST_URI']))
    {
    $event->replace('#PLACEHOLDER_CODE#i', "<span class=\"myClass\">Activities</span>");
    }
    }

    ...with no success.
    It works only for the first page (I'm working on Browse Users page). When I click on "next" or manually search for something, the AFTER_RENDER event is not called anymore.
    How can I push HTML inside a field, as it is, without code replacements?
    Thanks in advance
  2. yoursuite

    yoursuite New Member

    Joined:
    May 16, 2019
    Messages:
    4
    I got it!
    I had to use the function setRenderFunction.
    Here is the code:

    $newColumn->setRenderFunction(function($record, $fieldname, $grid, $field){
    $columnCode = "";
    return $grid->renderTd($columnCode, false); // false turns off the escape function.
    });

    Watch out the order of the parameters because it is different compared to setGetFunction.
    Best regards!

Share This Page