XF 2.1 Pass Variables to poll_macros

Yodrak

in memoriam 1976 - 2020
Hello, i have problems to pass 2 Variables wich i have created in my Addon to the template poll_macros.
The path is similar to /XF/Pub/Controller.php and this is my code.

PHP:
<?php

namespace Yodrak\PhotoPoll\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Thread extends XFCP_Thread

{
    public function actionIndex(ParameterBag $params)
    {
        $parent = parent::actionIndex($params);

        if (!$parent instanceof \XF\Mvc\Reply\View)
        {
            return $parent;
        }

        $visitor = \XF::visitor();

        if (!$visitor->hasPermission('photoPoll', 'view'))

        {
            return $parent;
        }

        $options = \XF::options();

        $includeForums = $options->photoContestIncludeForums;

        $thread = $this->assertViewableThread($params->thread_id);

        $threadId = $thread['thread_id'];

        $nodeId = $thread->node_id;

        if (!in_array($nodeId, $includeForums))

        {
            return $parent;
        }

        $finder = \XF::finder('XF:Post');

        $photoPollPosts = $finder
            ->where('thread_id', $threadId)
            ->where('message_state', '=', 'visible')
            ->order('username')
            ->fetch();

        $postRepo = $this->getPostRepo();

        $postList = $postRepo->findPostsForThreadView($thread)->onPost(2, 200);

        $photoPollThumbnails = $postList->fetch();

        $parent->setParams([

            'photoPollPosts' => $photoPollPosts,
            'photoPollThumbnails' => $photoPollThumbnails
        ]);

        return $this->view('XF:Poll\Create', 'poll_create', $parent);
    }

}

If i dump the Poll Edit Template my variables are not visible.
 
Last edited:
Are you trying to add a poll to an existing thread, change an existing poll, or add a poll when creating a new thread? If it is the latter then post-thread action is in Forum.php

Also, why are you changing the view of actionIndex in Thread.php? You only need to return $parent;

Note that poll_create template doesn't accept any arguments.
 
Last edited:
Are you trying to add a poll to an existing thread, change an existing poll, or add a poll when creating a new thread? If it is the latter then post-thread action is in Forum.php

Also, why are you changing the view of actionIndex in Thread.php? You only need to return $parent;

Note that poll_create template doesn't accept any arguments.
I try to explain you, what the addon should do. Every Month we make a Photo Contest in our forum. The Members posts Photos and at the 10. of the month i start a poll. I change the poll template so that is possible to see the photos inside the poll and you can vote the photo. What i want to do with this addon is to get two variables inside the template, so that it autocreate the poll answers after i click on the "create Poll" Button.

Till now i give in every row in the poll answers, what is time comsuming and faulty.

Bildschirmfoto zu 2020-08-30 15-26-17.webp

After i safe the poll form it's look like this.

1598794785837.webp
 
The way I would go is to extend ControllerPlugin/Poll actionCreate. $thread is available there (as $content) so you can retrieve the relevant thread info you need.

What you will then need to do is create a new poll_create template for the controller plugin that will have access to your data, ie: return $this->view('XF:Poll\Create', 'your_new_poll_create_template', $viewParams);
 
The way I would go is to extend ControllerPlugin/Poll actionCreate. $thread is available there (as $content) so you can retrieve the relevant thread info you need.

Yes i've tried that and im able to send the variables to the poll macro, if i set the thread_id static. But i have problems to get the dynamic thread_id in ICODE]ControllerPlugin/Poll actionCreate[/ICODE].

@Lawrence thanks for your help. (y)
 
Ok, i've got it with the thread_id.

PHP:
$thread = $content;
$threadId = $thread['thread_id'];
You should do a content type check first, for sanity reasons:

PHP:
if ($contentType != 'thread')
{
    return $parent;
}

if (!$threadId = $content['thread_id'])
{
    return $parent;
}
...
 
@Lawrence thank you again for gave me the hint. The Addon Work, what i still have todo is to finish the Templates

My Problem is, i'm a noob in PHP and i don't understand till now, how to inspect the code right.
 
I have one issue left in my addon. After i submit the poll form i get this message:

Code:
Oops! We ran into some problems.
You do not have permission to view this page or perform this action.

If i check the consols its throws me this error.

1599245325156.webp

After i close the form and reload the page i can see the created poll. Know anybody why i get this error?
 
