You are here

class MigratePathEntityHandler in Migrate 7.2

@file Support for paths in core Drupal objects

Hierarchy

Expanded class hierarchy of MigratePathEntityHandler

1 string reference to 'MigratePathEntityHandler'
migrate_migrate_api in ./migrate.migrate.inc

File

plugins/destinations/path.inc, line 8
Support for paths in core Drupal objects

View source
class MigratePathEntityHandler extends MigrateDestinationHandler {
  public function __construct() {
    $this
      ->registerTypes(array(
      'entity',
    ));
  }

  /**
   * Implementation of MigrateDestinationHandler::fields().
   */
  public function fields($entity_type, $bundle, $migration = NULL) {
    if (module_exists('path')) {
      return array(
        'path' => t('Path alias'),
      );
    }
    return array();
  }
  public function prepare($entity, stdClass $row) {
    if (module_exists('path') && isset($entity->path)) {

      // Make sure the alias doesn't already exist
      $query = db_select('url_alias')
        ->condition('alias', $entity->path)
        ->condition('language', $entity->language);
      $query
        ->addExpression('1');
      $query
        ->range(0, 1);
      if (!$query
        ->execute()
        ->fetchField()) {
        $path = $entity->path;
        $entity->path = array();
        $entity->path['alias'] = $path;
      }
      else {
        unset($entity->path);
      }
    }
  }
  public function complete($entity, stdClass $row) {

    // Check if this is a forum taxonomy term.
    if (module_exists('forum')) {
      if (isset($entity->vocabulary_machine_name) && $entity->vocabulary_machine_name == 'forums') {

        // Check if a path ID exists for this term.
        $term_pid = db_select('url_alias', 'url_alias')
          ->fields('url_alias', array(
          'pid',
        ))
          ->condition('source', 'taxonomy/term/' . $entity->tid)
          ->condition('language', $entity->language)
          ->execute()
          ->fetchField();

        // Check if there is also a path ID for this term referencing forums.
        $forum_term_pid = db_select('url_alias', 'url_alias')
          ->fields('url_alias', array(
          'pid',
        ))
          ->condition('source', 'forum/' . $entity->tid)
          ->condition('language', $entity->language)
          ->execute()
          ->fetchField();

        // If both term and forum term path IDs exist, delete the term's path.
        if ($term_pid && $forum_term_pid) {
          db_delete('url_alias')
            ->condition('pid', $term_pid)
            ->execute();
        }
        elseif ($term_pid && empty($forum_term_pid)) {
          db_update('url_alias')
            ->fields(array(
            'source' => 'forum/' . $entity->tid,
          ))
            ->condition('pid', $term_pid)
            ->execute();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateHandler::$dependencies protected property List of other handler classes which should be invoked before the current one.
MigrateHandler::$typesHandled protected property List of "types" handled by this handler. Depending on the kind of handler, these may be destination types, field types, etc.
MigrateHandler::getDependencies public function
MigrateHandler::getTypesHandled public function
MigrateHandler::handlesType public function Does this handler handle the given type? 1
MigrateHandler::registerTypes protected function Register a list of types handled by this class
MigratePathEntityHandler::complete public function
MigratePathEntityHandler::fields public function Implementation of MigrateDestinationHandler::fields().
MigratePathEntityHandler::prepare public function
MigratePathEntityHandler::__construct public function Overrides MigrateHandler::__construct