{% extends "demo/_layout" %}

{% set pageTitle = "Populate a form with Craft Entry Data | Extras" %}

{# Replace 'freeformCraftEntryData' with your form handle #}
{% set demoFormHandle = "freeformCraftEntryData" %}

{# Replace 'freeform-test' with your Craft Entry slug #}
{% set entry = craft.entries().slug('freeform-test').one %}

{% block content %}

    {% set form = freeform.form(demoFormHandle, { formattingTemplate: pageTemplateFile }) %}

    <div class="freeform-page-heading">
        <h2>Populate a form with Craft Entry Data</h2>
        <p>The following example will populate some of the form's fields with some data from a Craft Entry.</p>
    </div>

    {% if entry and form %}

        <div class="freeform-notice{{ colorMode == "dark" ? " freeform-notice-dark" }}">
            <p>Craft Entry:</p>
            <h4>{{ entry.title }} <small>(#{{ entry.id }})</small></h4>
            <p>Posted on {{ entry.postDate.format('F d, Y') }}</p>
        </div>

        {# Replace 'overrideValues' parameters as you please #}
        {{ form.render({
            overrideValues: {
                productId: entry.id,
                productTitle: entry.title,
                productUrl: url(craft.app.request.pathInfo),
            }
        }) }}

    {% else %}

        <div class="freeform-error{{ colorMode == "dark" ? " freeform-notice-dark" }}">
            <p>You must rename your form handle to <code>{{ demoFormHandle }}</code> and correctly load an existing Craft Entry for this part of the demo to work.</p>
        </div>

    {% endif %}

    {# Instructions to get this page working correctly #}
    <div class="extras-instructions">
        <h4>This page will not display correctly until...</h4>
        <p>
            In order to view this page live, you'll need to make some adjustments to this template or on your test from:
            <ol>
                <li>Rename your test form handle to <code>{{ demoFormHandle }}</code> or adjust the form handle name inside this template to match your test form.</li>
                <li>
                    Make sure your test form includes the following Freeform fields at minimum, or adjust the overrides inside this template:
                    <ul>
                        <li>Product ID field (text or hidden) with handle of <code>productId</code>.</li>
                        <li>Product Title field (text or hidden) with handle of <code>productTitle</code>.</li>
                        <li>Product URL field (text or hidden) with handle of <code>productUrl</code>.</li>
                    </ul>
                </li>
                <li>
                    Make sure you have a Craft Entry with a slug of <code>freeform-test</code> and includes the following Craft fields at minimum, or adjust the overrides inside this template:
                    <ul>
                        <li>Title field</li>
                    </ul>
                </li>
            </ol>
        </p>
    </div>

{% endblock %}