0

I have theme with custom content types, views, etc. and i would like to package into a custom module so it can be easy installed in another Drupal instance via MODULE_install().

Currently, Module_install() looks something like this:

function MODULE_install(){
    $t = get_t();
    foreach (_jobs_installed_fields() as $field) {
        field_create_field($field);
        drupal_set_message($t('Field type: '.$field['field_name'] . ' created'));
    }

    foreach (_jobs_installed_field_instances() as $field) {
        field_create_instance($field);
        drupal_set_message($t('Field instance: '.$field['label'].' initiated and added to User Profile'));
    }

    drupal_set_message($t('Finished adding fields to User profile'));
}

function _jobs_installed_fields() {
    return array(
        'field_name' => array(
           ...
        ),
        'field_name2' => array(
           ...
        ),
     );
}


function _jobs_installed_field_instances() {
    return array(
        'field_name' => array(
           ...
        ),
        'field_name2' => array(
           ...
        ),
     );
}

The content of _jobs_installed_field_instances and _jobs_installed_fields functions is generated via PHP Execute Script Dev module block from the current instance content and then copy/paste in the install script.

$entity_type = 'user';
$field_name = 'field_name';
$bundle_name = 'user';

$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>')

This can be lot of work for larger Themes and i cannot rerun it later once the change is introduced. I was thinking creating some Php scripts that would generate me the install script from the current nodes, views, etc so i can rerun as like in future. Is there anything like this already available? What would be your approach to this problem? Thank you

5
  • You mean, you want to create Features?
    – Mołot
    Commented Dec 10, 2013 at 21:53
  • Thank You Motot. Not exactly. I have new theme it in its own folder but this theme depends on Content Type, Views that also need to be present from the theme to properly function. So I would have a separate custom module that all will do is create these content types, views on install
    – latvian
    Commented Dec 11, 2013 at 3:16
  • But with features you can do that, you can only select views and content types to be exported.
    – Mołot
    Commented Dec 11, 2013 at 7:32
  • Ah Motot...you made my day! The Features module looks exactly what i am looking for. I am sorry I didn't got the first time you mentioned but Thank You for pointing out again. You my hero!
    – latvian
    Commented Dec 11, 2013 at 13:35
  • Posted it as an answer - if it is really what you need, no point in keeping your question on "unanswered" list.
    – Mołot
    Commented Dec 11, 2013 at 14:05

1 Answer 1

1

Looks like Features module is what you really want:

The features module enables the capture and management of features in Drupal. A feature is a collection of Drupal entities which taken together satisfy a certain use-case.

Features provides a UI and API for taking different site building components from modules with exportables and bundling them together in a single feature module. A feature module is like any other Drupal module except that it declares its components (e.g. views, contexts, CCK fields, etc.) in its .info file so that it can be checked, updated, or reverted programmatically.

With addition of UUID Features Integration:

The UUID Features Integration module provides a mechanism for exporting content (nodes, taxonomy, fields) into a features module. What's that you say? You thought features was only for configuration? This module is meant to be used in the cases where certain pieces of content straddle the line between pure content and configuration.

Of course you will probably only use a subset of it's capabilities, but it should cover all your needs with content types, nodes, and views definitions.

2
  • Motot, the feature works well for exporting content structures(content types, views, etc), but what would i use for exporting content itself (Nodes). i have some test data that i would like to package in another module as well. Thank you
    – latvian
    Commented Dec 11, 2013 at 14:58
  • @latvian answer updated.
    – Mołot
    Commented Dec 11, 2013 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.