Related Content
Drupal 11: Finding A Better Way To Display Code Examples
I've been running this site for about 18 years now and the code I post has been in much the same format since it was started. The code is rendered onto the page using a <code> element and (mostly) syntax highlighted using a JavaScript plugin.
Drupal 11: Theming The Search API Search Input
A common request I see when theming Search API forms is to swap out the normal submit element with a magnifying glass icon. Performing this action isn't difficult, but it does require adding a couple of operations to add a suggestion so a custom template can be used.
Drupal 11: Controlling LED Lights Using A REST Service
Following on from my article looking at the Pimoroni Plasma 2350 W I decided to do something interesting with the Wifi interface that would connect to a Drupal site.
Drupal 11: How To Alter Entity View Builder Configuration Before Rendering
I encountered an issue on a Drupal 11 site recently where I had a block that was rendering an entity to display on a page.
DrupalCamp Scotland 2025
This year, DrupalCamp Scotland was held on the 7th November, at the University of Edinburgh.
On the morning of the conference I made the quick walk from by bed and breakfast and arrived at 50 George Square to join in with around 60 attendees to a day of talks and chatting.
Drupal 11: Programmatically Change A Layout Paragraphs Layout
The Layout Paragraphs module is a great way of combining the flexibility of the layout system with the content component sytem of the Paragraphs module.
Comments
I'm stumped because, theoretically, the $nid isn't created nor available at the time when the page loads.
I'd like to use it to db_insert into a field belonging to a table different than {node} - - - - so that I can perform joins in future queries
Many thnx for your time and consideration
-Ben
Submitted by mcferren on Mon, 12/05/2011 - 02:24
You are correct. The nid isn't available in the form until the node has been saved, which would mean that the form is in the edit state. In this case the node object is kept in the $form['node']->nid parameter.
What you need is to create a hook that will intercept the information after it is saved. This would probably be hook_insert($node), which gets passed the node information after it has been saved and so contains the nid you want.
Of course, you'll probably want to create another hook (something like hook_load()) that will load the information into the node object that you saved in the previous hook.
I hope that helps :)
Submitted by philipnorton42 on Mon, 12/05/2011 - 09:16
Hi Philip,
Many thnx for your guidance! It definitely opened up doors for me to further research. I took your advice and understand how hook_node_insert will perform what I am looking for. I noticed that hook_insert works so long that I use it in the same module that defines the the node type. However, I am planning on using module [B] to add fields to a form that has been created in module [A] - - module[A].install being where the node type is created & module[B] is where I plan to put the hook_node_insert.
I've been reading about creating node types programatically. In my readings, they recommend creating instances (and the fields they align with) inside the install file.
// Create all the fields we are adding to our content type.
foreach (_node_example_installed_fields() as $field) {
field_create_field($field);
}
// Create all the instances for our fields.
foreach (_node_example_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $avatard['type'];
field_create_instance($instance);
}
My question is this:
If I have one module[A] where I create the node type and another module[B] that has a hook_form_alter that adds field(s) to the form_id of the node creation form in module[A] ,,,,
,,,to define the table structure that I need to hook_node_insert into, would I create an additional instance in module[B].install or should I have preplanned (when creating module[A].install) and added those extra instances in module[A].install - - - even though the instances would be orphaned until the hook_form_alter in module[B] comes into play?
Or should I bypass this complexity and just use hook_schema in the .install file of module[B]?
Many thnx for taking the time to help.
-Ben
Submitted by mcferren on Thu, 12/08/2011 - 03:02
Thank you
Submitted by Anonymous on Sat, 03/31/2012 - 13:10
Add new comment