Related Content
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.
Drupal 11: Using SDC Component Library To Preview Single Directory Components
Single Directory Components (SDC) consist of a directory of files that go together to create a small component on a Drupal website. SDC can be nested together, which means that a consistent set of elements can be created and plugged together on a site.
Drupal 11: Using Storybook To Preview Single Directory Components
Single Directory Components (SDC) consist of a directory of files that go together to create a small component on a Drupal website. The component contains all the templates, styles, script, and images needed to display some content in a consistent way.
Drupal 11: Object Oriented Hooks And Hook Service Classes
Hooks are used in Drupal to allow modules and themes to listen to or trigger different sorts of events in a Drupal system. During these events Drupal will pause and ask if any modules want to have any say in the event that is currently being triggered.
LocalGov Drupal Camp 2025
LocalGov Drupal Camp 2025 was held in The Abbey Community Centre (near Westminster Abbey in London) on July 3rd 2025. I travelled down (despite the English rail network's attempts to the contrary) for the day as an attendee.
DrupalCamp England 2025
The first DrupalCamp in England was held this weekend (1st March 2025) in Cambridge. I drove down on the Friday with Chris Maiden to attend (and speak!) at the event.
We arrived early at the event and and started the day with a much needed coffee (or two) and started chatting with the other attendees.
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
PermalinkYou 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
PermalinkHi 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
PermalinkThank you
Submitted by Anonymous on Sat, 03/31/2012 - 13:10
PermalinkAdd new comment