You are here

class MigrateDestinationTermMachineName in Taxonomy Machine Name 7

Class MigrateDestinationTermMachineName

Hierarchy

Expanded class hierarchy of MigrateDestinationTermMachineName

File

./taxonomy_machine_name.migrate.inc, line 81
Taxonomy Machine Name Migrate Module File.

View source
class MigrateDestinationTermMachineName extends MigrateDestinationTerm {

  /**
   * @var array
   */
  protected $deferred;

  /**
   * Constructor for MigrateDestinationTermMachineName.
   *
   * @param string $bundle
   *   The machine name of the taxonomy vocabulary.
   *
   * @param array  $options
   *   An optional array of options to control the import process.
   */
  public function __construct($bundle, array $options = array()) {
    parent::__construct($bundle, $options);
    $this->deferred = array();
  }

  /**
   * Returns the fields that are exposed by this migration destination.
   *
   * The only additional field is "Parent (by machine name)".
   *
   * @param Migration $migration
   *   Migration instance.
   *
   * @return array
   *   Destination fields.
   */
  public function fields($migration = NULL) {
    $fields = parent::fields();
    $fields['parent_machine_name'] = t('Parent (by machine name).');
    return $fields;
  }

  /**
   * Attempt to find a term that has the same machine name.
   *
   * @param object $term
   *   A taxonomy term object with at least the machine
   *   name and vid properties defined.
   *
   * @return object
   *   A matching taxonomy term object if found, otherwise FALSE.
   */
  public function findMatchingTerm($term) {
    $candidate = taxonomy_term_machine_name_load(trim($term->machine_name), $term->vid);
    if (is_object($candidate)) {
      return $candidate;
    }
    else {
      return FALSE;
    }
  }

  /**
   * Import the provided term, using information from the provided row.
   *
   * @param stdclass $term
   *   Term object to build. Prefilled with any fields mapped in the Migration.
   * @param stdclass $row
   *   Raw source data object - passed through to prepare/complete handlers.
   *
   * @return array
   *   Array of key fields (tid only in this case) of the term that was saved if
   *   successful. FALSE on failure.
   *
   * @throws \MigrateException
   */
  public function import(stdClass $term, stdClass $row) {

    // Look up parent name if provided.
    if (isset($term->parent_machine_name)) {
      $parent_machine_name = trim($term->parent_machine_name);
      if (!empty($parent_machine_name)) {

        // Default to bundle if no vocabulary machine name provided.
        if (!isset($term->vocabulary_machine_name)) {
          $term->vocabulary_machine_name = $this->bundle;
        }
        $parent_tid = taxonomy_term_machine_name_load($parent_machine_name, $term->vocabulary_machine_name);
        if (!empty($parent_tid)) {
          $term->parent = $parent_tid->tid;
        }
        else {

          // Store aside for post import process once parent will be created.
          $deferred_import = new stdClass();
          $deferred_import->term = clone $term;
          $deferred_import->row = clone $row;
          $this->deferred[] = $deferred_import;
        }
      }
    }
    $tid = parent::import($term, $row);
    return $tid;
  }

  /**
   * Check whether or not this destination has terms for which the import was.
   *
   * "deferred" because their parent term could not be found.
   *
   * @return bool
   *   TRUE if there are terms for which import was deferred, FALSE otherwise.
   */
  public function hasDeferredImports() {
    return count($this->deferred) > 0;
  }

  /**
   * Return the machine names of any terms for which import was deferred.
   *
   * @return array
   *   An array of machine names for all deferred term imports.
   */
  public function getDeferredImportKeys() {
    $keys = array();
    foreach ($this->deferred as $import) {
      $keys[] = $import->term->machine_name;
    }
    return $keys;
  }

  /**
   * Attempt to import any terms for which import was previously deferred.
   *
   * This will run import() again for each term and row, which will again
   * attempt to match up each term with its parent term.
   *
   * Any term for which a parent term is still not found will be returned to
   * the set of deferred imports. If the import is a multi-level hierarchy,
   * this method can be invoked multiple times until every level of the
   * hierarchy has been successfully imported.
   */
  public function runDeferredImports() {
    $current = $this->deferred;

    // Clear the list of deferred imports, to be rebuilt during the import.
    $this->deferred = array();
    foreach ($current as $import) {

      // This will automatically re-queue any imports that
      // still need to be deferred.
      $this
        ->import($import->term, $import->row);
    }
  }

}

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.
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::array_flatten public static function Flattens an array of allowed values.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::fieldAttachValidate public static function Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString
MigrateDestinationTerm::$allowDuplicateTerms protected property Boolean indicating whether to permit duplicate terms to be created.
MigrateDestinationTerm::getKeySchema public static function
MigrateDestinationTerm::options public static function Return an options array for term destinations.
MigrateDestinationTerm::rollback public function Delete a migrated term
MigrateDestinationTermMachineName::$deferred protected property
MigrateDestinationTermMachineName::fields public function Returns the fields that are exposed by this migration destination. Overrides MigrateDestinationTerm::fields
MigrateDestinationTermMachineName::findMatchingTerm public function Attempt to find a term that has the same machine name. Overrides MigrateDestinationTerm::findMatchingTerm
MigrateDestinationTermMachineName::getDeferredImportKeys public function Return the machine names of any terms for which import was deferred.
MigrateDestinationTermMachineName::hasDeferredImports public function Check whether or not this destination has terms for which the import was.
MigrateDestinationTermMachineName::import public function Import the provided term, using information from the provided row. Overrides MigrateDestinationTerm::import
MigrateDestinationTermMachineName::runDeferredImports public function Attempt to import any terms for which import was previously deferred.
MigrateDestinationTermMachineName::__construct public function Constructor for MigrateDestinationTermMachineName. Overrides MigrateDestinationTerm::__construct