Blog

How to add file upload ability in comment form in Drupal 7

It is really simple question!
In Drupal 7 we can add any type of content fields to the comments entity, as well as to nodes or users.
Lets take a look on the picture below:

How to transfer the content type settings by Features?

In the post How to Create Content Type in Drupal 7 Programmatically I told about creating content type with using .install file.

But more often during development, we are dealing with a situation where some type of content is created in the admin to set up a specific field. These settings are made on staging while production site lived his own life, filled with new content and users. Obviously, it would be inexcusable mistake to overwrite production DB by staging ones.

On the other hand, we can transfer all the settings manually, but it takes a long time, moreover, there is no guarantee that we do not forget anything.

A simple solution to this issue is to use Features module.

Lets we have the type of content portfolio (admin/structure/types/manage/portfolio/fields):

We should be sure that module Features exitsts and enabled.

Lets go Administration » Structure » Features
and click "Create Feature" tab.

We should input "Name" and "Description" of our new feature. Also you can see "Version" and "URL of update XML", do not pay attention on them. Just now we are not going to provide our feature to somebody and make some new versions.

Lets go ahead! Take a look on the second fieldset. We should select content type that we would like to export. On the right we can see dependencies.

After we press download the feature will be ready for downloading.
In the current case got portfolio_content_type.tar

If you will take a look on content of this archive you can find module.
Pay attention to the .info file. Module has dependencies:

dependencies[] = "colorbox"
dependencies[] = "features"
dependencies[] = "image"
dependencies[] = "strongarm"

So on the site, where you would like to copy content type, these modules should be enabled.

Copy this module to the other site and you can see it in the list of modules:

But you should not turn on it here. Please, go to Administration » Structure » Features:

You can see the list of all features available. On this cas there is only one Feature.
You can turn on it to click on "Disabled". And new content type with corresponding field settings will appear!

Good! It is more simple way to create content on the site by one click that I told in the topic How to Create Content Type in Drupal 7 Programmatically. But it is feature based method, in the post that I mention content type was created without Features module, it is more native way, more preferable for the Code Driven Development approach.

How to Create Content Type in Drupal 7 Programmatically

Please, take a look on node_example module (http://drupal.org/project/examples)
There is a good example in this module, but I'm try to stop only on simple content type adding without any fields as extensions.

The code that will be created new content type we should add into .install file.

Lets add hook_install():

<?php
function your_module_name_install() {
 
// use get_t() to get the name of our localization function for translation
  // during install, when t() is not available.
 
$t = get_t();

 
// Define the node type.
 
$node_example = array(
   
'type' => 'node_example',
   
'name' => $t('Example Node'),
   
'base' => 'node_content',
   
'description' => $t('This is an example node type with a few fields.'),
   
'body_label' => $t('Example Description')
  );

 
// Complete the node type definition by setting any defaults not explicitly
  // declared above.
  // http://api.drupal.org/api/function/node_type_set_defaults/7
 
$content_type = node_type_set_defaults($node_example);
 
node_add_body_field($content_type);

 
// Save the content type
 
node_type_save($content_type);
}
?>

We should not add
node_types_rebuild();
menu_rebuild();
in the code of your_module_name_install() because rebuild will perfome automatically.

According to the rules of good Drupal code, we should to make drupal message and write this event to the log:

<?php
function your_module_name_install() {
 
$t = get_t();
 
$node_example = array(
   
'type' => 'node_example',
   
'name' => $t('Example Node'),
   
'base' => 'node_content',
   
'description' => $t('This is an example node type with a few fields.'),
   
'body_label' => $t('Example Description')
  );
 
$content_type = node_type_set_defaults($node_example);
 
node_add_body_field($content_type);
// Check if we create content type or update.
 
$status = node_type_save($content_type);
// Replacement rule for the messages.
 
$t_args = array('%name' => $content_type->name);
  if (
$status == SAVED_UPDATED) { // update case
   
drupal_set_message($t('The content type %name has been updated.', $t_args));
  }
  elseif (
$status == SAVED_NEW) { // create case
   
drupal_set_message($t('The content type %name has been added.', $t_args));
   
watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types'));
  }
}
?>

Ok, and we also should provide hook_uninstall() to remove our content type:

<?php
function your_module_name_uninstall() {
 
// Gather all the example content that might have been created while this
  // module was enabled.  Simple selects still use db_query().
  // http://api.drupal.org/api/function/db_query/7
 
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
 
$result = db_query($sql, array(':type' => 'node_example'));
 
$nids = array();
  foreach (
$result as $row) {
   
$nids[] = $row->nid;
  }
 
// Delete all the nodes at once
  // http://api.drupal.org/api/function/node_delete_multiple/7
 
node_delete_multiple($nids);
 
// Delete our content type
  // http://api.drupal.org/api/function/node_type_delete/7
 
node_type_delete('node_example');
}
?>

I should mention that if we will not delete all nodes of such content type the function node_type_delete() will not run.

See also How to transfer the content type settings by Features?

Pages