Commit 7259d77e authored by Gorodkov Denis's avatar Gorodkov Denis

Refactor Drush command

parent 9a418fb3
name: Drush
description: Custom task
package: Tasks
type: module
core: 8.x
core_version_requirement: ^8 || ^9
services:
drush.commands:
class: Drupal\drush\Commands\DrushCommand
tags:
- { name: drush.command }
<?php
namespace Drupal\drush\Commands;
use Drush\Commands\DrushCommands;
/**
* A Drush command file.
*
* @package Drupal\drush\Commands
*/
class DrushCommand extends DrushCommands {
/**
* Drush command for swapi
*
* @param string $date
* @command drush:getNodes
* @aliases import_nodes
* @usage drush import_nodes $date
*/
public function getNodes($date = null) {
/**
* @var Drupal\swapi\Service\swapi
*/
$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']);
}
}
$count_update = 0;
$count_create = 0;
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) {
$node = $swapi->getNodeFromUrl($item['url']);
if ($node) {
$swapi->update($item, $node);
$count_update++;
} else {
$node = $swapi->createNode($item);
$swapi->update($item, $node);
$count_create++;
}
}
}
$this->output->writeln('Нод создано: ' . $count_create);
$this->output->writeln('Нод обновлено: ' . $count_update);
}
}
......@@ -50,25 +50,6 @@ function mymodule_cron() {
Drupal::state()->set('date_queue_swapi', $time);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_contact_message_feedback_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$uid = \Drupal::currentUser()->id();
if ($uid) {
$user = \Drupal::entityTypeManager()->getStorage('user')->load($uid);
$name = $user->field_name->getValue();
$surname = $user->field_surname->getValue();
if (!empty($name[0]['value'])) {
$form['field_name']['widget'][0]['value']['#default_value'] = $name[0]['value'];
}
if (!empty($surname[0]['value'])) {
$form['field_surname']['widget'][0]['value']['#default_value'] = $surname[0]['value'];
}
}
}
function mymodule_preprocess_field_multiple_value_form(&$variables) {
$path = \Drupal::service('path.current')->getPath();
......
......@@ -12,7 +12,7 @@ use Drupal\mymodule\Batch\Import;
*/
class BatchCommands extends DrushCommands {
/**
* Drush command that displays the given text.
* Drush command import from swapi
*
* @param string $time
* @command mymodule:custom
......@@ -20,10 +20,10 @@ class BatchCommands extends DrushCommands {
* @usage drush custom $time
*/
public function custom($time = '01.01.1970') {
$import = new Import();
/*$import = new Import();
$import->getNodes();
batch_set($import->batch);
drush_backend_batch_process();
drush_backend_batch_process();*/
/*$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
......
......@@ -62,9 +62,7 @@ class swapi {
'field_swapi_id' => $swapi_id,
]);
$node = current($nodes);
return isset($node) ? $node : null;
return !empty($nodes) ? current($nodes) : null;
}
public function update($data, $node) {
......@@ -107,6 +105,7 @@ class swapi {
]);
$node->setUnpublished();
$node->save();
return $node;
}
public function getSwapiId($url) {
......
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