You are here

public function MigrateItemsXML::getItem in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/xml.inc \MigrateItemsXML::getItem()

Implementers are expected to return an object representing a source item. Items from currentUrl are cached as an array of key=ID and value=stdClass object with attribute xml containing the xml SimpleXMLElement object of the item.

Parameters

mixed $id:

Return value

stdClass

Overrides MigrateItems::getItem

File

plugins/sources/xml.inc, line 807
Support for migration from XML sources.

Class

MigrateItemsXML
Implementation of MigrateItems, for providing a list of IDs and for retrieving a parsed XML document given an ID from this list.

Code

public function getItem($id) {

  // Make sure we actually have an ID.
  if (empty($id)) {
    return NULL;
  }

  // If $id is in currentXml return the right item immediately.
  if (isset($this->currentItems) && isset($this->currentItems[$id])) {
    $item = $this->currentItems[$id];
  }
  else {

    // Otherwise find the right url and get the items from.
    if ($this->idsMap === NULL) {

      // Populate the map.
      $this
        ->getIdList();
    }
    foreach ($this->idsMap as $url => $ids) {
      if (in_array($id, $ids, TRUE)) {
        $this->currentItems = NULL;
        $this->currentUrl = $url;
        $items = $this
          ->getAllItems();
        $item = $items[$id];
      }
    }
  }
  if (!empty($item)) {
    return $item;
  }
  else {
    $migration = Migration::currentMigration();
    $message = t('Loading of item XML for ID !id failed:', array(
      '!id' => $id,
    ));
    foreach (libxml_get_errors() as $error) {
      $message .= "\n" . $error->message;
    }
    $migration
      ->getMap()
      ->saveMessage(array(
      $id,
    ), $message, MigrationBase::MESSAGE_ERROR);
    libxml_clear_errors();
    return NULL;
  }
}