Experimental project

This is a sandbox project, which contains experimental code for developer use only.

About

The module implements the following Rules Actions to interact with FS objects - directories and files:

  • Open directory
  • Create directory
  • Delete directory
  • Find directories
  • Find files
  • Copy file
  • Save file to the database;

and one additional action to support Image and File fields:

  • Add a file to a list

New types

The actions manipulate with special objects which abstract file system entities using two data types: File and Directory. They are plain simple and has the only property — an URI to a file or directory.

How to use

First you should open an existing or create a new directory and get the Directory object as a handler for the subsequent operations. That's what the actions Open directory and Create directory are created for.

The rest of the actions takes the Directory object as a parameter so you can work with several directories at the same time.

The Find directories and Find files actions take Search mask and Directory and scan it for matching directory or file name respectively. The matching works similarly to how Text comparison condition works in Rules. These actions return a list of Directory or File objects.

The Copy file action performs copying of a file to a new destination. The File parameter can be either Drupal File entity or a File object implemented by this module. The Destination parameter is of Directory type. The action copies files using Drupal file_unmanaged_copy() function and returns an object of File type.

The Save file to the database action saves an existing (supposingly unmanaged) file to the list of managed files. It takes a parameter of File type and returns the whole Drupal File entity.

This was a bit odd that default Rules action set had no means to put a Drupal File entity in the list which is consumable by Fields API. So one more action was implemented to make it possible: Add a file to a list, where a list could be either of Image field items or File field items.

A use case

Suppose you have an eCommerce website with hundreds or products and you have to upload all or a great part of the product images. Doing this by hand using File/Image field UI is a sort of nightmare, especially if you already have the images on disk. So you would definitely want a bulk-upload functionality in you Drupal.

Solving this task you'll face two problems:

1) How to perform bulk-upload of the lot of files
2) How to establish relationship between files on disk and products (or product displays) in the database.

This module is targeted on solving the former problem directly, but allows to implement the latter one either.

Here you will find two rules components for export and import correspondingly: https://gist.github.com/OnkelTem/272ea3daad0934c67062
They won't work out of the box as they are specific to some existing Drupal instance, but you can modify them to suit your needs.

The idea is to first export all existing images into directories so that each directory would unambiguously correspond to some node, then copy needed files to those directories and then import files from the updated export directories back to Drupal.

To make exported directories look familiar and appealing for a human they are named by original nodes. The relationships are established by augmenting the directory names with "__[nid]" suffix.

Project information

  • Module categories: Automation
  • Created by OnkelTem on , updated