XF 2.2 Using the editor in my own template

grantus

Active member
I'm wondering what is the correct way to use Xenforo's editor in my own template.

I have this so far in my template:
Code:
<xf:form action="{{ link('battle/comment', $battle) }}" class="block" ajax="1">
                    <div class="block-container">
                        <div class="block-body">
                            <div class="js-controls">
                                <xf:editorrow name="message" 
                                    rowtype="fullWidth noLabel"
                                    label="{{ phrase('comment') }}" />
                                <xf:submitrow submit="Save" fa="fa-save" />
                            </div>
                        </div>
                    </div>
                </xf:form>

Then in my controller:
Code:
public function actionBattleComment(ParameterBag $params) {
        $battle = $this->em()->create('ILL:BattleComments');
        $this->battleCommentSaveProcess($battle, $params->battle_num)->run();
        
    }

public function battleCommentSaveProcess(\ILL\Entity\BattleComments $battle, $params->battle_num) {
        $input = $this->filter([
            'message' => 'str'
        ]); 
        $form = $this->formAction();
        $form->basicEntitySave($battle, $input);
        $form->complete(function() use ($battle, $params->battle_num) {
            $battle->battle_num = $params->battle_num;
            $battle->save();
        });
        return $form;
        
    }

For my entity, I set one up and copied everything from the XF Post entity (and added battle_num) to see what would work. I also created a table, copying the xf_post table.

So far the only issue I'm encountering is that it keeps telling me to enter a valid message. I can see that my message is being sent but nothing is saving.

However, is there an easier way to implement the editor without having to try and copy the xf_post table and XF Post entity?
 
You should use the editor controller plugin to capture editor input:
PHP:
$input['message'] = $this->plugin(\XF\ControllerPlugin\EditorPlugin::class)->fromInput('message');
 
Last edited:
You should use the editor controller plugin to capture editor input:
PHP:
$input['message'] = $this->plugin(\XF\ControllerPlgin\EditorPlugin::class)->fromInput('message');
I tried that but I don't see an EditorPlugin, but there's an Editor file so I did this:

Code:
$editorPlugin = $this->plugin(\XF\ControllerPlugin\Editor::class);
        $message = $editorPlugin->fromInput('message');
        $form = $this->formAction();
        $form->setup(function () use ($battle, $message) {
            $battle->message = $message;
        });
        $battle->save();
        return $form;

Is EditorPlugin in version 2.2?

What I'm wondering though is what columns are required in my table? I'll insert user_id, username, message, etc but are there certain columns that the editor requires?

I'm getting an error though when I go to save the message, it's "please_enter_valid_message" from the Entity structure.
 
Sorry, I missed the 2.2 prefix. Class suffixes were added in many places in 2.3. For 2.2, just use the short-name:

PHP:
$input['message'] = $this->plugin('XF:Editor')->fromInput('message');

What I'm wondering though is what columns are required in my table? I'll insert user_id, username, message, etc but are there certain columns that the editor requires?
The editor only requires a column to store the BBCode, but you may also want embed_metadata if you plan to have things like attachments, quotes, unfurls, etc. Those require delegating to the message preparer service though. You can see eg. the core thread creator or post replier services for usage examples.

I'm getting an error though when I go to save the message, it's "please_enter_valid_message" from the Entity structure.
When using form actions, the basicEntitySave method will add the save callback during the apply phase already. You're likely getting this error because you're also calling it manually. You can instead do something like:

PHP:
public function battleCommentSaveProcess(\ILL\Entity\BattleComments $battle, $params->battle_num)
{
    $form = $this->formAction();

    $input = $this->filter([
        'message' => 'str'
    ]);
    $form->basicEntitySave($battle, $input);

    $form->setup(function() use ($battle, $params->battle_num)
    {
        $battle->battle_num = $params->battle_num;
    });

    return $form;       
}
 
Ok, thanks.

So far I'm on track, I'm able to insert no problem, but in the editor when I post, the message stays in the editor (I eventually want the same functionality as here in the thread view). Is there something in the editor template syntax that should be added, or is it just a matter of sending the user like "return $this->view" etc?
 
Last edited:
You'd want data-xf-init="quick-reply" on the <xf:form> element with an appropriate data-message-container attribute set to the selector for the element to append the messages to (see eg. the thread_view template). In your controller, you'll want to switch on the _xfWithData input to return a view reply of the messages to return instead of a redirect (see eg. \XF\Pub\Controller\Thread::actionAddReply).
 
You'd want data-xf-init="quick-reply" on the <xf:form> element with an appropriate data-message-container attribute set to the selector for the element to append the messages to (see eg. the thread_view template). In your controller, you'll want to switch on the _xfWithData input to return a view reply of the messages to return instead of a redirect (see eg. \XF\Pub\Controller\Thread::actionAddReply).
I completely forgot about quick-reply!

I've never seen _xfWithData before. I just looked it up and for example in Post.php there's
Code:
if ($this->filter('_xfWithData', 'bool')
 
Back
Top Bottom