/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']); $destination = $row->getDestinationProperty($this->configuration['destination_path_property']); $replace = FILE_EXISTS_REPLACE; if (!empty($this->configuration['rename'])) { $entity_id = $row->getDestinationProperty($this->getKey('id')); if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) { $replace = FILE_EXISTS_RENAME; } } $dirname = drupal_dirname($destination); if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) { throw new MigrateException(t('Could not create directory %dirname', array('%dirname' => $dirname))); } if ($this->configuration['move']) { $copied = file_unmanaged_move($source, $destination, $replace); } else { // Determine whether we can perform this operation based on overwrite rules. $original_destination = $destination; $destination = file_destination($destination, $replace); if ($destination === FALSE) { throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination))); } $source = $this->urlencode($source); $copied = copy($source, $destination); } if ($copied) { return parent::import($row, $old_destination_id_values); } else { throw new MigrateException(t('File %source could not be copied to %destination.', array('%source' => $source, '%destination' => $destination))); } }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { if ($bundle_key = $this->getKey('bundle')) { $bundle = $row->getDestinationProperty($bundle_key); } else { $bundle = $this->storage->getEntityTypeId(); } // Some migrations save additional data of an existing entity and only // provide the reference to the entity, in those cases, we can not run the // processing below. Migrations that need that need to provide the bundle. if ($bundle) { $field_definitions = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle); foreach ($field_definitions as $field_name => $field_definition) { $field_type = $field_definition->getType(); if ($this->migrateEntityFieldPluginManager->getDefinition($field_type, FALSE)) { $destination_value = $this->migrateEntityFieldPluginManager->createInstance($field_type)->import($field_definition, $row->getDestinationProperty($field_name)); // @TODO: check for NULL return? Add an unset to $row? Maybe needed in // exception handling? Propagate exception? $row->setDestinationProperty($field_name, $destination_value); } } } $entity = $this->getEntity($row, $old_destination_id_values); return $this->save($entity, $old_destination_id_values); }
/** * {@inheritdoc} */ protected function getEntityId(Row $row) { $entity_type = $row->getDestinationProperty('entity_type'); $bundle = $row->getDestinationProperty('bundle'); $field_name = $row->getDestinationProperty('field_name'); return "{$entity_type}.{$bundle}.{$field_name}"; }
/** * {@inheritdoc} */ protected function getEntityId(Row $row) { // Try to find the block by its plugin ID and theme. $properties = array('plugin' => $row->getDestinationProperty('plugin'), 'theme' => $row->getDestinationProperty('theme')); $blocks = array_keys($this->storage->loadByProperties($properties)); return reset($blocks); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $uid = $row->getDestinationProperty('uid'); $module = $row->getDestinationProperty('module'); $key = $row->getDestinationProperty('key'); $this->userData->set($module, $uid, $key, $row->getDestinationProperty('settings')); return [$uid, $module, $key]; }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { /** @var \Drupal\shortcut\ShortcutSetInterface $set */ $set = $this->shortcutSetStorage->load($row->getDestinationProperty('set_name')); /** @var \Drupal\user\UserInterface $account */ $account = User::load($row->getDestinationProperty('uid')); $this->shortcutSetStorage->assignUser($set, $account); return array($set->id(), $account->id()); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { db_insert('photos_comment') ->fields(array( 'fid' => $row->getDestinationProperty('fid'), 'cid' => $row->getDestinationProperty('cid') )) ->execute(); return array($row->getDestinationProperty('fid'), $row->getDestinationProperty('cid')); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { db_insert('photos_access_user') ->fields(array( 'id' => $row->getDestinationProperty('id'), 'uid' => $row->getDestinationProperty('uid'), )) ->execute(); return array($row->getDestinationProperty('id'), $row->getDestinationProperty('uid')); }
/** * Get the entity. * * @param \Drupal\migrate\Row $row * The row object. * * @return \Drupal\Core\Entity\EntityInterface|false * The entity or false if it can not be created. */ protected function getEntity(Row $row, array $old_destination_id_values) { $revision_id = $old_destination_id_values ? reset($old_destination_id_values) : $row->getDestinationProperty($this->getKey('revision')); if (!empty($revision_id) && ($entity = $this->storage->loadRevision($revision_id))) { $entity->setNewRevision(FALSE); } else { $entity_id = $row->getDestinationProperty($this->getKey('id')); $entity = $this->storage->load($entity_id); $entity->enforceIsNew(FALSE); $entity->setNewRevision(TRUE); } $this->updateEntity($entity, $row); $entity->isDefaultRevision(FALSE); return $entity; }
/** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $source = $this->configuration['source']; $properties = is_string($source) ? array($source) : $source; $return = array(); foreach ($properties as $property) { if ($property || (string) $property === '0') { $is_source = TRUE; if ($property[0] == '@') { $property = preg_replace_callback('/^(@?)((?:@@)*)([^@]|$)/', function ($matches) use(&$is_source) { // If there are an odd number of @ in the beginning, it's a // destination. $is_source = empty($matches[1]); // Remove the possible escaping and do not lose the terminating // non-@ either. return str_replace('@@', '@', $matches[2]) . $matches[3]; }, $property); } if ($is_source) { $return[] = $row->getSourceProperty($property); } else { $return[] = $row->getDestinationProperty($property); } } else { $return[] = $value; } } if (is_string($source)) { $this->multiple = is_array($return[0]); return $return[0]; } return $return; }
/** * {@inheritdoc} * @throws \Drupal\migrate\MigrateException */ public function import(Row $row, array $old_destination_id_values = array()) { // Do not overwrite the root account password. if ($row->getDestinationProperty('uid') == 1) { $row->removeDestinationProperty('pass'); } return parent::import($row, $old_destination_id_values); }
/** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { // Only the filter_html filter's settings have a changed format. if ($row->getDestinationProperty('id') === 'filter_html') { $value['allowed_html'] = str_replace(array_keys($this->allowedHtmlDefaultAttributes), array_values($this->allowedHtmlDefaultAttributes), $value['allowed_html']); } return $value; }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $values = array(); // array_intersect_key() won't work because the order is important because // this is also the return value. foreach (array_keys($this->getIds()) as $id) { $values[$id] = $row->getDestinationProperty($id); } $entity = $this->getEntity($values['entity_type'], $values['bundle'], $values[static::MODE_NAME]); if (!$row->getDestinationProperty('hidden')) { $entity->setComponent($values['field_name'], $row->getDestinationProperty('options') ?: array()); } else { $entity->removeComponent($values['field_name']); } $entity->save(); return array_values($values); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { // @todo build storage interface for photos? // @todo look up node->nid in migration system and match pid! // @todo add support to check if pid exists. $path = db_update('photos_album') ->fields(array( 'fid' => $row->getDestinationProperty('fid'), 'wid' => $row->getDestinationProperty('wid'), 'count' => $row->getDestinationProperty('count'), 'data' => $row->getDestinationProperty('data'), )) ->condition('pid', $row->getDestinationProperty('pid')) ->execute(); return array($path['pid']); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $values = array(); // array_intersect_key() won't work because the order is important because // this is also the return value. foreach (array_keys($this->getIds()) as $id) { $values[$id] = $row->getDestinationProperty($id); } $entity = $this->getEntity($values['entity_type'], $values['bundle'], $values[static::MODE_NAME]); if (!$entity->isNew()) { $settings = $row->getDestinationProperty('field_group'); $entity->setThirdPartySetting('field_group', $row->getDestinationProperty('id'), $settings); if (isset($settings['format_type']) && ($settings['format_type'] == 'no_style' || $settings['format_type'] == 'hidden')) { $entity->unsetThirdPartySetting('field_group', $row->getDestinationProperty('id')); } $entity->save(); } return array_values($values); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']); $destination = $row->getDestinationProperty($this->configuration['destination_path_property']); $replace = FILE_EXISTS_REPLACE; if (!empty($this->configuration['rename'])) { $entity_id = $row->getDestinationProperty($this->getKey('id')); if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) { $replace = FILE_EXISTS_RENAME; } } $dirname = drupal_dirname($destination); file_prepare_directory($dirname, FILE_CREATE_DIRECTORY); if ($this->configuration['move']) { file_unmanaged_move($source, $destination, $replace); } else { file_unmanaged_copy($source, $destination, $replace); } return parent::import($row, $old_destination_id_values); }
/** * Gets the entity. * * @param \Drupal\migrate\Row $row * The row object. * @param array $old_destination_id_values * The old destination IDs. * * @return \Drupal\Core\Entity\EntityInterface|false * The entity or false if it can not be created. */ protected function getEntity(Row $row, array $old_destination_id_values) { $revision_id = $old_destination_id_values ? reset($old_destination_id_values) : $row->getDestinationProperty($this->getKey('revision')); if (!empty($revision_id) && ($entity = $this->storage->loadRevision($revision_id))) { $entity->setNewRevision(FALSE); } else { $entity_id = $row->getDestinationProperty($this->getKey('id')); $entity = $this->storage->load($entity_id); // If we fail to load the original entity something is wrong and we need // to return immediately. if (!$entity) { return FALSE; } $entity->enforceIsNew(FALSE); $entity->setNewRevision(TRUE); } $this->updateEntity($entity, $row); $entity->isDefaultRevision(FALSE); return $entity; }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { db_insert('photos_image') ->fields(array( 'fid' => $row->getDestinationProperty('fid'), 'pid' => $row->getDestinationProperty('pid'), 'title' => $row->getDestinationProperty('title'), 'des' => $row->getDestinationProperty('des'), 'wid' => $row->getDestinationProperty('wid'), 'count' => $row->getDestinationProperty('count'), 'comcount' => $row->getDestinationProperty('comcount'), 'exif' => $row->getDestinationProperty('exif'), )) ->execute(); return array($row->getDestinationProperty('fid')); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { if ($row->hasDestinationProperty('langcode')) { $this->config = $this->language_manager->getLanguageConfigOverride($row->getDestinationProperty('langcode'), $this->config->getName()); } foreach ($row->getRawDestination() as $key => $value) { if (isset($value) || !empty($this->configuration['store null'])) { $this->config->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $key), $value); } } $this->config->save(); return [$this->config->getName()]; }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $destination = $row->getDestination(); $keys = array(); foreach (array_keys($this->ids) as $id) { $keys[$id] = $destination[$id]; unset($destination[$id]); } \Drupal::database()->merge($this->table_name)->keys($keys)->fields($destination)->execute(); $return = array_map(function ($key) use($row) { return $row->getDestinationProperty($key); }, $keys); return $return; }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = []) { $effects = []; // Need to set the effects property to null on the row before the ImageStyle // is created, this prevents improper effect plugin initialization. if ($row->getDestinationProperty('effects')) { $effects = $row->getDestinationProperty('effects'); $row->setDestinationProperty('effects', []); } /** @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 array($style->id()); }
/** * {@inheritdoc} * * Set field formatter settings when the map didn't map: for date * formatters, the fallback format, for everything else, empty array. */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { // If the 1 index is set then the map missed. if (isset($value[1])) { $module = $row->getSourceProperty('module'); if ($module === 'date') { $value = array('format_type' => 'fallback'); } elseif ($module === 'number') { // We have to do the lookup here in the process plugin because for // number we need to calculated the settings based on the type not just // the module which works well for other field types. return $this->numberSettings($row->getDestinationProperty('options/type'), $value[1]); } else { $value = array(); } } return $value; }
/** * {@inheritdoc} * @throws \Drupal\migrate\MigrateException */ public function import(Row $row, array $old_destination_id_values = array()) { if ($this->password) { if ($this->password instanceof MigratePassword) { $this->password->enableMd5Prefixing(); } else { throw new MigrateException('Password service has been altered by another module, aborting.'); } } // Do not overwrite the root account password. if ($row->getDestinationProperty('uid') == 1) { $row->removeDestinationProperty('pass'); } $ids = parent::import($row, $old_destination_id_values); if ($this->password) { $this->password->disableMd5Prefixing(); } return $ids; }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { db_insert('photos_access_album') ->fields(array( 'id' => $row->getDestinationProperty('id'), 'nid' => $row->getDestinationProperty('nid'), 'viewid' => $row->getDestinationProperty('viewid'), 'updateid' => $row->getDestinationProperty('updateid'), 'pass' => $row->getDestinationProperty('pass'), )) ->execute(); return array($row->getDestinationProperty('id')); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $file = $row->getSourceProperty($this->configuration['source_path_property']); $destination = $row->getDestinationProperty($this->configuration['destination_path_property']); // We check the destination to see if this is a temporary file. If it is // then we do not prepend the source_base_path because temporary files are // already absolute. $source = $this->isTempFile($destination) ? $file : $this->configuration['source_base_path'] . $file; $dirname = drupal_dirname($destination); if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) { throw new MigrateException(t('Could not create directory %dirname', array('%dirname' => $dirname))); } // If the start and end file is exactly the same, there is nothing to do. if (drupal_realpath($source) === drupal_realpath($destination)) { return parent::import($row, $old_destination_id_values); } $replace = FILE_EXISTS_REPLACE; if (!empty($this->configuration['rename'])) { $entity_id = $row->getDestinationProperty($this->getKey('id')); if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) { $replace = FILE_EXISTS_RENAME; } } if ($this->configuration['move']) { $copied = file_unmanaged_move($source, $destination, $replace); } else { // Determine whether we can perform this operation based on overwrite rules. $original_destination = $destination; $destination = file_destination($destination, $replace); if ($destination === FALSE) { throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination))); } $source = $this->urlencode($source); $copied = @copy($source, $destination); } if ($copied) { return parent::import($row, $old_destination_id_values); } else { throw new MigrateException(t('File %source could not be copied to %destination.', array('%source' => $source, '%destination' => $destination))); } }
/** * {@inheritdoc} */ protected function processStubRow(Row $row) { // We stub the uri value ourselves so we can create a real stub file for it. if (!$row->getDestinationProperty('uri')) { $field_definitions = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $this->getKey('bundle')); $value = UriItem::generateSampleValue($field_definitions['uri']); if (empty($value)) { throw new MigrateException('Stubbing failed, unable to generate value for field uri'); } // generateSampleValue() wraps the value in an array. $value = reset($value); // Make it into a proper public file uri, stripping off the existing // scheme if present. $value = 'public://' . preg_replace('|^[a-z]+://|i', '', $value); $value = Unicode::substr($value, 0, $field_definitions['uri']->getSetting('max_length')); // Create a real file, so File::preSave() can do filesize() on it. touch($value); $row->setDestinationProperty('uri', $value); } parent::processStubRow($row); }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $identifier = $row->getDestinationProperty('identifier'); $disqus = disqus_api(); if ($disqus) { try { $thread = $disqus->threads->details(array('forum' => $this->config->get('disqus_domain'), 'thread:ident' => $identifier, 'thread' => '1')); } catch (\Exception $exception) { $this->logger->error('Error loading thread details for entity : @identifier. Check your API keys.', array('@identifier' => $identifier)); $thread = NULL; } if (!isset($thread->id)) { try { $thread = $disqus->threads->create(array('forum' => $this->config->get('disqus_domain'), 'access_token' => $this->config->get('advanced.disqus_useraccesstoken'), 'title' => $row->getDestinationProperty('title'), 'identifier' => $identifier)); } catch (\Exception $exception) { $this->logger->error('Error creating thread for entity : @identifier. Check your user access token.', array('@identifier' => $identifier)); } } try { $message = $row->getDestinationProperty('message'); $author_name = $row->getDestinationProperty('author_name'); $author_email = $row->getDestinationProperty('author_email'); $author_url = $row->getDestinationProperty('author_url'); $date = $row->getDestinationProperty('author_url'); $ip_address = $row->getDestinationProperty('ip_address'); if (empty($author_name) || empty($author_email)) { // Post comment as created by site's moderator. $disqus->posts->create(array('message' => $message, 'thread' => $thread->id, 'access_token' => $this->config->get('advanced.disqus_useraccesstoken'), 'date' => $date, 'ip_address' => $ip_address)); } else { // Cannot create comment as anonymous user, needs 'api_key' // (api_key is not the public key). $disqus->posts->create(array('thread' => $thread, 'message' => $message, 'author_name' => $author_name, 'author_email' => $author_email, 'author_url' => $author_url, 'date' => $date, 'ip_address' => $ip_address)); } return TRUE; } catch (\Exception $exception) { $this->logger->error('Error creating post on thread @thread.', array('@thread' => $thread->id)); } return FALSE; } }
/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $path = $this->aliasStorage->save($row->getDestinationProperty('source'), $row->getDestinationProperty('alias'), $row->getDestinationProperty('langcode'), $old_destination_id_values ? $old_destination_id_values[0] : NULL); return array($path['pid']); }
/** * Update an entity with the new values from row. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to update. * @param \Drupal\migrate\Row $row * The row object to update from. */ protected function updateEntity(EntityInterface $entity, Row $row) { // If the migration has specified a list of properties to be overwritten, // clone the row with an empty set of destination values, and re-add only // the specified properties. if (isset($this->configuration['overwrite_properties'])) { $clone = $row->cloneWithoutDestination(); foreach ($this->configuration['overwrite_properties'] as $property) { $clone->setDestinationProperty($property, $row->getDestinationProperty($property)); } $row = $clone; } foreach ($row->getDestination() as $field_name => $values) { $field = $entity->{$field_name}; if ($field instanceof TypedDataInterface) { $field->setValue($values); } } $this->setRollbackAction($row->getIdMap()); }
/** * Determines how to handle file conflicts. * * @param \Drupal\migrate\Row $row * * @return integer * Either FILE_EXISTS_REPLACE (default) or FILE_EXISTS_RENAME, depending * on the current configuration. */ protected function getOverwriteMode(Row $row) { if (!empty($this->configuration['rename'])) { $entity_id = $row->getDestinationProperty($this->getKey('id')); if ($entity_id && ($entity = $this->storage->load($entity_id))) { return FILE_EXISTS_RENAME; } } return FILE_EXISTS_REPLACE; }