Categories

(83)
(69)
(8)
(34)
(74)
(149)

Drupal 7 Install Profile

02.06.2011
Drupal 7 Install Profile
Author:

From the previous article, the web developers got to know how to create install profiles for Drupal 6. In this article we will elaborate on creating install profile for Drupal 7, the newest updates to the program and consider the relevant examples.

The profile structure is the following:

  • my_profile
  • libraries
  • modules
  • my_profile.info
  • my_profile.install
  • my_profile.profile

As we can see from the above-mentioned structure, as opposed to Drupal 6, install profile in Drupal 7 resembles 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 the profile name. In this case, my_profile.

Now we are going to investigate, step by step, how each of the files in the installation profile is being created.

1. Creating .info.

This file is similar to .info file of any module.

Below follows the 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

There is not much novelty in the creation procedure in the first 4 lines.

name - profile name that will be shown in the list of available installation profiles.

description - the description that will be shown under profile name.

Dependencies - array of names of modules that must be enabled during installation process.

The above allows for the simpler indication of required modules, for there is no need now to secure the strict order for the modules like it was in Drupal 6 where the main modules had to be described first, and the dependent ones were to follow.

Files - full file name of install profile. In this case, my_profile.profile.

2. Creating .profile.

In Drupal 7 this file doesn't perform the role of the main function as it happened in Drupal 6. Now it is specifying the name for the site My profile. But it's necessary to have this file in the installation 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 has a key role, for right here the descriptions for all installation process are to be kept, like in the case of roles creation, 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 investigate the 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 =

There's only a description of enabling procedure for one block in this code. In order 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 requied, it's also possible to enable some options of content type like those for displaying it on the main page or the leaving comments option.

 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, new term entries for the previously created vocabulary can be entered.

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 the 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 the administator role creation is being realized.

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 procedure.

 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 the profile folder, and later, in the modules folder. It's also necessary, for the sake of WUSIWYG moduleto to have an editor folder in the libraries folder.

It may as well happen that rules, field group or flags will have to be created.

6 votes, Rating: 5

Read also

1

In Drupal 7 API there is a chapter dedicated to site theming. All elements of site, without exclusion, should go through the theming process. But first of all theming itself should be set up.

2

In this post the bases of creating environment for Drupal projects development on the basis of Debian 6 "Squeeze" will be described. Having this, everyone will be able to practice setting OS...

3

Being involved in development process one has got to realize actions/commands for which he has not been granted enough rights or the user whose server has launched and executed Drupal, is lacking...

4

One of Drupal 7's major advantages over its precursors is its flexibility with settings and systems. Drupal developers haven't bypassed DB queries, either. In this release the latter have become...

5

Late in April of the current year Google Analytics representatives have announced in their official blog about launching of beta-testing of new interface. In this article I want to share about the...

Subscribe to our blog updates