1

I have a new route and the yml file looks like this:

rsvplist.form:
  path: '/rsvplist'
  defaults:
    _form: '\Drupal\rsvplist\Form\RSVPForm'
    _title: 'RSVP to this Event'
  requirements:
    _permission: 'access content'

From the module root directory, I have src/Form/RSVPForm.php, and inside is where I have the build form function that looks like the following:

public function buildForm(array $form, FormStateInterface $form_state) {
    $node = \Drupal::routeMatch()->getParameter('node');
    $nid = $node->nid->value;

    $form['email'] = [
        '#title' => t('Email Address'),
        '#type' => 'textfield',
        '#size' => 25,
        '#description' => t('We will send updates to the email you provide.'),
        '#required' => TRUE,
    ];

    $form['submit'] = [
        '#type' => 'submit',
        '#value' => t('RSVP'),
    ];

    return $form;
}

When I clear the site cache and refresh the page: mylocal.com/rsvplist, I see the following error:

Notice: Trying to get property 'nid' of non-object

I initially used the following code to get the node

$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->nid->value;

If I change the last piece of code to this:

$nid = $node->id();

I see the following error:

Error: Call to a member function id() on null

How can I get the node id when am on the new route?

Reference here

2 Answers 2

2

Your route doesn't have a parameter called node, so therefore $node is null.

In order to make node available as a parameter you have to be on a route that has node as a parameter.

So to add the node parameter to your route, you can do like this:

rsvplist.form:
  path: '/rsvplist/{node}'
  defaults:
    _form: '\Drupal\rsvplist\Form\RSVPForm'
    _title: 'RSVP to this Event'
  requirements:
    _permission: 'access content'
    _entity_access: 'node.view'
    node: \d+

And then you can add the $node parameter to your buildForm method, like this:

public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
  // Your form.
}

Since I added node: \d+ to your form, $node is now the loaded entity in the buildForm method.

3
  • I edited my question and added the reference for my code. The route does not have a node parameter Dec 11, 2019 at 8:49
  • 1
    No, that's why I say you need to add it if you want to access it. You can't get a route parameter that is not on your route.
    – user72672
    Dec 11, 2019 at 8:51
  • Jdrupal is right, without a parameter in the route there is no notion of what node you want. The route represents a uri, which is totally independent of any other uri (for example a node view page). It sounds like you want Drupal to just know what page is currently displayed in the user's browser, but that page doesn't even have to be a Drupal page.
    – anonymous
    Dec 11, 2019 at 9:00
-2

You can try code

rsvplist.form:
  path: '/rsvplist/{nid}'
  ....


use Drupal\node\NodeInterface;
....
$nid= \Drupal::routeMatch()->getParameter('nid');
$node = Node::load($nid);
1
  • 4
    No, you should be using Jdrupal's example and allow the routing system to automatically load the node. The upcasting from nid to Node isn't always trivial (e.g. if you have language translations or node revisions etc.) and by doing it through the routing system like Jdrupal shows you have the most flexibility and power to customize the process.
    – anonymous
    Dec 11, 2019 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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