簡體   English   中英

FOSMessageBundle - 如何在沒有字段表單收件人的情況下發送郵件

[英]FOSMessageBundle - How to send a message without the field form recipient

我隱藏了我的收件人字段表單,現在我想了解控制器中的哪個位置可以告訴收件人的價值

MessageController:

/**
 * Create a new message thread
 *
 * @return Response
 */
public function newThreadAction()
{
    $form = $this->container->get('fos_message.new_thread_form.factory')->create();
    $formHandler = $this->container->get('fos_message.new_thread_form.handler');

    if ($message = $formHandler->process($form)) {
        return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array(
            'threadId' => $message->getThread()->getId()
        )));
    }

    return $this->container->get('templating')->renderResponse('FOSMessageBundle:Message:newThread.html.twig', array(
        'form' => $form->createView(),
        'data' => $form->getData()
    ));
}

$ form math to:

class NewThreadMessageFormFactory extends AbstractMessageFormFactory
{
    /**
     * Creates a new thread message
     *
     * @return Form
     */
    public function create()
    {
        $message = $this->createModelInstance();

        return $this->formFactory->createNamed($this->formName, $this->formType, $message);
    }
}

$ formHandler匹配到:

class NewThreadMessageFormHandler extends AbstractMessageFormHandler
{
    /**
     * Composes a message from the form data
     *
     * @param AbstractMessage $message
     * @return MessageInterface the composed message ready to be sent
     * @throws InvalidArgumentException if the message is not a NewThreadMessage
     */
    public function composeMessage(AbstractMessage $message)
    {
        if (!$message instanceof NewThreadMessage) {
            throw new \InvalidArgumentException(sprintf('Message must be a NewThreadMessage instance, "%s" given', get_class($message)));
        }

        return $this->composer->newThread()
                    ->setSubject($message->getSubject())
                    ->addRecipient($message->getRecipient())
                    ->setSender($this->getAuthenticatedParticipant())
                    ->setBody($message->getBody())
                    ->getMessage();
    }
}

我希望有一些解決方案!

非常感謝 !

創建表單后,您可以設置表單的表單數據。 我的表單以下列方式解決:

在你的newThreadAction中:

$form = $this->getFormForNewThreadAction($account);

請記住,$ account在我的示例中有ParticipantInterface。

在MessageController中:

/**
 * @param ParticipantInterface $account
 * @return \FOS\MessageBundle\FormFactory\Form|FormInterface
 */
private function getFormForNewThreadAction(ParticipantInterface $account)
{
    $form = $this->getNewThreadFormFactory()->create();
    $preSetFormData = $this->getPreSetFormData($account, $form);
    $form->setData($preSetFormData);

    return $form;
}

/**
 * @param ParticipantInterface $account
 * @param Form $form
 * @return NewThreadMessage
 */
private function getPreSetFormData(ParticipantInterface $account, Form $form)
{
    $recipients = new ArrayCollection();
    $recipients->add($account);

    /** @var NewThreadMessage $newThreadMessage */
    $newThreadMessage = $form->getData();
    $newThreadMessage->setRecipients($recipients);

    return $newThreadMessage;
}

表單需要將NewThreadMessage作為數據,您可以在那里設置收件人。 就這些了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM