Render live HTML in DataObject table

Silverstripe Version: 3.6.5

Question:

Hi team, I’m using UserForms to save latitude and longitude coordinates via a form field which is working great.

I would like to link the coordinates to a google map from the admin. I’ve been able to add the a tags to make the link. But the gridfield renders the HTML to view. eg. <a href="//google.com/maps/?q=-42.408169545130654,173.67394540527346" target="_blank">-42.408169545130654,173.67394540527346</a>

Here’s my working code as found in userforms > code > model > submissions > SubmitedFormField.php

I modified the line under getFormattedValue

<?php
/**
 * Data received from a UserDefinedForm submission
 *
 * @package userforms
 */

class SubmittedFormField extends DataObject
{

    private static $db = array(
        "Name" => "Varchar",
        "Value" => "Text",
        "Title" => "Varchar(255)"
    );

    private static $has_one = array(
        "Parent" => "SubmittedForm"
    );

    private static $summary_fields = array(
        'Title' => 'Title',
        'FormattedValue' => 'Value'
    );

    /**
     * @param Member
     *
     * @return boolean
     */
    public function canCreate($member = null)
    {
        return $this->Parent()->canCreate();
    }

    /**
     * @param Member
     *
     * @return boolean
     */
    public function canView($member = null)
    {
        return $this->Parent()->canView();
    }

    /**
     * @param Member
     *
     * @return boolean
     */
    public function canEdit($member = null)
    {
        return $this->Parent()->canEdit();
    }

    /**
     * @param Member
     *
     * @return boolean
     */
    public function canDelete($member = null)
    {
        return $this->Parent()->canDelete();
    }

    /**
     * Generate a formatted value for the reports and email notifications.
     * Converts new lines (which are stored in the database text field) as
     * <brs> so they will output as newlines in the reports
     *
     * @return string
     */
    public function getFormattedValue()
    {
        return '<a href="//google.com/maps/?q=' . nl2br($this->dbObject('Value')->ATT()) . '" target="_blank">' . nl2br($this->dbObject('Value')->ATT()) . '</a>';
    }

    /**
     * Return the value of this submitted form field suitable for inclusion
     * into the CSV
     *
     * @return Text
     */
    public function getExportValue()
    {
        return $this->Value;
    }

    /**
     * Find equivalent editable field for this submission.
     *
     * Note the field may have been modified or deleted from the original form
     * so this may not always return the data you expect. If you need to save
     * a particular state of editable form field at time of submission, copy
     * that value to the submission.
     *
     * @return EditableFormField
     */
    public function getEditableField()
    {
        return $this->Parent()->Parent()->Fields()->filter(array(
            'Name' => $this->Name
        ))->First();
    }
}

Hi @mrdweb, you can cast the value as HTML at the point you return it (I don’t think you should need to use nl2br so I took those out, but I could be wrong):

public function getFormattedValue()
{
    return DBField::create_field('HTMLText', '<a href="//google.com/maps/?q=' . $this->dbObject('Value')->ATT() . '" target="_blank">' . $this->dbObject('Value')->XML() . '</a>');
}

Or you can continue to return a string and let scaffolding take care of the casting:

private static $casting = [
    'Value' => 'HTMLText'
];

More info:
https://docs.silverstripe.org/en/3/developer_guides/model/data_types_and_casting/

Too easy! Thanks heaps @JonoM

Also thanks for sharing the page on casting. That’s going to be very useful

Another question - How can I do an if statement with values at this stage?

Eg. If field is TextField then do this else something else

I’m not sure what you mean - can you expand?

Sorry, brain must’ve turned off before I started typing.

I’d like to use an if statement to conditionally control that Userforms function. Something like below that I’ve added into "userforms > code > model > submissions > SubmittedFormField.php?

public function getFormattedValue()
	{
		if ($EditableFormField == $EditableMapField) {
	    	return DBField::create_field('HTMLText', '<a href="//google.com/maps/?q=' . $this->dbObject('Value')->ATT() . '" target="_blank">' . $this->dbObject('Value')->XML() . '</a>');
	    } else {
	    	return DBField::create_field('HTMLText', $this->dbObject('Value')->ATT());
	    }
	}

However that’s not working. The output is always favours the first statement. Hope that makes better sense. Cheers

Hmmm, I don’t know too much about the UserForms module so I’m not sure how you’d do that. It does feel like your pushing UserForms to its limit though :smiley: do you really need to use UserForms? The point of User Forms is to let non-technical CMS users build forms themselves, but if you don’t need that functionality you can create your own Form and related FormSubmission object and add custom behaviour a lot easier.