Save Node's Data to Another Table After Saving Node - drupal-7

I'm building drupal module, i wanna make function that triggered after saving certain content type. example
after saving article, i wanna save article's data to like nid, title, type, status to another table.
I'm stuck here, please help me. this some code I've made:
//using hook_node_insert
//i just wanna save data from content type 'article' only.
function mymodule_node_insert($node) {
db_insert('mytable')->fields(array(
'nid' => $node->nid,
'title' => $node->title,
'type' => $node->type,
'status' => $node->status
))->execute();
}

The Rules module will help you to handle this,
**
The rules module allows administrators to define conditionally
executed actions based on occurring events (known as reactive or ECA
rules). It's a replacement with more features for the trigger module
in core.
**
You can use the "After saving new content" event for the content part.
In this you'll be able to use conditions on your rules to make sure the actions are only fired for your required content types.

Related

Store Custom Wordpress Contact form data into Contact form DB

I created a custom form in Wordpress where user fill some input fields and on submitting the form, now the recepient is getting mail. What I need is when user submits the data, the data should gets stored into the contact form DB. With Contact Form 7 plugin, all the data is stored, but I need to store the custom contact form data into the contact form 7 db.
Can you please help me to find a possible solution for the same?
Finally, found the exact solution.
If we want to programatically push data into the Contact Form DB, there are two basic things you will need to know:
How to struture your form data so that the plugin knows how to consume it
How to call the plugin’s save data function
Data should be structured like this:
$data = (object) array(
'title' => 'form-name',
'posted_data' => array(
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'email' => $_POST['email']);
Where ‘form-name’ is the name of the form, and ‘fname’, ‘lname’, and ’email’ are form fields in this example. Replace them with the fields from your form.
Calling the Plugin
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
$plugin = new CF7DBPlugin();
$plugin->saveFormData(&$data);
OR Use CF7's hook
do_action_ref_array( 'wpcf7_before_send_mail', array( &$data) );
The advantage of using CF's hook is that that we do not need to include the CF7DBPlugin.php file, it is a de-coupled approach. The disadvantage is that any other plugin listening to the hook will also get the data.

Drupal 7: How to draw all widgets for a node-add form dynamically

I'm working on a module in which I need to draw multiple node-add forms, from different content-types, in the same page and save them all at once. I have all the content types of every node I need to add, and I even managed to get the fields using field_info_instances("node", $type).
Now I need to render the fields. I found node_add() but this function creates the entire form, including save buttons and publishing options. I only need the widgets for the fields.
Is there a hook or a function in drupal that will render only the widgets for a node-add form given the content type, or even the widgets for a field given it's info?
R.
PS: I'm working on drupal 7.x
Calling node_add($content_type); will give you the node add form for this specific form type.
I'd think this way
$form = array();
$types = array('page', 'blog', 'article');
foreach ($types as $type) {
$type_form = node_add($type);
// Somehow merge this data with the $form array avoiding the conflicts
// resulting from mulitple fields with same name.
// and find a way to submit all of them with one button .. ajax?
}
return $form;

cakephp calling controller action from model

I have a shopping cart site and on successful purchase I need to send a mail with all product details in the order as attachment. I have used fat model skinny controller approach and all my functions are in model. I have a controller action which will give the order details by passing order id along with view. Using dompdf I can convert this html to pdf and can create a file. So for creating attachment I can use the same function by passing some parameter. My mail sending code is in model. From here I need to call the controller action and need to get the pdf file name that just created. I know calling controller action from model is against MVC architecture. But how can I achieve this functionality ?
'fat' models is a good thing to do, however, try not to put things in a Model that should not be in a Model. In MVC, Models should handle all things related to data.
Fat models
The 'fat' Model concept is to reduce the amount of code in your Controller, by moving data related code to the Model. for example:
In stead of this; (in your Controller):
public function view($id)
{
$this->request->data = $this->SomeModel->find('first', array(
'fields' => array(
// list of fields to retrieve
),
'conditions' => array(
// conditions
),
// etc.
);
}
Move the find instructions to a method inside your model and use this:
public function view($id)
{
$this->request->data = $this->SomeModel->someMethod($id);
}
Other locations to put your code
Code that is not related to data, can also be moved outside your Controller (to make it 'skinny'). CakePHP offers other locations to move your code to, for example inside a Component
Then inside your Controller;
this:
public function view($id)
{
$this->request->data = $this->SomeModel->someMethod($id);
// use functionality of a component
$this->SomeComponent->doSomething();
}
Triggering functionality via Events
To keep code and logic outside your controller, CakePHP 2.x now offers an 'Event' system. This allows you to execute code if a certain event happens. You can pass additional information through the events (the event will become a 'communication channel' that passes through your application).
Sending e-mails for certain events is a good example. The CakePHP also uses sending mails to illustrate the Event system in CakePHP; this is probably what you are looking for:
Dispatching Events - send emails
Make your model method return the data so you have it in the controller and pass it to the other model function together with your pdf related data.

How to get user ID on the registration and add in to custom table

I am trying to get user ID on registration and automatically add that ID to my custom table. I am using wp-members plugin for registration.
So is it possible to get user ID on the fly while registering and add that ID to another custom table with WP-Members plugin.
Or I can use custom registration page if require. But need some guideline how to get user ID on the fly during registration.
I am adding below code to my theme functions.php but nothing works.. I dont know where I am wrong or this code is completely wrong?
function get_customs_id($user_id){
global $wpdb;
$wpdb->insert( 'wp_customs', array( 'customs_id' => 'value1' ), array( '%d' ) );
}
add_filter('user_register', 'get_customs_id');
Please help me to make it works.. :)
With the exception of the fact that 'user_register' is an action hook and not a filter, that should work.
Looking at the date on the question, it's possible that the plugin did not make use of wp_insert_user at that time. I'm not certain. But it does now, so that hook should work.
There is also an action hook specific to the plugin that could be used as well - wpmem_post_register_data. This hook comes after the user is registered and inserted in the db. It receives an array of all of the user fields which includes the id.
So using that hook and your other code, this should work:
function get_customs_id($fields){
global $wpdb;
$wpdb->insert( 'wp_customs', array( 'customs_id' => $fields['ID'] ), array( '%d' ) );
}
add_filter('user_register', 'get_customs_id');

How do I make a custom form module in Drupal and make it appear in the add content type?

Drupal at the moment is making me feel dumb but I am relentlessly pursuing it and need help right now.
My aim is to create a custom form inside table elements. After probing online and on stackoverflow, people recommended I dig deep into the forms api and build my own form module.
Now when I create a module and enable it, I am not able to see it when I click 'Add content type' like I would when I download the webform module and enable it appears automatically on my content creation form.
Thanks!
I do not know what your module has in it but you need to use the hook_node_info() to create a new content type.
Here's the documentation on the drupal website http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_info/7
It should be something like this
/**
* Implements hook_node_info().
*/
function mymodule_node_info() {
return array(
'blog' => array(
'name' => t('MyModule entry'),
'base' => 'mymodule',
'description' => t('Description of content type'),
)
);
}

Resources