You are here

public function MigrateDestinationMenuLinks::import in Migrate 7.2

Import a single row.

Parameters

$menu_link: Menu link object to build. Prefilled with any fields mapped in the Migration.

$row: Raw source data object - passed through to prepare/complete handlers.

Return value

array Array of key fields of the object that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

File

plugins/destinations/menu_links.inc, line 87
Support for menu link destinations.

Class

MigrateDestinationMenuLinks
Destination class implementing migration into {menu_links}.

Code

public function import(stdClass $menu_link, stdClass $row) {

  // Updating previously-migrated content
  if (isset($row->migrate_map_destid1)) {
    $menu_link->mlid = $row->migrate_map_destid1;
  }

  // Load old values if necessary.
  $migration = Migration::currentMigration();
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($menu_link->mlid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination mlid provided'));
    }
    if (!($old_menu_link = menu_link_load($menu_link->mlid))) {
      throw new MigrateException(t('System-of-record is DESTINATION, and the provided mlid could not be found'));
    }
    $menu_link_to_update = (object) $old_menu_link;
    foreach ($old_menu_link as $key => $value) {
      if (!isset($menu_link->{$key})) {
        $menu_link->{$key} = $old_menu_link[$key];
      }
    }
  }

  // Invoke migration prepare handlers
  // @todo derive existing mlids?
  $this
    ->prepare($menu_link, $row);

  // Menu links are handled as arrays, so clone the object to an array.
  $item = clone $menu_link;
  $item = (array) $item;
  migrate_instrument_start('menu_link_save');

  // Check to see if this is a new menu item.
  $update = FALSE;
  if (isset($item['mlid'])) {
    $update = TRUE;
    $mlid = menu_link_save($item);
  }
  else {

    // menu_link_save() should return an mlid integer.
    $mlid = menu_link_save($item);
  }
  migrate_instrument_stop('menu_link_save');

  // Return the new id or FALSE on failure.
  if (!empty($mlid)) {

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

    // Return the primary key to the mapping table.
    $return = array(
      $mlid,
    );
  }
  else {
    $return = FALSE;
  }

  // Invoke migration complete handlers.
  $menu_link = (object) menu_link_load($mlid);
  $this
    ->complete($menu_link, $row);
  return $return;
}