That's typically a server error due to a security module of some sort, e.g. mod_sec.
 
That's typically a server error due to a security module of some sort, e.g. mod_sec.
Thanks @Brogan. I'll check that. But i'm sure i don't have mod_security installed at apache in my localhost. I get the same error in nginx at my dedicated server development website too.
 
Still developing my addon i get issue after i create a poll and i want to edit the poll again, im out of scope from my variable $node_id .

So i have extended. XF/Pub/Controller/Thread

PHP:
<?php

namespace Yodrak\PhotoPoll\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Thread extends XFCP_Thread

{

    public function actionPollEdit(ParameterBag $params)
    {
        $thread = $this->assertViewableThread($params->thread_id);
        $poll = $thread->Poll;

        $node_id = $thread['node_id'];

        $breadcrumbs = $thread->getBreadcrumbs();

        /** @var \XF\ControllerPlugin\Poll $pollPlugin */
        $pollPlugin = $this->plugin('XF:Poll');
        return $pollPlugin->ppEdit($node_id, $poll, $breadcrumbs);
    }
}

And pass the variable $node_id to XF/ControllerPlugin/Poll

PHP:
    public function ppEdit($node_id, $poll, array $breadcrumbs = [])
    {
        if (!($poll instanceof \XF\Entity\Poll))
        {
            return $this->notFound();
        }

        /** @var \XF\Poll\AbstractHandler $handler */
        $handler = $poll->Handler;
        $contentType = $poll->content_type;
        $content = $poll->Content;

        if (!$poll->canEdit($error))
        {
            return $this->noPermission($error);
        }

        if ($this->isPost())
        {
            $editor = $this->setupPollEdit($poll, $contentType, $content, $handler);
            if (!$editor->validate($errors))
            {
                return $this->error($errors);
            }

            $editor->save();

            return $this->redirect($this->getDynamicRedirect());
        }

        else

        {
            $viewParams = [
                'poll' => $poll,
                'breadcrumbs' => $breadcrumbs,
                'node_id' => $node_id
            ];
            return $this->view('XF:Poll\Edit', 'poll_edit', $viewParams);
        }

    }

If i edit the created poll i can see the variable but if i want to save i get this error:

1599772445539.webp

Can somebody give me a hint, what i do wrong?
 
I have another question. Now i try to extend the column response from the table poll_response.

I have extended XF/Entity/PollResponse as follows.

PHP:
<?php

namespace Yodrak\Polaroid\XF\Entity;

use XF\Mvc\Entity\Structure;

class PollResponse extends XFCP_PollResponse
{
    public static function getStructure(Structure $structure)
    {
        $struct = parent::getStructure($structure);
        $struct->columns['response']['maxLength'] = 250;
        return $struct;
    }
}

And extended the XF/Poll/ResponseEditor also.



PHP:
<?php

namespace Yodrak\Polaroid\XF\Poll;

class ResponseEditor extends XFCP_ResponseEditor
{
    public function saveChanges()
    {
        if (!$this->poll->poll_id)
        {
            throw new \LogicException("Poll must be saved before responses can be saved");
        }

        if (!$this->addResponses && !$this->deleteResponses && !$this->replaceResponses)
        {
            return;
        }

        $db = $this->poll->em()->getDb();
        $existingResponses = $this->existingResponses;

        $db->beginTransaction();

        foreach ($this->deleteResponses AS $responseId)
        {
            $response = $existingResponses[$responseId];
            $response->delete(true, false);
        }

        foreach ($this->replaceResponses AS $responseId => $value)
        {
            $response = $existingResponses[$responseId];
            $response->response = utf8_substr($value, 0, 250);
            $response->save(true, false);
        }

        foreach ($this->addResponses AS $value)
        {
            $response = \XF::em()->create('XF:PollResponse');
            $response->poll_id = $this->poll->poll_id;
            $response->response = utf8_substr($value, 0, 250);
            $response->save(true, false);
        }

        $db->commit();

        $this->poll->clearCache('Responses');
    }
}

But unfortunately it isn't working.

I can type more then the 100 Chars, but after the form save, the
String is cutted at 100 chars.

Have anybody a glue where i need to extend this?
 
If i change manually the row in /XF/Poll/ResponseEditor to
$response->response = utf8_substr($value, 0, 250);
then im able to save more as 100 chars. But if i try to override the function it is not working.
 
Top Bottom