I am working through a Drupal 8 module building exercise from YouTube and I am not able to figure out what is causing an error. The relevant page loads and displays the form, but I get the following error message.
Notice: Trying to get property of non-object in Drupal\rsvplist\Form\RSVPForm->buildForm() (line 29 of modules/custom/rsvplist/src/Form/RSVPForm.php).
Drupal\rsvplist\Form\RSVPForm->buildForm(Array, Object) call_user_func_array(Array, Array) (Line: 518)
Drupal\Core\Form\FormBuilder->retrieveForm('rsvplist_email_form', Object) (Line: 275)
Drupal\Core\Form\FormBuilder->buildForm('rsvplist_email_form', Object) (Line: 93)
Drupal\Core\Controller\FormController->getContentResult(Object, Object) call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
The code for the form is the following.
/**
*@file
*Contains \Drupal\rsvplist\Form\RSVPForm
*/
namespace Drupal\rsvplist\Form;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides an RSVP email form
*/
class RSVPForm extends FormBase {
/**
* (@inheritdoc)
*/
public function getFormId() {
return 'rsvplist_email_form';
}
/**
* (@inheritdoc)
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->nid->value;
$form['email'] = array(
'#title' => t('email address'),
'#type' => 'textfield',
'#size' => 25,
'#description' => t("We will send updates to the email address you provide"),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('RSVP'),
);
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $nid,
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
drupal_set_message(t('The form is working'));
}
}
I am using Drupal 8.6.15 which runs on PHP 7.0.15.
Can anyone spot an error I am missing?