What is proper way to render a WYSIWYG field on front end?
Created 7 years ago by mattcdavis1I'm currently doing this:
Controller:
$propertyFormCriteria = $this->dispatch(new GetFormCriteria('form.property'));
$propertyForm = $propertyFormCriteria->get();
View:
{% 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)?
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.
@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.
@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)
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?
Yep - the result of the below twig is a WysiwygFieldTypePresenter:
{% set propertyDirections = fields.get('property_directions') %}
{{ dd(propertyDirections) }}
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?
You need to trace your entities