Hi,
I've created a button link to create a meeting entity directly on student profile page. It will take node id from current student page to add it to prepopulate modüle link automatically.

<a href="https://example.com/node/add/meeting?edit[field_student][widget][0][target_id]=[current-page:node:nid] "target="_blank">New Meeting</a>

I'm trying to get student node ID. I can get current page node:id with token(patch is working) on other part of website. But I couldn't get with prepopulate module.

How can I get node ID instead of manual adding on url. Is it possible to use with prepopulate module or Do I have to do something else?
Thanks

Comments

toprak created an issue. See original summary.

simplified-computing’s picture

same thing here, did you get a solution?

toprak’s picture

Unfortunately, no.
You may consider to use Field Clone with eva module as an alternative.

scottsawyer’s picture

I am not sure in what context you are creating this link, so I'll assume it's either something you are either generating yourself or it's something coming from a field.

In case it's just a link field, you should be able to specify the token as part of the default value using something like https://www.drupal.org/project/field_default_token.

In case it's something you are generating in either twig or a preprocess function, you could do something like:

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_preprocess_node()
 */
function my_theme_preprocess_node(array &$variables) {
  $nid = NULL;
  $node = Drupal::routeMatch()->getParameter('node');
  // Check that we have a node ( revisions return NID ).
  if ($node instanceof \Drupal\node\NodeInterface ) {
    $nid = $node->id();
  }
  elseif (is_int($node)) { 
    $nid = $node;
  }
  
  if ($nid !== NULL) {
    $variables['current_nid'] = $nid;
  }

}

Then in your Twig, you could do something like:

{% if current_nid %}
  <a href="https://example.com/node/add/meeting?edit[field_student][widget][0][target_id]={{ current_nid }}" "target="_blank">New Meeting</a>
{% endif %}

UPDATE:
Duh, forgot, you can also use a Markup Field with the patch in this thread.

simplified-computing’s picture

content type: student
content type: meeting, with reference field "field_student"

meetings is a view (block) on student node with contextual filter: field "field_student", provide default value, content id from url

to create a new meeting node with prepopulate
1. add a view display to meetings view (type attachment)
2. change filter to: Content: Content type (= Student)
2. add a field: Content: ID [hidden]
3. add a field: Global: Custom text
<a href="/node/add/meeting?edit[field_student][widget][0][target_id]={{ nid }}">New Meeting</a>
5. set "Inherit contextual filters: No"
4. replace the contextual filter "field_student" with "Content: ID" for the attached view display
5. attach to: Block

toprak’s picture

Status: Active » Fixed

I gave up two months ago, but I decided to try again.
Thank you @simplified-computing. Your way is easier for me. It Works.

Also, thank you @scottsawyer for help .

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.