0

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?

3 Answers 3

0

Line line 29:

$nid = $node->nid->value;

Try this solution:

$node->id(); 

Very useful thing "Drupal 8 Entity API cheat sheet": https://www.metaltoad.com/blog/drupal-8-entity-api-cheat-sheet

3
  • Thank you for the suggestion. I tried it and I got this error: The website encountered an unexpected error. Please try again later. Error: Call to a member function id() on null 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) ... Commented May 25, 2019 at 18:16
  • "Call to a member function id() on null" it is mean that "\Drupal::routeMatch()->getParameter('node');" returns NULL. Commented May 25, 2019 at 19:34
  • Try to display form only on page with node object, or use default nid for case when form will be on page without node. Commented May 25, 2019 at 19:39
0

Someone else had the same problem - this is the reason:

"In the video the form shows the form with no warnings, but when I try it shows the following warning: Notice: Trying to get property 'nid' of non-object in Drupal\rsvplist\Form\RSVPForm->buildForm()

This is caused by getting $node = \Drupal::routeMatch()->getParameter('node'); in a page with any node. So it returns NULL and fails trying to get the nid from NULL.

So the code should be updated to provide a default nid when $node is NULL. I guess this form will be displayed just on node pages, so the route /rsvplist will not be used. So keep watching the videos."

0

Here is what you should add to your code to avoid the error you have when you're not on a node page (I have improved as well to the latest standards, you might have to adjust since you're using D8 and PHP7) :

$node = \Drupal::routeMatch()->getParameter('node'); $nid = 0; if ($node instanceof NodeInterface) { $nid = $node->id(); } 

Full code below :

<?php /** *@file *Contains \Drupal\rsvplist\Form\RSVPForm */ namespace Drupal\rsvplist\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\node\NodeInterface; /** *Provides an RSVP email form */ class RSVPForm extends FormBase { use StringTranslationTrait; /** * (@inheritdoc) */ public function getFormId(): string { return 'rsvplist_email_form'; } /** * (@inheritdoc) */ public function buildForm(array $form, FormStateInterface $form_state): array { $node = \Drupal::routeMatch()->getParameter('node'); $nid = 0; if ($node instanceof NodeInterface) { $nid = $node->id(); } $form['email'] = array( '#title' => $this->t('email address'), '#type' => 'textfield', '#size' => 25, '#description' => $this->t("We will send updates to the email address you provide"), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => $this->t('RSVP'), ); $form['nid'] = array( '#type' => 'hidden', '#value' => $nid, ); return $form; } public function submitForm(array &$form, FormStateInterface $form_state): void { $this->messenger()->addMessage($this->t('The form is working')); } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.