XF 2.0 Datalist Toggle

Lukas W.

Formerly katsulynx
I'm trying to utilize the toggle ControllerPlugin to toggle a custom database column, but whatever I do, the request doesn't go through and throws an error instead.

The call itself isn't anything fancy:
public function actionMemberToggle() {
    /** @var \XF\ControllerPlugin\Toggle $plugin */
    $plugin = $this->plugin('XF:Toggle');
    $return = $plugin->actionToggle('RPGS:RPGmembership', 'can_create_characters');
    return $return;
This is the error that is thrown:

ErrorException: array_key_exists() expects parameter 2 to be array, integer given in src\XF\Mvc\Entity\Finder.php at line 431
array_key_exists() in src\XF\Mvc\Entity\Finder.php at line 431
XF\Mvc\Entity\Finder->whereIds() in src\XF\Mvc\Entity\Manager.php at line 188
XF\Mvc\Entity\Manager->findByIds() in src\XF\ControllerPlugin\Toggle.php at line 29
XF\ControllerPlugin\Toggle->toggle() in src\XF\ControllerPlugin\Toggle.php at line 11
XF\ControllerPlugin\Toggle->actionToggle() in src\addons\RPGS\Pub\Controller\RPG.php at line 36
RPGS\Pub\Controller\RPG->actionMemberToggle() in src\XF\Mvc\Dispatcher.php at line 249
XF\Mvc\Dispatcher->dispatchClass() in src\XF\Mvc\Dispatcher.php at line 89
XF\Mvc\Dispatcher->dispatchLoop() in src\XF\Mvc\Dispatcher.php at line 41
XF\Mvc\Dispatcher->run() in src\XF\App.php at line 1787
XF\App->run() in src\XF.php at line 326
XF::runApp() in index.php at line 13
And this is the template snippet responsible for toggling:
<xf:toggle name="can_create_characters[{$member.user_id}]" selected="$member.can_create_characters"
           class="dataList-cell--separated" submit="true"
           tooltip="{{ phrase('rpgs_can_create_character') }}" />
If someone can see the mistake/error, I'd be glad to hear it.

Lukas W.

Formerly katsulynx
Yes, I do have a combined primary key. Now that you mention it, that could've been obvious. Any way to ship around that? The rpg_id is available in all places mentioned above, so adding it somewhere isn't a hurdle.

        $structure->primaryKey = ['user_id', 'rpg_id'];
        $structure->columns = [
            'user_id' => ['type' => self::UINT, 'required' => true, 'default' => \XF::visitor()->user_id],
            'rpg_id' => ['type' => self::UINT, 'required' => true],
            'membership_state' => ['type' => self::STR, 'default' => 'pending',
                            'allowedValues' => ['invited', 'pending', 'member', 'moderator']
            'join_date' => ['type' => self::UINT, 'default' => \XF::$time],
            'can_create_characters' => ['type' => self::BOOL, 'default' => 0]

Liam W

Well-known member
I don't think there is with the toggle element... the code in the helper just gets the keys from the sent array in the toggle name and passes them to the whereIds finder method.

You could probably do it with a hidden input, and re-implementing the toggle action in your controller though.


Lukas W.

Formerly katsulynx
Hm, unfortunate. But as I'm toggling the membership_state between two states at the same time, I guess it doesn't hurt to add that there. Thanks for the help!