0

I am new to drupal 8 and assisting my development team with design part. We are stuck in one place and not manage to move forward. We want to pass the node ID info to a webform filed which exist in another page. In the attached image I have explained what we want to achieve in Drupal 8 Environment, previously with PHP hack it was possible in drupal 7 but no idea how to achieve the same in drupal 8.

enter image description here

0

Assuming you want: The user will create a node. Upon submission a form will be displayed showing the node id pre-filled in a form. Make a module called example:

// example.module:
// we created a node type "product" (using UI). Saving this content
// will trigger the product form
/**
 * Implements hook_ENTITY_TYPE_insert().
 * @param \Drupal\node\NodeInterface
 */
function example_node_insert(NodeInterface $node) {
  if ($node->bundle() == 'product') {
    $nid = $node->id();
    $response = new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal\Core\Url::
    fromRoute('product.form', ['nid' => $nid])->toString());
    $response->send(); // When OO we will return the response object instead
  }
}

// example.routing.yml
product.form:
  path: '/form/product/{nid}'
  defaults:
    _form: '\Drupal\example\Form\ProductForm'
    _title: 'Product Form'
  requirements:
    _permission: 'access content'

// src/Form/ProductForm
<?php

namespace Drupal\example\Form;


use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;

class ProductForm extends FormBase {

  /**
   * @var NodeInterface
   */
  protected $node;

  public function getFormId() {
    return 'example_product_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state, $nid = null) {
    // if you need to load the node
    // $this->node = NodeInterface::load($nid);

    $form['node_id'] = [
      '#type' => 'textfield',
      '#title' => 'Node ID',
      '#value' => $nid,
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    // TODO: Implement submitForm() method.
    // you can redirect back to the node
  }

}

When dealing with this issue please realize the node id is not available until you save the node.

0

Webform 8.x-5.x allows elements to be populated using query string parameters but you must enable this feature.

enter image description here

Another approach would be to set the element's default value to a [current-page:query:?] token like [current-page:query:nid]

  • Hi, Thank you for your comments. Actually my form is located in different URL; I need to catch current page title and pass this to my webform which is located in different URL. With your method it is getting the title of my webform page; I want my product page title to be passed in my webform. – user1551692 Mar 28 '17 at 9:31
  • In short, I want to populate my referral page title in my current webform input box. – user1551692 Mar 28 '17 at 9:47
0

Trying using the [webform_submission:node:title] within the confirmation redirect URL's querystring parameter.

enter image description here

BTW, you must use an absolute URL. I just found this bug and created a issue on D.O. https://www.drupal.org/node/2864996

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.