2

I need to reference specific entries from my Sprout Form so that I can grab the data the user uploaded through that form to use to make changes in the database. I am stuck here with this error message: "Trying to get property 'forms' of non-object".

$appFormHandle = 'formHandle';
$appForm = SproutForms::$app->forms->getFormByHandle($appFormHandle);

$appEntry = SproutForms::$app->entries->getEntry($appForm);
echo $appEntry->entryId;

How do I properly reference the form so I can get the data from its entries to use in a plugin?

1

The error indicates that SproutForms::$app is not an object, which would mean it has not been initialized yet. If you look at the source code, you can see that SproutForms::$app is a static property on the SproutForms class that starts out undefined. It's only assigned the current App instance once it's init method has been executed. The static property access (SproutForms::$app) doesn't instantiate the app, so if the plugin hasn't been instantiated yet you could get this error.

You can probably solve this by requesting an instance of the SproutForms plugin using the Plugins service manually to ensure it gets initialized properly before accessing it's $app property:

$SproutForms = \Craft::$app->plugins->getPlugin('sprout-forms');
1
  • 1
    Thank you so much for this suggestion! I am just learning how to work with oop php and this is really helpful. Jul 16 at 17:34
1

Make sure you are importing the SproutForms base class into your php file. The code below assumes you'd be using the namespace of your actual custom module and class:

<?php
namespace modules/mycustommodule;

use barrelstrength\sproutforms\SproutForms;

class MyCustomClass
{
  public function myCustomMethod()
  {
    $formHandle = 'my-form-handle';
    $form = SproutForms::$app->forms->getFormByHandle($formHandle);
  }
}

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.