Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created February 9, 2017 05:35
Show Gist options
  • Select an option

  • Save cyberlex404/b8909713fcd2b140872dbc27b9c5578f to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/b8909713fcd2b140872dbc27b9c5578f to your computer and use it in GitHub Desktop.
batch init form
<?php
namespace Drupal\housing_port\Form;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Queue\Batch;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\node\NodeStorageInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Stream;
use Drupal\user\Entity\User;
use Drupal\gallery\Entity\Gallery;
use Drupal\housing_gallery\Entity\HousingGallery;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class MigrateRealty.
*
* @package Drupal\realty_migrate\Form
*/
class MigrateHousingContent extends FormBase {
/**
* @var AccountInterface $account
*/
protected $account;
/**
* @var \Drupal\Core\Config\Entity\Query\QueryFactory
*/
private $entity_query;
/**
* @var \Drupal\node\NodeStorageInterface
*/
protected $node_storage;
/**
* Class constructor.
*/
public function __construct(AccountInterface $account, QueryFactory $entity_query, NodeStorageInterface $node_storage) {
$this->account = $account;
$this->node_storage = $node_storage;
$this->entity_query = $entity_query;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
// Load the service required to construct this class.
$container->get('current_user'),
$container->get('entity.query'),
$container->get('entity.manager')->getStorage('node')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'migrate_housing_content';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$query = $this->entity_query->get('node')
->condition('type', 'housing')
->sort('field_old_nid', 'ASC')
//->condition('uid', 0)
->condition('status', 1);
$ids = $query->execute();
// dpm($ids, 'pre batch');
$nodes = $this->node_storage->loadMultiple($ids);
// Prepare batch operations array.
foreach ($nodes as $node) {
if ($node->id()) {
$operations[] = array(
'\Drupal\housing_port\HousingUpdater::execute',
array($node),
);
}
}
if (!empty($operations)) {
$batch = array(
'operations' => $operations,
'title' => t('Update housing nodes'),
'init_message' => t('Job housing is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('error.'),
'finished' => '\Drupal\housing_port\HousingUpdater::finishedCallback',
);
batch_set($batch);
}
else {
drupal_set_message($this->t('No data for job'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment