A MigrateFieldHandler for Link Module

30 Dec 2010
Posted by jcfiala

I continue my work with the Migrate Module, and recently ran into needing to migrate a value to the Link Module. If you haven't gotten into it yet, Migrate generally expects other modules to handle migrating special bits for nodes, except for the CCK module, and that's included both (I think) because it's in D7 core and because it's so bloody common. Field-specific handling is dealt with by implementing a MigrateFieldHandler, which basically sets up the arrays properly. Again, migrate comes with a number of MigrateFieldHandler classes for the core cck fields, but not for custom CCK fields, like Link.

So, having recently run into a link field I needed to migrate, this is a first swing attempt at handling it. Do note that any module that includes this class will need the Migrate api hook that I discussed in my last post, and also note that I intend on including this fix in the next release of the Link module.

class MigrateLinkFieldHandler extends MigrateFieldHandler {
  public function __construct() {
    $this->registerTypes(array('link'));
  }

  public function prepare(stdClass $entity, array $instance, array $values) {
    // Setup the standard Field API array for saving.
    $delta = 0;
    $return = array();
    foreach ($values as $value) {
      // Don't save empty links
      if ($value) {
        if (is_string($value)) {
          $return[$delta]['url'] = $value;
          $return[$delta]['attributes'] = array();
          $delta++;
        }
        else { // we assume the value is an array of url/title/etc.
          $return[$delta] = $value;
          $delta++;
        }
      }
    }
    return $return;
  }
}

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <small> <del> <ins> <br>
  • Lines and paragraphs break automatically.

More information about formatting options