Commit c5d0be44 authored by Gorodkov Denis's avatar Gorodkov Denis

Commit

parent f318f4b0
......@@ -40,6 +40,7 @@
"drupal/search_api": "^1.23",
"drupal/search_api_autocomplete": "^1.6",
"drupal/search_api_exclude": "^2.0",
"drupal/search_api_exclude_entity": "^1.3",
"drupal/shariff": "^1.7",
"drupal/simple_sitemap": "^4.1",
"drupal/smtp": "^1.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": "3df970584b1b9eb7ad06837036bba875",
"content-hash": "c7d3d0deb367b8080feafd1b3f5de48d",
"packages": [
{
"name": "asm89/stack-cors",
......@@ -4250,6 +4250,51 @@
"issues": "https://www.drupal.org/project/issues/search_api_exclude"
}
},
{
"name": "drupal/search_api_exclude_entity",
"version": "1.3.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/search_api_exclude_entity.git",
"reference": "8.x-1.3"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/search_api_exclude_entity-8.x-1.3.zip",
"reference": "8.x-1.3",
"shasum": "a4b8fd96bceedf4f6cff382f531585613325a702"
},
"require": {
"drupal/core": "^8 || ^9",
"drupal/search_api": "*"
},
"type": "drupal-module",
"extra": {
"drupal": {
"version": "8.x-1.3",
"datestamp": "1603201534",
"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": "beltofte",
"homepage": "https://www.drupal.org/user/151799"
}
],
"description": "Exclude one or more entities from being indexed in Search API.",
"homepage": "https://www.drupal.org/project/search_api_exclude_entity",
"support": {
"source": "https://git.drupalcode.org/project/search_api_exclude_entity"
}
},
{
"name": "drupal/shariff",
"version": "1.7.0",
......
......@@ -9,14 +9,45 @@
*/
function mymodule_cron() {
$api = \Drupal::service('mymodule.custom_services');
$queue_api = \Drupal::queue('mymodule_api');
$result_api = $api->getResponse('https://swapi.dev/api');
$queue_api = \Drupal::queue('mymodule_swapi');
foreach ($result_api as $item) {
$queue_api->createItem($item);
}
$date = new DateTime();
$date = $date->getTimestamp();
\Drupal::state()->set('date_queue_swapi', $date);
\Drupal::state()->set('date_queue_swapi', date());
foreach($result_api as $item) {
$bundle = $api->getTypeNode($item);
if ($api->checkBundle($bundle)) {
$uri = $item;
while (!empty($uri)) {
$result = $api->getResponse($uri);
foreach($result['results'] as $value) {
$bundle = $api->getTypeNode($value['url']);
$swapi_id = $api->getSwapiId($value['url']);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
foreach ($node as $val) {
$node_edited = $val;
}
if (!empty($node)) {
if ($node_edited->field_edited->value !== $value['edited']) {
$queue_api->createItem($value);
}
} else {
$api->createNode($value);
}
}
$uri = $result['next'];
}
} else {
\Drupal::logger('Queue mymodule_api')->notice("Content type $bundle не создан!");
}
}
}
/**
......@@ -38,6 +69,9 @@ function mymodule_form_contact_message_feedback_form_alter(&$form, \Drupal\Core\
}
}
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_id == 'views_exposed_form' and $form['#id'] == 'views-exposed-form-index-films-page-1') {
$form["actions"]["reset"]["#access"] = true;
......
services:
mymodule.custom_services:
class: Drupal\mymodule\API
arguments: ['@http_client']
......@@ -3,13 +3,12 @@
namespace Drupal\mymodule;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client;
class API {
private $httpClient;
protected $httpClient;
public function __construct() {
$this->httpClient = \Drupal::httpClient();
public function __construct(ClientInterface $httpClient) {
$this->httpClient = $httpClient;
}
public function getResponse($uri) {
......@@ -44,4 +43,21 @@ class API {
return false;
}
public function createNode($item) {
$queue_api = \Drupal::queue('mymodule_swapi');
$bundle = $this->getTypeNode($item['url']);
$swapi_id = (int)$this->getSwapiId($item['url']);
$title = $item[array_keys($item)[0]];
$node = \Drupal::entityTypeManager()
->getStorage('node')
->create([
'type' => $bundle,
'title' => $title,
'field_swapi_id' => $swapi_id
]);
$node->save();
$queue_api->createItem($item);
}
}
......@@ -8,27 +8,26 @@ use Drupal\Core\Queue\QueueWorkerBase;
* Comments
*
* @QueueWorker (
* id = "mymodule_update",
* title = "Queue update node",
* id = "mymodule_swapi",
* title = "Queue swapi",
* cron = {"time" = 60}
* )
*/
class QueueWorkerUpdate extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
class QueueSwapi extends QueueWorkerBase {
public function processItem($data) {
$node = $this->getNodeByUrl($data['url']);
if ($data['edited'] != $node->field_edited) {
$this->edited($data, $node);
}
}
private function edited($data, $node) {
foreach ($data as $key => $value) {
$field_name = "field_" . $key;
$bool = (bool)($key != 'name' and $key != 'title' and $key != 'homeworld' and $key != 'url' and $key != 'created');
$bool = (bool)($key != 'name' and $key != 'title' and $key != 'homeworld' and $key != 'url' and $key != 'created' and $key != 'people_exclude');
if ($bool and !is_array($value)) {
!empty($value) ? $node->set("field_" . strtolower($key), $value) : '';
}
......
<?php
namespace Drupal\mymodule\Plugin\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
/**
* Comments
*
* @QueueWorker (
* id = "mymodule_node",
* title = "Queue create node",
* cron = {"time" = 60}
* )
*/
class QueueWorkerNode extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
public function processItem($data) {
$api = \Drupal::service('mymodule.custom_services');
$queue_node_update = \Drupal::queue('mymodule_update');
$bundle = $api->getTypeNode($data['url']);
$swapi_id = (int)$api->getSwapiId($data['url']);
$title = $data[array_keys($data)[0]];
$node = \Drupal::entityTypeManager()
->getStorage('node')
->create([
'type' => $bundle,
'title' => $title,
'field_swapi_id' => $swapi_id
]);
$node->save();
$queue_node_update->createItem($data);
}
}
<?php
namespace Drupal\mymodule\Plugin\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
/**
* Comments
*
* @QueueWorker (
* id = "mymodule_api",
* title = "Queue api",
* cron = {"time" = 60}
* )
*/
class QueueWorkerSwapi extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
// $data приходит в виде https://swapi.dev/api/string/
public function processItem($data) {
$api = \Drupal::service('mymodule.custom_services');
$queue_node_update = \Drupal::queue('mymodule_update');
$queue_node = \Drupal::queue('mymodule_node');
$type_node = $api->getTypeNode($data);
$result = $api->getResponse($data);
$uri = $data;
if (!empty($result['results'])) {
do {
$result = $api->getResponse($uri);
foreach($result['results'] as $item) {
$bundle = $api->getTypeNode($item['url']);
$swapi_id = $api->getSwapiId($item['url']);
if ($api->checkBundle($bundle)) {
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
foreach ($node as $val) {
$node_edited = $val;
}
if (!empty($node)) {
if ($node_edited->field_edited->value !== $item['edited']) {
$queue_node_update->createItem($item);
}
} else {
$queue_node->createItem($item);
}
} else {
\Drupal::logger('Queue mymodule_api')->notice("Content type $type_node не создан!");
}
}
$uri = $result['next'];
} while(!empty($uri));
}
\Drupal::logger('Queue mymodule_api')->notice("Очередь по $type_node была выполнена");
}
}
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