Commit fd76bad4 authored by Gorodkov Denis's avatar Gorodkov Denis

commit

parent 15b5d6f2
......@@ -37,6 +37,7 @@
"drupal/rabbit_hole": "^1.0@beta",
"drupal/recaptcha": "^3.0",
"drupal/redirect": "^1.7",
"drupal/restui": "^1.20",
"drupal/search_api": "^1.23",
"drupal/search_api_autocomplete": "^1.6",
"drupal/search_api_exclude": "^2.0",
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c7d3d0deb367b8080feafd1b3f5de48d",
"content-hash": "786aa69208b3efb27fa74ecad0d06e91",
"packages": [
{
"name": "asm89/stack-cors",
......@@ -4074,6 +4074,62 @@
"source": "https://git.drupalcode.org/project/redirect"
}
},
{
"name": "drupal/restui",
"version": "1.20.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/restui.git",
"reference": "8.x-1.20"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/restui-8.x-1.20.zip",
"reference": "8.x-1.20",
"shasum": "df1d3c486ee0e7b4e9a24e6523a69c9efe73caff"
},
"require": {
"drupal/core": "^8.7.7 || ^9"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "8.x-1.20",
"datestamp": "1616839543",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
}
}
},
"notification-url": "https://packages.drupal.org/8/downloads",
"license": [
"GPL-2.0-or-later"
],
"authors": [
{
"name": "-enzo-",
"homepage": "https://www.drupal.org/user/294937"
},
{
"name": "clemens.tolboom",
"homepage": "https://www.drupal.org/user/125814"
},
{
"name": "juampynr",
"homepage": "https://www.drupal.org/user/682736"
},
{
"name": "klausi",
"homepage": "https://www.drupal.org/user/262198"
}
],
"description": "Provides a user interface to manage REST resources.",
"homepage": "https://www.drupal.org/project/restui",
"support": {
"source": "https://git.drupalcode.org/project/restui"
}
},
{
"name": "drupal/search_api",
"version": "1.23.0",
......
......@@ -16,12 +16,12 @@ class API {
$request = $this->httpClient->request('GET', $uri);
$responseCode = $request->getStatusCode();
if ($responseCode == 200) {
#if ($responseCode == 200) {
$response = $request->getBody()->getContents();
$response = json_decode($response, true);
return $response;
}
return false;
#}
#return false;
}
public function getTypeNode($uri) {
......@@ -99,18 +99,4 @@ class API {
}
$node->save();
}
public function batch() {
$nids = \Drupal::entityQuery('node')->condition('type','people')->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$operations = [];
foreach ($nodes as $node) {
$operations[] = ['callback', $node];
}
batch_set([
'title' => 'Nodes import',
'operations' => $operations,
]);
}
}
......@@ -4,6 +4,7 @@ namespace Drupal\mymodule\Batch;
class Import {
public $batch;
public $operations;
public function __construct() {
$this->batch = [
......@@ -14,6 +15,7 @@ class Import {
public function getNodes($time = '01.01.1970') {
$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
$this->operations = [];
foreach($result_api as $item) {
$bundle = $api->getTypeNode($item);
......@@ -39,6 +41,7 @@ class Import {
$this->setOperation([$value]);
}
}
#$uri = '';
$uri = $result['next'];
}
} else {
......@@ -66,6 +69,7 @@ class Import {
}
public function finished($success, $results, $operations) {
$a = 1;
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One post processed.', '@count posts processed.');
......
......@@ -30,10 +30,12 @@ class BatchForm extends FormBase {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$date = $form_state->getValue('date');
$date = $date->getTimestamp();
$this->startImport();
#$date = $form_state->getValue('date');
#$date = $date->getTimestamp();
#$this->startImport();
$import = new Import();
$import->getNodes();
batch_set($import->batch);
}
public function startImport() {
......
<?php
namespace Drupal\mymodule\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
/**
* Provides a resource to get people
*
* @RestResource(
* id = "get_people",
* label = @Translation("Get people"),
* uri_paths = {
* "canonical" = "/api/people"
* }
* )
*/
class GetPeople extends ResourceBase {
public function get() {
$query = \Drupal::request()->query;
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => 'people'
]);
$response['count'] = count($nodes);
$response['next'] = '';
$response['previous'] = '';
$response['query'] = $query;
foreach ($nodes as $node) {
if (count($response['results']) < 11) {
$result['name'] = $node->label();
$result['height'] = $this->getFieldValue($node->field_height->getValue());
$result['mass'] = $this->getFieldValue($node->field_mass->getValue());
$result['hair_color'] = $this->getFieldValue($node->field_hair_color->getValue());
$result['skin_color'] = $this->getFieldValue($node->field_skin_color->getValue());
$result['eye_color'] = $this->getFieldValue($node->field_eye_color->getValue());
$result['birth_year'] = $this->getFieldValue($node->field_birth_year->getValue());
$result['gender'] = $this->getFieldValue($node->field_gender->getValue());
$node_homeworld = $node->field_homeworld;
$result['homeworld'] = $node_homeworld;
$response['results'][] = $result;
}
}
return new ResourceResponse($response);
}
public function getFieldValue($value) {
$value = $value[0]['value'];
return $value;
}
public function permissions()
{
}
}
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