Created
February 9, 2017 05:34
-
-
Save cyberlex404/80354670d4a75410eedf41009a174ef1 to your computer and use it in GitHub Desktop.
batch class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\housing_port; | |
use Drupal\Core\Url; | |
use Drupal\node\Entity\Node; | |
use Drupal\Core\Database\Database; | |
use Drupal\node\NodeInterface; | |
use Drupal\taxonomy\Entity\Term; | |
use Drupal\user\Entity\User; | |
use Drupal\file\Entity\File; | |
class HousingUpdater { | |
public static function execute(NodeInterface $node, &$context){ | |
$message = 'Job is ' . $node->label() . ': '; | |
//$root_path = Url::fromRoute('entity.node.canonical', ['node' => $node->id()])->toString(); | |
//$root_alias = $path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($root_path); | |
//$message .= $root_path . ' ' . $root_alias; | |
//$housing_page = \Drupal::entityQuery('housing_page')->execute(); | |
//dpm($housing_page); | |
$gs = []; | |
$file = File::load(1); | |
$uid = $node->getOwnerId(); | |
if(!$node->field_housing_galleries->isEmpty()) { | |
foreach ($node->field_housing_galleries->getIterator() as $item) { | |
$gallery = $item->entity; | |
$gallery->setOwnerId($uid); | |
$gallery->save(); | |
foreach ($gallery->field_photos->getIterator() as $item) { | |
//$file = $item->entity; | |
//$file->setOwnerId($uid); | |
//$file->save(); | |
} | |
// | |
} | |
} | |
//$urls[0]['alias'] = $this->alias_storage->getAliasByPath($urls[0]['url'], 'ru'); | |
/* | |
$field = 'category'; | |
$oid = $node->get('field_old_nid')->value; | |
$other_database = array( | |
'database' => 'user1106073_braslav2016', | |
'username' => 'braslav2016', | |
'password' => 'TErjukBM', | |
'host' => 'localhost', | |
'driver' => 'mysql', | |
); | |
Database::addConnectionInfo('other', 'default', $other_database); | |
Database::setActiveConnection('other'); | |
//user1106073_braslav2016.file_managed user1106073_braslav2016.field_data_category.category_target_id | |
$result = db_select('field_data_'. $field, 'p'); | |
//$result->leftJoin('file_managed', 'f', 'p.phototrio_fid = f.fid'); | |
$result->fields('p'); | |
$result->condition('p.entity_id', $oid); | |
$values = $result->execute()->fetchAllAssoc('category_target_id'); | |
Database::setActiveConnection(); | |
// dpm($context); | |
$items = []; | |
foreach ($values as $item) { | |
$query = \Drupal::entityQuery('taxonomy_term') | |
->condition('field_oid', $item->category_target_id); | |
$ids = $query->execute(); | |
if(!empty($ids)) { | |
$entry = Term::load(reset($ids)); | |
$items[]= $entry; | |
$message .= $entry->label(). '; '; | |
} | |
} | |
if(count($items)) { | |
//drupal_set_message('Есть совпадение по ' . $node->id() . ' ' . $node->label() ); | |
$node->set('field_tags', $items); | |
$node->save(); | |
} | |
*/ | |
// $node->set('field_header_type', 'auto'); | |
// $node->save(); | |
$context['message'] = $message; | |
$context['results']['nodes'][] = [ | |
'id' => $node->id(), | |
'gs' => $gs, | |
//'path' => $root_path, | |
// 'alias' => $root_alias, | |
// 'oid' => $oid, | |
// 'values' => $values, | |
// 'items' => $items, | |
]; | |
} | |
public static function finishedCallback($success, $results, $operations) { | |
// The 'success' parameter means no fatal PHP errors were detected. All | |
// other error management should be handled using 'results'. | |
if ($success) { | |
dpm($results); | |
$message = \Drupal::translation()->formatPlural( | |
count($results['nodes']), | |
'One post processed.', '@count posts processed.' | |
); | |
} | |
else { | |
$message = t('Finished with an error.'); | |
} | |
drupal_set_message($message); | |
} | |
private function MigrateDB() { | |
} | |
private function getField($nid) { | |
$values = []; | |
return $values; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment