Drupal 7 Install Profile
In the previous article we wrote about creating install profiles for Drupal 6. In this article it'll be described how to create install profile for Drupal 7, its updates and examples.
Profile structure is the following:
- my_profile
- libraries
- modules
- my_profile.info
- my_profile.install
- my_profile.profile
As we see from above-mentioned structure as opposed to Drupal 6, install profile in Drupal 7 reminds of a module with its .info and .*install files.
Important! Because install profile is more like a module now, the folder in which it'll be placed, must have the same name as profile name. In this case my_profile.
Let's look at creation of each file of install profile step by step.
1. Creating .info.
This file is identic to .info file of any module.
Example of my_profile.info
<?php
; $Id$
name = My Profile
description = Install My profile.
version = VERSION
core = 7.x
dependencies[] = block
dependencies[] = color
dependencies[] = comment
dependencies[] = contextual
dependencies[] = dashboard
dependencies[] = dblog
dependencies[] = field
dependencies[] = field_sql_storage
dependencies[] = field_ui
dependencies[] = filter
dependencies[] = image
dependencies[] = list
dependencies[] = menu
dependencies[] = node
dependencies[] = number
dependencies[] = options
dependencies[] = overlay
dependencies[] = path
dependencies[] = php
dependencies[] = rdf
dependencies[] = search
dependencies[] = shortcut
dependencies[] = system
dependencies[] = taxonomy
dependencies[] = simpletest
dependencies[] = text
dependencies[] = user
dependencies[] = field_group
dependencies[] = flag
dependencies[] = flag_actions
dependencies[] = rules
dependencies[] = rules_admin
dependencies[] = wysiwyg
files[] = my_profile.profile
?>First 4 lines show us nothing new.
name - profile name that will be shown in the list of available install profiles.
description - the description that will be shown under profile name.

