1

After updating to 3.7.19 I ran the command as instructed in the changelog:

php craft utils/repair/section-structure <sectionHandle>

But it returned the following:

Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: craft\console\Request::getBodyParam()'

in /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/Component.php:301

Stack trace:
#0 /home/pfwebco/sos/sos_craft/config/contact-form.php(3): yii\base\Component->__call('getBodyParam', Array)
#1 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Config.php(205): include('/home/pfwebco/s...')
#2 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Plugins.php(967): craft\services\Config->getConfigFromFile('contact-form')
#3 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/services/Plugins.php(233): craft\services\Plugins->createPlugin('contact-form', Array)
#4 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/base/ApplicationTrait.php(1479): craft\services\Plugins->loadPlugins()
#5 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/console/Application.php(48): craft\console\Application->_postInit()
#6 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application->init()
#7 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/base/Application.php(212): yii\base\BaseObject->__construct(Array)
#8 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/console/Application.php(90): yii\base\Application->__construct(Array)
#9 [internal function]: yii\console\Application->__construct(Array)
#10 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/di/Container.php(420): ReflectionClass->newInstanceArgs(Array)
#11 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/di/Container.php(171): yii\di\Container->build('craft\\console\\A...', Array, Array)
#12 /home/pfwebco/sos/sos_craft/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\console\\A...', Array, Array)
#13 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/src/Craft.php(67): yii\BaseYii::createObject(Array, Array)
#14 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/bootstrap/bootstrap.php(242): Craft::createObject(Array)
#15 /home/pfwebco/sos/sos_craft/vendor/craftcms/cms/bootstrap/console.php(51): require('/home/pfwebco/s...')
#16 /home/pfwebco/sos/sos_craft/craft(22): require('/home/pfwebco/s...')
#17 {main}

Although I got that error, would it have done the command and repaired the table and how can I check?

It mentions the contact-form several times, so I'm wondering if there is something I need to edit in the config contact-form following updates. Or is there something else I'm missing? Below is the code in that form.

<?php
$request = Craft::$app->request;
$toEmailId = $request->getBodyParam('toEmail');

$criteria = \craft\elements\Entry::find()
    ->section('contacts')
    ->all();

$allAddresses = [];
foreach($criteria as $entry){
    $allAddresses[$entry->contactKey] = $entry->contactEmail;
}
$toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
return [
    'toEmail' => ($toEmail ?: null),
    'prependSubject'      => '',
    'prependSender'       => 'Orienteering website:',
    'allowAttachments'    => false,
    'successFlashMessage' => 'Thanks for your message'
];

The submit button on the public page sends the data field 'contactKey' as the 'toEmail' value. Then the config contact-form gets all the 'contacts' section entries and then matches the contactKey to the contactEmail which is then passed to the toEmail field. This is so that the email address is not visible in the public html and so hidden from spammers.

That code came from a previous post answered by Robin Schambach

This is how the config/contact-form.php looks after adding the suggested code:

<?php
$request = Craft::$app->request;
if (!$request->getIsConsoleRequest()) {
    $toEmailId = $request->getBodyParam('toEmail');
    // the rest of the configuration code goes here
    
$criteria = \craft\elements\Entry::find()
    ->section('contacts')
    ->all();

$allAddresses = [];
foreach($criteria as $entry){
    $allAddresses[$entry->contactKey] = $entry->contactEmail;
}
$toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
return [
    'toEmail' => ($toEmail ?: null),
    'prependSubject'      => '',
    'prependSender'       => 'Orienteering website:',
    'allowAttachments'    => false,
    'successFlashMessage' => 'Thanks for your message'
];

1 Answer 1

2

The problem is that this config file is for console commands as well as web requests. Console commands differ from web requests in that they don't have body parameters. For web requests, Craft::$app->request is an instance of craft\web\Request. But for console commands, it's an instance of craft\console\Request. This one doesn't have the getBodyParam method, so you get this error.

The solution is to check if the current request is a console request and don't execute the rest of the code if it is:

$request = Craft::$app->request;
if (!$request->getIsConsoleRequest()) {
    $toEmailId = $request->getBodyParam('toEmail');
    // the rest of the configuration code goes here
}

You can see something similar in the documentation on overriding plugin settings for the contact-form plugin.

Update: Complete working code with added condition

<?php
$request = Craft::$app->request;
if (!$request->getIsConsoleRequest()) {
    $toEmailId = $request->getBodyParam('toEmail');

    $criteria = \craft\elements\Entry::find()
        ->section('contacts')
        ->all();

    $allAddresses = [];
    foreach($criteria as $entry) {
        $allAddresses[$entry->contactKey] = $entry->contactEmail;
    }
    $toEmail = (array_key_exists($toEmailId, $allAddresses))? $allAddresses[$toEmailId] : null;
    return [
        'toEmail' => ($toEmail ?: null),
        'prependSubject'      => '',
        'prependSender'       => 'Orienteering website:',
        'allowAttachments'    => false,
        'successFlashMessage' => 'Thanks for your message'
    ];
}
3
  • Thanks for the help, but it's not working. I'm getting a Parse error "syntax error, unexpected end of file", you can see the result here: sos.pfweb.co.uk/contacts I've added the updated code above.
    – Paul Frost
    Dec 7, 2021 at 18:22
  • @PaulFrost You're missing the last closing bracket (}) at the end. I've updated my answer with the full corrected code snippet.
    – MoritzLost
    Dec 8, 2021 at 8:48
  • 1
    Thanks so much for this help, it's all working now. I love Craft and what I can build out of the box, but these little tweaks that need php skills that I don't have just frustrate me so much.
    – Paul Frost
    Dec 8, 2021 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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