Commit e8f22fb4 authored by Gorodkov Denis's avatar Gorodkov Denis

refactor batch

parent 7259d77e
name: Batch
description: Custom task
package: Tasks
type: module
core: 8.x
core_version_requirement: ^8 || ^9
batch.batch_form:
path: '/admin/config/system/batch-form'
defaults:
_form: '\Drupal\batch\Form\BatchForm'
requirements:
_permission: 'administer site configuration'
services:
batch.drush_command:
class: Drupal\batch\Commands\BatchCommand
tags:
- { name: drush.command }
<?php
namespace Drupal\batch\Batch;
class ImportSwapi {
private $batch;
const STEP = 1;
public function __construct($title = 'Nodes import') {
$this->batch = [
'title' => $title
];
}
public function getOperations($date = null) {
$swapi = \Drupal::service('swapi.service_swapi');
$date = strtotime($date);
$urls = $swapi->getResponse();
$items = [];
foreach ($urls as $url) {
$result = $swapi->setUrl($url)
->getResponse();
$items = array_merge($items, $result['results']);
while ($result['next']) {
$result = $swapi->setUrl($result['next'])
->getResponse();
$items = array_merge($items, $result['results']);
}
}
foreach ($items as $item) {
$swapi_edited = date_parse($item['edited']);
$swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year'];
$swapi_edited = strtotime($swapi_edited);
if ($swapi_edited > $date) {
$this->setOperation([$item]);
}
}
}
public function processItem($data) {
$swapi = \Drupal::service('swapi.service_swapi');
if (!empty($data['url'])) {
$node = $swapi->getNodeFromUrl($data['url']);
if ($node) {
$swapi->update($data, $node);
} else {
$node = $swapi->createNode($data);
$swapi->update($data, $node);
}
}
}
public function setOperation($data) {
$this->batch['operations'][] = [[$this, 'processItem'], $data];
}
public function setBatch() {
batch_set($this->batch);
}
}
<?php
namespace Drupal\batch\Commands;
use Drush\Commands\DrushCommands;
use Drupal\batch\Batch\ImportSwapi;
/**
* A Drush command file.
*
* @package Drupal\batch\Commands
*/
class BatchCommand extends DrushCommands {
/**
* Drush command import from swapi
*
* @param string $date
* @command batch:ImportNodes
* @aliases import_nodes_batch
* @usage drush import_nodes_batch $date
*/
public function ImportNodes($date = null) {
$import = new ImportSwapi();
$import->getOperations();
$import->setBatch();
drush_backend_batch_process();
}
}
<?php
namespace Drupal\batch\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\batch\Batch\ImportSwapi;
class BatchForm extends FormBase {
public function getFormId() {
return 'batch_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['date'] = array(
'#type' => 'datetime',
'#title' => 'Date and time',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Import',
'#button_type' => 'primary',
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = new ImportSwapi();
$batch->getOperations();
$batch->setBatch();
}
}
......@@ -14,11 +14,11 @@ class DrushCommand extends DrushCommands {
* Drush command for swapi
*
* @param string $date
* @command drush:getNodes
* @command drush:import
* @aliases import_nodes
* @usage drush import_nodes $date
*/
public function getNodes($date = null) {
public function import($date = null) {
/**
* @var Drupal\swapi\Service\swapi
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment