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 here:

<?php 
/**
  *@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'));
  }
}

The Drupal version is 8.6.15. PHP Version is 7.0.15 Can anyone spot an error I am missing?

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

  • 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) ... – Finn May 25 at 18:16
  • "Call to a member function id() on null" it is mean that "\Drupal::routeMatch()->getParameter('node');" returns NULL. – Sergey Serov May 25 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. – Sergey Serov May 25 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."

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.