isStub example


class EntityFile extends EntityContentBase {

  /** * {@inheritdoc} */
  protected function getEntity(Row $row, array $old_destination_id_values) {
    // For stub rows, there is no real file to deal with, let the stubbing     // process take its default path.     if ($row->isStub()) {
      return parent::getEntity($row$old_destination_id_values);
    }

    // By default the entity key (fid) would be used, but we want to make sure     // we're loading the matching URI.     $destination = $row->getDestinationProperty('uri');
    if (empty($destination)) {
      throw new MigrateException('Destination property uri not provided');
    }
    $entity = $this->storage->loadByProperties(['uri' => $destination]);
    if ($entity) {
      
if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
      // Allow updateEntity() to change the entity.       $entity = $this->updateEntity($entity$row) ?: $entity;
    }
    else {
      // Attempt to ensure we always have a bundle.       if ($bundle = $this->getBundle($row)) {
        $row->setDestinationProperty($this->getKey('bundle')$bundle);
      }

      // Stubs might need some required fields filled in.       if ($row->isStub()) {
        $this->processStubRow($row);
      }
      $entity = $this->storage->create($row->getDestination());
      $entity->enforceIsNew();
    }
    return $entity;
  }

  /** * Gets the entity ID of the row. * * @param \Drupal\migrate\Row $row * The row of data. * * @return string * The entity ID for the row that we are importing. */
$container->get('file_system'),
      $container->get('http_client')
    );
  }

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    // If we're stubbing a file entity, return a uri of NULL so it will get     // stubbed by the general process.     if ($row->isStub()) {
      return NULL;
    }
    [$source$destination] = $value;

    // Modify the destination filename if necessary.     $final_destination = $this->fileSystem->getDestinationFilename($destination$this->configuration['file_exists']);

    // Reuse if file exists.     if (!$final_destination) {
      return $destination;
    }

    
public function freezeSource() {
    $this->frozen = TRUE;
    return $this;
  }

  /** * Clones the row with an empty set of destination values. * * @return static */
  public function cloneWithoutDestination() {
    return (new static($this->getSource()$this->sourceIds, $this->isStub()))->freezeSource();
  }

  /** * Tests if destination property exists. * * @param array|string $property * An array of properties on the destination. * * @return bool * TRUE if the destination property exists. */
  
'process' => [],
      'destination' => ['plugin' => 'null'],
    ];

    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
    $source = $migration->getSourcePlugin();

    // Validate the plugin returns the source data that was provided.     $results = [];
    /** @var \Drupal\migrate\Row $row */
    foreach ($source as $row) {
      $this->assertFalse($row->isStub());

      $data_row = $row->getSource();
      // The "data" row returned by getSource() also includes all source       // configuration - we remove it so we see only the data itself.       unset($data_row['plugin']);
      unset($data_row['data_rows']);
      unset($data_row['ids']);
      $results[] = $data_row;
    }
    $this->assertSame($data_rows$results);

    

class FileUri extends ProcessPluginBase {

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    // If we're stubbing a file entity, return a uri of NULL so it will get     // stubbed by the general process.     if ($row->isStub()) {
      return NULL;
    }
    [$filepath$file_directory_path$temp_directory_path$is_public] = $value;

    // Specific handling using $temp_directory_path for temporary files.     if (substr($filepath, 0, strlen($temp_directory_path)) === $temp_directory_path) {
      $uri = preg_replace('/^' . preg_quote($temp_directory_path, '/') . '/', '', $filepath);
      return 'temporary://' . ltrim($uri, '/');
    }

    // Strip the files path from the uri instead of using basename
$container->get('entity_field.manager'),
      $container->get('plugin.manager.field.field_type'),
      $container->get('state'),
      $container->get('account_switcher')
    );
  }

  /** * {@inheritdoc} */
  public function import(Row $row, array $old_destination_id_values = []) {
    if ($row->isStub() && ($state = $this->state->get('comment.maintain_entity_statistics', 0))) {
      $this->state->set('comment.maintain_entity_statistics', 0);
    }
    $return = parent::import($row$old_destination_id_values);
    if ($row->isStub() && $state) {
      $this->state->set('comment.maintain_entity_statistics', $state);
    }
    return $return;
  }

  /** * {@inheritdoc} */
// Intentionally create a new revision. Setting new revision TRUE here may       // not be necessary, it is done for clarity.       $entity->setNewRevision(TRUE);
    }
    else {
      // Attempt to set the bundle.       if ($bundle = $this->getBundle($row)) {
        $row->setDestinationProperty($this->getKey('bundle')$bundle);
      }

      // Stubs might need some required fields filled in.       if ($row->isStub()) {
        $this->processStubRow($row);
      }
      $entity = $this->storage->create($row->getDestination());
      $entity->enforceIsNew();
    }

    // We need to update the entity, so that the destination row IDs are     // correct.     $entity = $this->updateEntity($entity$row);
    $entity->isDefaultRevision(TRUE);
    if ($entity instanceof EntityChangedInterface && $entity instanceof ContentEntityInterface) {
      
$container->get('file_system'),
      $container->get('plugin.manager.migrate.process')->createInstance('download', $configuration)
    );
  }

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    // If we're stubbing a file entity, return a URI of NULL so it will get     // stubbed by the general process.     if ($row->isStub()) {
      return NULL;
    }
    [$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.
$container->get('entity_type.manager')->getStorage($entity_type_id),
      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));
      }
Home | Imprint | This part of the site doesn't use cookies.