dependencies - array of names of modules that must be enabled during installation process.
This allows to indicate necessary modules in an easier way because there is no need now to worry about situating modules as it was in Drupal 6 where main modules had to be described first and then dependant ones.
files - full file name of install profile. In this case my_profile.profile.
2. Creating .profile.
In Drupal 7 this file doesn't play the role of the main function as it was in Drupal 6. In this case it gives the name for site My profile. But it's necessary to have this file in install profile.
<?php
// $Id$
/**
* Implements hook_form_alter().
*
* Allows the profile to alter the site configuration form.
*/
function my_profile_form_install_configure_form_alter(&$form, $form_state) {
// Set a default name for the dev site.
$form['site_information']['site_name']['#default_value'] = t('My Profile');
}
?>3. Creating .install.
In Drupal 7 this file plays the main role, right here all actions of installation process are described, for example, creation of roles, types of content and taxonomy vocabularies. The whole file consists of main function my_profile_install() and depending on requirements, of additional functions, unlike in Drupal 6 where all below-mentioned actions were executed in several functions.
So let's look at content of my_profile_install() function step by step.
3.1. Creating input formats.
<?php
$filtered_html_format = array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(
// URL filter.
'filter_url' => array(
'weight' => 0,
'status' => 1,
),
// HTML filter.
'filter_html' => array(
'weight' => 1,
'status' => 1,
),
// Line break filter.
'filter_autop' => array(
'weight' => 2,
'status' => 1,
),
// HTML corrector filter.
'filter_htmlcorrector' => array(
'weight' => 10,
'status' => 1,
),
),
);
$filtered_html_format = (object) $filtered_html_format;
filter_format_save($filtered_html_format);
?>full html format is created the same way.
3.2. Enabling blocks.
<?php
$default_theme = variable_get('theme_default', 'bartik');
$admin_theme = 'seven';
$values = array(
array(
'module' => 'system', //module that generates this block
'delta' => 'main',
'theme' => $default_theme, //theme in which this block will be displayed
'status' => 1, //1 - block is enabled, 0 - disabled
'weight' => 0,
'region' => 'content', //region, in which block will be dispalyed
'pages' => '', //pages on which block should be displayed
'cache' => -1, //-1 - not to cache
),
);
$query =
?>In this code enabling of only 1 block is described. To enable other blocks, you just need to add necessary arrays with settings into $values array.
3.3. Creating types of content.
<?php
$types = array(
array(
'type' => 'page',
'name' => st('Basic page'),
'base' => 'node_content',
'description' => st("Use <em>basic pages</em> for your static content, such as an 'About us' page."),
'custom' => 1,
'modified' => 1,
'locked' => 0,
),
);
foreach ($types as $type) {
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
?>If necessary it's also possible to enable some options of content type, for example, displaying on the main page or possibility to comment.
<?php
variable_set('node_options_page', array('status')); //after being created node will be only published('status')
variable_set('comment_page', COMMENT_NODE_HIDDEN);//hiding comment function
variable_set('node_submitted_page', FALSE);//disabling information about author when node is looked at
?>3.4. Creating taxonomy vocabulary.
<?php
$description = st('Tags vocabulary');
$vocabulary = (object) array(
'name' => 'Tags',
'description' => $description,
'machine_name' => 'tags',
'help' => '',
);
taxonomy_vocabulary_save($vocabulary);
?>If necessary terms for previously created vocabulary can be created.
<?php
//indicating vocabulary id
$vocabulary_id = db_select('taxonomy_vocabulary', 't')
->condition('t.machine_name','catalog', '=')
->fields('t', array('vid'))
->execute()
->fetchField();
$term = new stdClass;
$term->vid = $vocabulary_id;
$term->name = 'Tag1';
taxonomy_term_save($term);
?>3.5. Creating fields.
2 functions are used for that in new Field API:
field_create_field() - to create fields
field_create_instance() - to connect field to entity.
<?php
$field = array(
'translatable' => 1,
'settings' => array(
'max_length' => 60,
),
'storage' => array(
'type' => 'field_sql_storage',
'settings' => array(),
),
'field_name' => 'field_weigth',
'type' => 'text',
'module' => 'text',
'active' => 1,
'locked' => 0,
'cardinality' => 1,
);
field_create_field($field);
$instance = array(
'label' => 'Example',
'widget' => array(
'weight' => 1,
'type' => 'text_textfield',
'module' => 'text',
'active' => 1,
'settings' => array(
'size' => 30
)
),
'settings' => array(
'text_processing' => 0
),
//settings of field display
'display' => array(
'default' => array(
'label' => 'above',
'type' => 'text_default',
'settings' => array(),
'module' => 'text',
'weight' => 2,
),
'node_teaser' => array(
'type' => 'hidden',
'label' => 'above',
'settings' => array(),
'weight' => 0,
),
'line_item' => array(
'type' => 'hidden',
'label' => 'above',
'settings' => array(),
'weight' => 0,
),
),
'required' => 0,
'field_name' => 'field_weigth',
'entity_type' => 'node',
'bundle' => 'page'
);
field_create_instance($instance);
?>In this part of code creation of text field is described.
3.6. Saving settings of WYSIWYG.
<?php
$format = 'full_html';
$editor = 'tinymce';
$settings = array(
'default' => 1,
'user_choose' => 0,
'show_toggle' => 1,
'theme' => 'advanced',
'language' => 'en',
'buttons' => array(
'default' => array(
'bold' => 1,
'italic' => 1,
'underline' => 1,
'justifyleft' => 1,
'justifycenter' => 1,
'justifyright' => 1,
'justifyfull' => 1,
'bullist' => 1,
'numlist' => 1,
'outdent' => 1,
'indent' => 1,
'link' => 1,
'unlink' => 1,
'anchor' => 1,
'image' => 1,
'cleanup' => 1,
'forecolor' => 1,
'backcolor' => 1,
'blockquote' => 1,
'cut' => 1,
'copy' => 1,
'paste' => 1,
'removeformat' => 1,
),
'directionality' => array(
'ltr' => 1,
'rtl' => 1,
),
'font' => array(
'fontselect' => 1,
'fontsizeselect' => 1,
'styleselect' => 1,
),
'insertdatetime' => array(
'insertdate' => 1,
'inserttime' => 1,
),
'paste' => array(
'pastetext' => 1,
'pasteword' => 1,
),
'searchreplace' => array(
'search' => 1,
),
'table' => array(
'tablecontrols' => 1,
),
'drupal' => array(
'break' => 1,
),
),
'toolbar_loc' => 'top',
'toolbar_align' => 'left',
'path_loc' => 'bottom',
'resizing' => 1,
'verify_html' => 1,
'preformatted' => 0,
'convert_fonts_to_spans' => 1,
'remove_linebreaks' => 1,
'apply_source_formatting' => 0,
'paste_auto_cleanup_on_paste' => 0,
'block_formats' => 'p,address,pre,h2,h3,h4,h5,h6,div',
'css_setting' => 'theme',
'css_path' => '',
'css_classes' => '',
);
db_insert('wysiwyg')
->fields(
array(
'format' => $format,
'editor' => $editor,
'settings' => serialize($settings),
)
)
->execute();
?>Settings of any other format is saved the same way.
3.7. Configuring access permission.
<?php
$filtered_html_permission = filter_permission_name($filtered_html_format);
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content', 'access comments', $filtered_html_permission));
user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('access content', 'access comments', 'post comments', 'skip comment approval', $filtered_html_permission));
?>3.8. Creating roles.
In this part of code creation of administator role is done.
<?php
$admin_role = new stdClass();
$admin_role->name = 'administrator';
$admin_role->weight = 2;
user_role_save($admin_role);
user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));
variable_set('user_admin_role', $admin_role->rid); //
db_insert('users_roles')
->fields(array('uid' => 1, 'rid' => $admin_role->rid))
->execute();
?>3.9. Configuring theme.
<?php
db_update('system')
->fields(array('status' => 1))
->condition('type', 'theme')
->condition('name', 'seven')
->execute();
variable_set('admin_theme', 'seven');
variable_set('node_admin_theme', '1');
?>4. Preparing files.
All modules indicated in .info file must be present in profile folder, in modules folder. For WUSIWYG module in libraries folder it's also necessary to have a folder with editor.
There might be a need to create rules, field group or flags. Next article will cover that.


