MigrateException example

class Concat extends ProcessPluginBase {

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    if (is_array($value)) {
      $delimiter = $this->configuration['delimiter'] ?? '';
      return implode($delimiter$value);
    }
    else {
      throw new MigrateException(sprintf('%s is not an array', var_export($value, TRUE)));
    }
  }

}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $return = $source = [];

    if ($this->configuration['include_source']) {
      $key = $this->configuration['source_key'];
      $source[$key] = $row->getSource();
    }

    if (is_array($value) || $value instanceof \Traversable) {
      foreach ($value as $key => $new_value) {
        if (!is_array($new_value)) {
          throw new MigrateException(sprintf("Input array should hold elements of type array, instead element was of type '%s'", gettype($new_value)));
        }
        $new_row = new Row($new_value + $source);
        $migrate_executable->processRow($new_row$this->configuration['process']);
        $destination = $new_row->getDestination();
        if (array_key_exists('key', $this->configuration)) {
          $key = $this->transformKey($key$migrate_executable$new_row);
        }
        // Do not save the result if the key is NULL. The configured process         // pipeline used in transformKey() will return NULL if a         // MigrateSkipProcessException is thrown.         // @see \Drupal\filter\Plugin\migrate\process\FilterID
/** @var \Drupal\image\Entity\ImageStyle $style */
    $style = $this->getEntity($row$old_destination_id_values);

    // Iterate the effects array so each effect plugin can be initialized.     // Catch any missing plugin exceptions.     foreach ($effects as $effect) {
      try {
        $style->addImageEffect($effect);
      }
      catch (PluginNotFoundException $e) {
        throw new MigrateException($e->getMessage(), 0, $e);
      }
    }

    $style->save();

    return [$style->id()];
  }

}
class ArrayBuild extends ProcessPluginBase {

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $new_value = [];

    foreach ((array) $value as $old_value) {
      // Checks that $old_value is an array.       if (!is_array($old_value)) {
        throw new MigrateException("The input should be an array of arrays");
      }

      // Checks that the key exists.       if (!array_key_exists($this->configuration['key']$old_value)) {
        throw new MigrateException("The key '" . $this->configuration['key'] . "' does not exist");
      }

      // Checks that the value exists.       if (!array_key_exists($this->configuration['value']$old_value)) {
        throw new MigrateException("The key '" . $this->configuration['value'] . "' does not exist");
      }

      
case 'datestamp':
        $from_format = 'U';
        $to_format = 'U';
        break;

      case 'datetime':
        $from_format = 'Y-m-d H:i:s';
        break;

      default:
        throw new MigrateException(sprintf('Field %s of type %s is an unknown date field type.', $field_namevar_export($data['type'], TRUE)));
    }
    $process = [
      'value' => [
        'plugin' => 'format_date',
        'from_format' => $from_format,
        'to_format' => $to_format,
        'source' => 'value',
      ],
    ];

    // If the 'todate' setting is specified the field is now a 'daterange' and
/** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $new_value = [
      'enabled' => [],
      'method_weights' => [],
    ];

    if (!is_array($value)) {
      throw new MigrateException('The input should be an array');
    }

    // If no weights are provided, use the keys by flipping the array.     if (empty($value[1])) {
      $new_value['enabled'] = array_flip(array_map([$this, 'mapNewMethods']array_keys($value[0])));
      unset($new_value['method_weights']);
    }
    else {
      foreach ($value[1] as $method => $weight) {
        $new_method = $this->mapNewMethods($method);
        $new_value['method_weights'][$new_method] = $weight;
        

    [$source$destination] = $value;

    // If the source path or URI represents a remote resource, delegate to the     // download plugin.     if (!$this->isLocalUri($source)) {
      return $this->downloadPlugin->transform($value$migrate_executable$row$destination_property);
    }

    // Ensure the source file exists, if it's a local URI or path.     if (!file_exists($source)) {
      throw new MigrateException("File '$source' does not exist");
    }

    // If the start and end file is exactly the same, there is nothing to do.     if ($this->isLocationUnchanged($source$destination)) {
      return $destination;
    }

    // Check if a writable directory exists, and if not try to create it.     $dir = $this->getDirectory($destination);
    // If the directory exists and is writable, avoid     // \Drupal\Core\File\FileSystemInterface::prepareDirectory() call and write
'not null' => TRUE,
        ];

      case 'string':
        return [
          'type' => 'varchar',
          'length' => 65536,
          'not null' => FALSE,
        ];

      default:
        throw new MigrateException($id_definition['type'] . ' not supported');
    }
  }

}
$multiple = FALSE;
    /** @var \Drupal\migrate\Plugin\MigrateProcessInterface $plugin */
    foreach ($plugins as $plugin) {
      $definition = $plugin->getPluginDefinition();
      // Many plugins expect a scalar value but the current value of the       // pipeline might be multiple scalars (this is set by the previous plugin)       // and in this case the current value needs to be iterated and each scalar       // separately transformed.       if ($multiple && !$definition['handle_multiples']) {
        $new_value = [];
        if (!is_array($value)) {
          throw new MigrateException(sprintf('Pipeline failed at %s plugin for destination %s: %s received instead of an array,', $plugin->getPluginId()$destination$value));
        }
        $break = FALSE;
        foreach ($value as $scalar_value) {
          try {
            $new_value[] = $plugin->transform($scalar_value$this$row$destination);
          }
          catch (MigrateSkipProcessException $e) {
            $new_value[] = NULL;
            $break = TRUE;
          }
          catch (MigrateException $e) {
            

class StaticMap extends ProcessPluginBase {

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $new_value = $value;
    if (is_array($value)) {
      if (!$value) {
        throw new MigrateException('Can not lookup without a value.');
      }
    }
    else {
      $new_value = [$value];
    }
    $new_value = NestedArray::getValue($this->configuration['map']$new_value$key_exists);
    if (!$key_exists) {
      if (array_key_exists('default_value', $this->configuration)) {
        if (!empty($this->configuration['bypass'])) {
          throw new MigrateException('Setting both default_value and bypass is invalid.');
        }
        

class Substr extends ProcessPluginBase {

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $start = $this->configuration['start'] ?? 0;
    if (!is_int($start)) {
      throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
    }
    $length = $this->configuration['length'] ?? NULL;
    if ($length !== NULL && !is_int($length)) {
      throw new MigrateException('The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.');
    }
    if (!is_string($value)) {
      throw new MigrateException('The input value must be a string.');
    }

    // Use optional start or length to return a portion of $value.     return mb_substr($value$start$length);
  }
class DefaultLangcode extends Config {

  /** * {@inheritdoc} */
  public function import(Row $row, array $old_destination_id_values = []) {
    $destination = $row->getDestination();
    $langcode = $destination['default_langcode'];

    // Check if the language exists.     if (ConfigurableLanguage::load($langcode) === NULL) {
      throw new MigrateException("The language '$langcode' does not exist on this site.");
    }

    $this->config->set('default_langcode', $destination['default_langcode']);
    $this->config->save();
    return [$this->config->getName()];
  }

}
array_keys($container->get('entity_type.bundle.info')->getBundleInfo($entity_type_id)),
      $container->get('language_manager'),
      $container->get('config.factory')
    );
  }

  /** * {@inheritdoc} */
  public function import(Row $row, array $old_destination_id_values = []) {
    if ($row->isStub()) {
      throw new MigrateException('Config entities can not be stubbed.');
    }
    $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
    $ids = $this->getIds();
    $id_key = $this->getKey('id');
    if (count($ids) > 1) {
      // Ids is keyed by the key name so grab the keys.       $id_keys = array_keys($ids);
      if (!$row->getDestinationProperty($id_key)) {
        // Set the ID into the destination in for form "val1.val2.val3".         $row->setDestinationProperty($id_key$this->generateId($row$id_keys));
      }
    }
$this->io->progressStart($migrationCount);
        $migratedCounter = 0;

        try {
            foreach ($this->getMigrationGenerator($collection$until$limit) as $_return) {
                $this->io->progressAdvance();
                ++$migratedCounter;
            }
        } catch (\Exception $e) {
            $this->finishProgress($migratedCounter$migrationCount);

            throw new MigrateException($e->getMessage() . \PHP_EOL . 'Trace: ' . \PHP_EOL . $e->getTraceAsString()$e);
        }

        $this->finishProgress($migratedCounter$migrationCount);
        $this->io->writeln(sprintf('all migrations for identifier: "%s" executed', $identifier));

        return $migrationCount;
    }
}

class Explode extends ProcessPluginBase {

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    if (empty($this->configuration['delimiter'])) {
      throw new MigrateException('delimiter is empty');
    }

    $strict = array_key_exists('strict', $this->configuration) ? $this->configuration['strict'] : TRUE;
    if ($strict && !is_string($value)) {
      throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
    }
    elseif (!$strict) {
      // Check if the incoming value can cast to a string.       $original = $value;
      if (!is_string($original) && ($original != ($value = @strval($value)))) {
        throw new MigrateException(sprintf('%s cannot be casted to a string', var_export($original, TRUE)));
      }
Home | Imprint | This part of the site doesn't use cookies.