Import/Export Sample Data and Assets for Kickstart All via Features

This is an overview of the custom module – Sample Data developed and used at DesignsSquare.com that was published at Drupal.org for anyone that finds it useful. It is also at github – designssquare-lib-sample-data branch ‘7.x-1.x’

Issue: Widget or Theme needs sample data for kickstart. There is no automatic, easy and standard way to package Sample Data with all of its assets(i.e. images, videos,etc) part of single artifact deliverable for easy install in Drupal.

Solution: Use Features module along with Sample Data module for sample data exports/imports in an automotive manner without another dependency

The common practice to package widgets and themes at DesignsSquare.com delivered to client is by separating the deliverable into 3 parts – code, Structures&Configurations and Sample Data for KickStart all part of one deliverable via Features. The Sample Data part contains sample nodes, menu instances and assets referenced from content, fields and variables.

To automate the packaging process for Sample Data part of the deliverable, we have created Sample Data module. The Sample Data module does the following:

      1. Export/Imports nodes with Alias path
      2. Export/Imports menus based on Alias path
      3. Handle the assets – images, videos, etc for sample data. Specifically:

          A) assets referenced from sample content or other modules located in default public directory(i.e. sites/default/file)
          B) assets referenced by fields(both core type or custom type)
          C) assets referenced by variables
      4. Overrides for context and StormArm variables

Note: assets referenced by custom fields and variables need to use file_managed functionality to work

1. Export/Imports nodes with Alias path

In order to have alias path, you will have to install and enable alias path module – https://www.drupal.org/project/pathauto. Once the sample data have alias path, then export it from features(admin/structure/features). The Sample Data module will hook into the export and store alias path for each node later to restore at the time of import

2. Export/Imports Menus based on Alias path

Once you have the pathauto module enabled, each node will have the alias path used by Sample Data to export and import Menus. Go to Features(admin/structure/features) and look for section “MENU ALIAS” with options to select menues you like to import/export. It will store the menus in the export and then import to the new instance based on alias path. Since it uses alias path, each menu has to have unique links(alias path) to work

3A) Assets referenced from sample content or other modules

Your sample content may be referencing assets. It is also possible that other modules like “imce” for editor is storing assets. The Sample Data module lets you export those assets. Go to Features(admin/structure/features) and look for section “Content Assets”. In this section, it will display all assets(files and directories) from the public directory. Select the ones you like to export and it will store them for later import in the new instance. If you select directory, then it will store the whole tree of the assets

3B) Assets Referenced by Fields

Your sample data(nodes) may have fields of type ‘file’ that is going to reference assets. The Sample Data module will automatically store those assets when you are exporting the sample data(nodes). It will automatically transfer the assets at the time of import in the new instance. The Sample Data module does the same for custom fields referencing assets and not only the Drupal default such as ‘file’. There is one requirement. The ‘file_managed’ functionality needs to be handling the asset in order for the import/export to work

3C) Assets Referenced By Variables

You may have chosen to store asset references in form of FID in the Drupal variable. The Sample Data module will export/import this assets as long as the name of the variable is stored part of the entry in the file_usage table for field(column) – ‘type’:

 //we also add variable name as the 'type' parameter, so we can export via Sample Data module
        file_usage_add($file, 'module_name', 'VARIABLE_NAME', '1');
        // Save.
        file_save($file);

As you see, the variable name is saved part of the usage entry. This is done, so Sample Data module exports the asset

Once the variable is holding reference to the asset and the variable name is stored part of entry in the file_usage table, then in section ‘VARIABLE ASSETS’ in the Features page(admin/structure/features) there is listed available references for you to select to export/import those assets.

4. Overrides for context and StormArm variables

The “CONTEXT OVERRIDE” provides a solution to the fact that features doesn’t let you export something that is already part of another feature. This is an issue if you say have a widget with context that needs to be overridden by site specific context. So by select the context from “CONTEXT OVERRIDE”, it will store those context to import in the new instance

The “STRONGARM OVERRIDE”, besides the ability of overriding another conflicting feature, it also provides a solution to be able export home page, error 404, 403 that is impossible otherwise because it uses the hard coded path(node/id) specific to each Drupal instance. The Sample Data utilizes alias path to import/export home page, error 404 and 403 pages

To summarize, the Sample Data module is one full solution for exporting/importing Sample data for Kickstart data. It exports/imports nodes and menus using alias path. It handles all the assets referenced by content, fields or variables. It also provides ability to override contexts as well as export/import home page, error 404, 403 pages. It does all for you, so you can focus on more important aspects than handling sample data/assets when building widgets,themes and other cool things

Troubleshooting

I am unable to export Content(nodes) via futures

Ensure you have the correct versions for features(7.x-2.0 or above), feature_uuid(7.x-1.0-alpha4…the dev version on July 2014) and uuid(7.x-1.0-alpha5…dev version on July, 2014). Afterwards, enable the content to by exportable at admin/config/content/uuid_features

The specified file public://artifact-name.jpg could not be copied, because the destination directory is not properly configured

Ensure the export feature module has write access and recreate the feature.

7 thoughts on “Import/Export Sample Data and Assets for Kickstart All via Features

  1. Pingback: Handling Assets for Custom Module in Drupal | Margots Kapacs Blog

  2. Thanks again for the update to your Drupal artical on Import/Export of media assets and the additional detail on Import/Export Sample Data. I now understand what my trouble is, I was trying to install on standard Drupal 7, Multi-Site with a traditional theme rather than the Drupal Commerce Kickstart All. Your approach is still the closest and I have all but sample data installed. I am not using Commerce so I believe a module is missing. I see if I can get a list of kickstart models and determine the dependency. It may be Community or such.

  3. Thanks. I tried a frankenstien merge of the the two D7 builds… not pretty and not maintainable. At least in the INC I could see the needed modules. Thanks for these notes.

  4. Pingback: Solving Menu Import Issue when Featuring Sample Data in Drupal | Margots Kapacs Blog

Leave a Reply

Your email address will not be published. Required fields are marked *