0

I've created a custom module that adds form submissions as relations to the entry they were submitted on. This functionality works, but I can't figure out how to change the template for the relations field in the CP. Currently this field displays submissions with only their titles, and Formie forces the title of each submission to be only the time it was submitted, so users won't be able to tell submissions apart. The list should instead display the name of the person who submitted, which is input by the form.

enter image description here

I don't understand how to use the module directory system, I tried many different configurations (with and wtihout /src/, with and without /templates/ etc). I've tried to keep it simple and read many docs. Currently I have:

  • my-project/modules/eventparticipants/Module.php
  • my-project/modules/eventparticipants/src/templates/elementSelect.twig

The error I get is "Unable to find the template “elementSelect.twig”."

I'd be really grateful if someone understands how this needs to come together.

In my module I have

<?php
namespace eventparticipants;

use Craft;

use yii\base\Event;
use craft\web\View;
use craft\events\RegisterTemplateRootsEvent;


class Module extends \yii\base\Module
{
    public function init()
    {

        // put this inside the init() method. NOT WORKING, I THINK THIS DOES NOTHING.
Event::on(
    View::class,
    View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
    function (RegisterTemplateRootsEvent $e) {
                Craft::info($e, 'custom-module');
            if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
                $e->roots[$this->id] = $baseDir;
                }
            }
);

//TRIED THIS BUT DONT UNDERSTAND HOW IT RELATES TO ABOVE. 
        /*    $view = Craft::$app->getView();
        $oldTemplatesPath = $view->getTemplatesPath();

        $view->setTemplatesPath($this->getBasePath());
        $html = $view->renderTemplate('/src/templates/elementSelect.twig');

        $view->setTemplatesPath($oldTemplatesPath);

        return $html;*/





        // Define a custom alias named after the namespace
        Craft::setAlias('@eventparticipants', __DIR__);

        // Set the controllerNamespace based on whether this is a console or web request
        if (Craft::$app->getRequest()->getIsConsoleRequest()) {
            $this->controllerNamespace = 'eventparticipants\\console\\controllers';
        } else {
            $this->controllerNamespace = 'eventparticipants\\controllers';
        }


        parent::init();

        // Custom initialization code goes here...
    }
}


/*

app should
- get form submission id
- get event entry
- attach form submission id as a relation to the event entry
*/

use craft\events\ModelEvent;
use craft\elements\Entry;
use verbb\formie\elements\Submission;
//use yii\base\Event;

Event::on(Submission::class, Submission::EVENT_AFTER_SAVE, function(ModelEvent $event) {
    $submission = $event->sender;
    $query = Entry::find()->section('event')->id($submission->eventId);
    $entry = $query->one();
    $existing_participants = $entry->participantssubs->ids();
    $existing_participants[] = $submission->id;
    if ($entry) {
            $entry->text500 = 'test from module asdf';
            $entry->participantssubs = $existing_participants;
            Craft::$app->elements->saveElement($entry);
    }
    
});



//attempt to template this. I don't understand where this code should go, or where the template should go. Using this, i can change nearly everything about the form except the template. 

namespace verbb\formie\fields;

use verbb\formie\elements\Submission;
use verbb\formie\elements\db\SubmissionQuery;
use verbb\formie\gql\arguments\SubmissionArguments;
use verbb\formie\gql\interfaces\SubmissionInterface;
use verbb\formie\gql\resolvers\SubmissionResolver;

use Craft;
use craft\fields\BaseRelationField;

use GraphQL\Type\Definition\Type;

class Submissions extends BaseRelationField
{
    // Public Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    public static function displayName(): string
    {
        return Craft::t('formie', 'Submissions (Formieasdf)');
    }

    /**
     * @inheritdoc
     */
    public static function elementType(): string
    {
        return Submission::class;
    }

    /**
     * @inheritdoc
     */
    public static function defaultSelectionLabel(): string
    {
        return Craft::t('formie', 'Add a submission');
    }


//THIS CAUSES ERRORS WITH: elementSelect.twig, /elementSelect.twig, src/templates/elementSelect.twig, /src/templates/elementSelect.twig, etc.

    protected string $inputTemplate = 'elementSelect.twig';

    /**
     * @inheritdoc
     */
    public static function valueType(): string
    {
        return SubmissionQuery::class;
    }

    /**
     * @inheritdoc
     */
    public function getContentGqlType(): array|Type
    {
        return [
            'name' => $this->handle,
            'type' => Type::listOf(SubmissionInterface::getType()),
            'args' => SubmissionArguments::getArguments(),
            'resolve' => SubmissionResolver::class . '::resolve',
        ];
    }
}
1
  • Just going to throw this out there, as it's not a direct answer to your question, but may still be beneficial nonetheless: you are actually able to change the format Formie uses for the submission names (edit your form > Settings > Submission Title Format - should be able to pull in the submitter's name from the respective form field via the + icon on the right)...
    – Jalen Davenport
    Commented Feb 24 at 23:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.