What is proper way to render a WYSIWYG field on front end?
Created 7 years ago by mattcdavis1

I'm currently doing this:


$propertyFormCriteria = $this->dispatch(new GetFormCriteria('form.property'));
$propertyForm = $propertyFormCriteria->get();


{% set fields = propertyForm.fields %}
{% set propertyDirections = fields.get('property_directions') %}
{{ propertyDirections.getObject().render({ 'form':propertyForm, 'entry':propertyForm.getEntry() })|raw }}

If i call render() without getObject() + entry it fails because it can't find a view. I don't think i should need to call getObject() and rendering a textarea / wysiwyg should not require an entry. As things stand now though, is this the correct way to render an individual WYSIWYG field (i don't want to render the whole form because i want more control)?

piterden  —  7 years ago

You need to trace your entities

{# So, fields would be FieldCollection below? #}
{% set fields = propertyForm.fields %}

{# What would you have then? #}     
{# * Get an item from the collection by key. #}
{# public function get($key, $default = null) #}
{% set propertyDirections = fields.get('property_directions') %}

{# Here I think you need classname of propertyDirections #}
{{ propertyDirections.getObject().render({ 'form':propertyForm, 'entry':propertyForm.getEntry() })|raw }}
mattcdavis1  —  7 years ago

@piterden - the code you posted is the same as what i posted. Not sure what you were trying to show here or what you mean by "You need to trace your entities"?

ryanthompson  —  7 years ago

Try {{ propertyForm.fields.property_directions|raw }} for starters. Then you can start using like .input for the input only (based on the .getInput() method the presenter will look for)

You can always call object methods directly on the presenter but there is some magic to clean up view code like .method checking first for .getMethod() on the object if it's not a method on the actual presenter: https://pyrocms.com/documentation/streams-platform/latest#the-basics/presenters/basic-usage/presenter-get (and the resulting __call()) docs.

mattcdavis1  —  7 years ago

@ryanthompson the result of propertyForm.fields.property_directions should be the same as below correct?

{% set fields = propertyForm.fields %}
{% set propertyDirections = fields.get('property_directions') %}

When i call "render" directly on the "propertyDirections" presenter i get an error (for this particular field type - works fine on others) - that's why i'm using getObject(). It just seems like i shouldn't have to do that.

mattcdavis1  —  7 years ago

@ryanthompson i went ahead and tried {{ propertyForm.fields.property_directions|raw }} and got no output. I also translated that twig to php in my controller and got the same thing:

$html = (string) $propertyForm->fields->property_directions;
echo 'html is: ' . $html; die();

("$html" is an empty string)

The code i posted above works, it just seems odd that i have to do all that just to render a form (getObject() + pass in entry)

ryanthompson  —  6 years ago

It does seem odd you have to use getObject().. what about $propertyForm->fields->property_directions->input ?

dd($propertyForm->fields->property_directions); should be the WysiwygFieldTypePresenter correct?

mattcdavis1  —  6 years ago

Yep - the result of the below twig is a WysiwygFieldTypePresenter:

{% set propertyDirections = fields.get('property_directions') %}
{{ dd(propertyDirections) }}


mattcdavis1  —  6 years ago

I have customized some things on my end - so it's possible it's something i've done. Does rendering a Wysiwyg field work on your end without the getObject() using the approach above?