1

I use Craft CMS 4.2.3. I can't find information on how to add an element from the site to a certain section. Please help me solve the problem. It would be very cool if it were some simple example. Thank you in advance.

I mean to add an element to the section not through the administrative panel. And, for example, create a form on the site and through it add an element to the section.

2 Answers 2

4

There's two methods for this, the first one I'll assume your frontend form doesnt directly make a new entry, but does other things and triggers a new entry as well. The second, I will assume your form is directly making an entry as soon as you hit submit, and is getting all of your entry info from the form itself.

METHOD 1 - THE FORM IS NOT THE ENTRY INFO

To do this via PHP you will need to create a Module. Modules are a Yii convention for extending PHP functionality.

Start Here in the "Extending Craft" docs: https://craftcms.com/docs/4.x/extend/module-guide.html

Then, you will want to create a public controller for the module, which you can call as the form's action in your code.

Controllers Guide: https://craftcms.com/docs/4.x/extend/controllers.html#creating-a-controller

Since you are looking to make new entries, the entries guide is your next stop: https://craftcms.com/docs/4.x/entries.html

METHOD 2 - THE FORM IS THE ENTRY INFO

<form method="post" accept-charset="UTF-8" enctype="multipart/form-data" class="send-form" id="send-form">
    <input type="hidden" name="action" value="entries/saveEntry"/>
    <input type="hidden" name="sectionId" value="X"> // Required! ID# of the section
    <input type="hidden" name="typeId" value="X"> // ID# of the entry type
    <input type="hidden" name="enabled" value="1">
    
    <label>Text Field
        <input type="text" name="fields[customField]" value="">
    </label>

    [... some more fields ...]

     <input class="btn-vote" type="submit" value="send">

</form>

Here's some guides and help for frontend forms:

Matrix (New & Editing Existing): adding a block of matrix field from a front end entry form

Video Tutorial (CraftQuest): https://craftquest.io/lessons/publishing-entries-from-the-front-end

2

If you mean the same things that Craft refers to as elements and sections, you can create a new entry in a section by opening the CMS and navigating to Entries using the sidebar menu. Select the desired section, then click the New entry button on the upper right. This creates a new unpublished draft, which you can then edit and publish when you're done.

If you mean something else, please explain what you're trying to do in detail.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.