You are here

class MigrateDestinationDomain in Domain Access 7.3

Migration class for importing domains into Drupal.

In your migration class, you may map existing data directly to a Drupal domain record. The fields are as follows:

  • subdomain -- the domain URL (e.g. example.com), not including a protocol or a path.
  • machine_name (optional) -- an optional machine name for the domain record.
  • sitename -- the human-readable name of the domain.
  • scheme (optional)-- the URL scheme for the domain, either http or https.
  • valid (optional) -- binary indicator that the domain is active or not.
  • weight (optional) -- signed integer indicating the sort order of the domain.
  • is_default (optional) -- binary indicator that this is the default domain.

Hierarchy

Expanded class hierarchy of MigrateDestinationDomain

File

./domain.migrate.inc, line 23
Support for domains in core Drupal objects

View source
class MigrateDestinationDomain extends MigrateDestination {
  public static function getKeySchema() {
    return array(
      'machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Primary Key for domain entries.',
      ),
    );
  }
  public function __construct() {
    parent::__construct();
  }
  public function __toString() {
    $output = t('Domain');
    return $output;
  }

  /**
   * Returns a list of fields available to be mapped for domains.
   *
   * @param Migration $migration
   *  Optionally, the migration containing this destination.
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields($migration = NULL) {
    $fields = array(
      'machine_name' => t('An optional machine name for the domain record. Will be generated is necessary.'),
      'subdomain' => t('The domain URL (e.g. example.com), not including a protocol or a path. Required.'),
      'sitename' => t('The human-readable name of the domain. Required.'),
      'scheme' => t('The URL scheme for the domain, either http or https. Optional.'),
      'valid' => t('Binary indicator that the domain is active or not. Optional.'),
      'weight' => t('Signed integer indicating the sort order of the domain. Optional.'),
      'is_default' => t('Binary indicator that this is the default domain. Optional.'),
    );
    return $fields;
  }

  /**
   * Import a single row.
   *
   * @param $domain
   *  Domain object to build. Prefilled with any fields mapped in the Migration.
   * @param $row
   *  Raw source data object - passed through to prepare/complete handlers.
   * @return array
   *  Array of key fields of the object that was saved if
   *  successful. FALSE on failure.
   */
  public function import(stdClass $domain, stdClass $row) {

    // Set up optional fields.
    if (empty($domain->machine_name)) {
      $domain->machine_name = domain_machine_name($domain->subdomain);
    }
    if (!isset($domain->valid)) {
      $domain->valid = 1;
    }
    if (empty($domain->scheme)) {
      $domain->scheme = 'http';
    }
    if (!isset($domain->weight)) {
      $domain->weight = count(domain_domains()) + 1;
    }
    if (!isset($domain->is_default)) {
      $domain->is_default = 0;
    }

    // Invoke migration prepare handlers.
    $this
      ->prepare($domain, $row);

    // Domains are handled as arrays, so clone the object to an array.
    $domain = clone $domain;
    $domain = (array) $domain;

    // Check to see if this is a new domain.
    $update = FALSE;
    if ($data = domain_machine_name_load($domain['machine_name'])) {
      $domain['domain_id'] = $data['domain_id'];
      $update = TRUE;
    }

    // domain_save() provides no return callback, so we can't really test this
    // without running a domain_load() check.
    migrate_instrument_start('domain_save');
    domain_save($domain);
    migrate_instrument_stop('domain_save');

    // Return the new id or FALSE on failure.
    if ($data = domain_machine_name_load($domain['machine_name'])) {

      // Increment the count if the save succeeded.
      if ($update) {
        $this->numUpdated++;
      }
      else {
        $this->numCreated++;
      }

      // Return the primary key to the mapping table.
      $return = array(
        $data['machine_name'],
      );
    }
    else {
      $return = FALSE;
    }

    // Invoke migration complete handlers.
    $domain = (object) $data;
    $this
      ->complete($domain, $row);
    return $return;
  }

  /**
   * Implementation of MigrateDestination::prepare().
   */
  public function prepare($domain, stdClass $row) {

    // We do nothing here but allow child classes to act.
    $migration = Migration::currentMigration();
    $domain->migrate = array(
      'machineName' => $migration
        ->getMachineName(),
    );

    // Call any general handlers.
    migrate_handler_invoke_all('domain', 'prepare', $domain, $row);

    // Then call any prepare handler for this specific Migration.
    if (method_exists($migration, 'prepare')) {
      $migration
        ->prepare($domain, $row);
    }
  }
  public function complete($domain, stdClass $row) {

    // We do nothing here but allow child classes to act.
    $migration = Migration::currentMigration();
    $domain->migrate = array(
      'machineName' => $migration
        ->getMachineName(),
    );

    // Call any general handlers.
    migrate_handler_invoke_all('domain', 'complete', $domain, $row);

    // Then call any complete handler for this specific Migration.
    if (method_exists($migration, 'complete')) {
      $migration
        ->complete($domain, $row);
    }
  }

  /**
   * Delete a single domain.
   *
   * @param $id
   *  Array of fields representing the key (in this case, just domain_name).
   */
  public function rollback(array $id) {
    $domain_name = reset($id);
    migrate_instrument_start('domain_delete');
    $this
      ->prepareRollback($domain_name);
    if ($domain = domain_machine_name_load($domain_name)) {

      // We cannot delete the primary domain.
      $default = domain_default_id();
      if ($domain['domain_id'] != $default) {
        domain_delete($domain);
      }
      else {
        drupal_set_message(t('The default domain may not be rolled back.'), 'status', FALSE);
      }
    }
    $this
      ->completeRollback($domain_name);
    migrate_instrument_stop('domain_delete');
  }

  /**
   * Give handlers a shot at cleaning up before a domain has been rolled back.
   *
   * @param $domain_name
   *  The machine_name ID of the domain about to be deleted.
   */
  public function prepareRollback($domain_name) {

    // We do nothing here but allow child classes to act.
    $migration = Migration::currentMigration();

    // Call any general handlers.
    migrate_handler_invoke_all('domain', 'prepareRollback', $domain_name);

    // Then call any complete handler for this specific Migration.
    if (method_exists($migration, 'prepareRollback')) {
      $migration
        ->prepareRollback($domain_name);
    }
  }

  /**
   * Give handlers a shot at cleaning up after a domain has been rolled back.
   *
   * @param $domain_name
   *  ID of the domain which has been deleted.
   */
  public function completeRollback($domain_name) {

    // We do nothing here but allow child classes to act.
    $migration = Migration::currentMigration();

    // Call any general handlers.
    migrate_handler_invoke_all('domain', 'completeRollback', $domain_name);

    // Then call any complete handler for this specific Migration.
    if (method_exists($migration, 'completeRollback')) {
      $migration
        ->completeRollback($domain_name);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationDomain::complete public function
MigrateDestinationDomain::completeRollback public function Give handlers a shot at cleaning up after a domain has been rolled back.
MigrateDestinationDomain::fields public function Returns a list of fields available to be mapped for domains. Overrides MigrateDestination::fields
MigrateDestinationDomain::getKeySchema public static function
MigrateDestinationDomain::import public function Import a single row. Overrides MigrateDestination::import
MigrateDestinationDomain::prepare public function Implementation of MigrateDestination::prepare().
MigrateDestinationDomain::prepareRollback public function Give handlers a shot at cleaning up before a domain has been rolled back.
MigrateDestinationDomain::rollback public function Delete a single domain.
MigrateDestinationDomain::__construct public function Null constructor Overrides MigrateDestination::__construct
MigrateDestinationDomain::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString