Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
Project
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Gorodkov Denis
Project
Commits
3ff49535
Commit
3ff49535
authored
May 06, 2022
by
Gorodkov Denis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add drush command
parent
7c6f071e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
132 additions
and
45 deletions
+132
-45
mymodule.module
app/docroot/modules/custom/mymodule/mymodule.module
+0
-2
mymodule.services.yml
app/docroot/modules/custom/mymodule/mymodule.services.yml
+4
-0
API.php
app/docroot/modules/custom/mymodule/src/API.php
+39
-0
BatchCommands.php
...ot/modules/custom/mymodule/src/Commands/BatchCommands.php
+87
-0
QueueSwapi.php
...les/custom/mymodule/src/Plugin/QueueWorker/QueueSwapi.php
+2
-43
No files found.
app/docroot/modules/custom/mymodule/mymodule.module
View file @
3ff49535
...
@@ -82,5 +82,3 @@ function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_
...
@@ -82,5 +82,3 @@ function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_
$form
[
"actions"
][
"reset"
][
"#access"
]
=
true
;
$form
[
"actions"
][
"reset"
][
"#access"
]
=
true
;
}
}
}
}
app/docroot/modules/custom/mymodule/mymodule.services.yml
View file @
3ff49535
...
@@ -2,3 +2,7 @@ services:
...
@@ -2,3 +2,7 @@ services:
mymodule.custom_services
:
mymodule.custom_services
:
class
:
Drupal\mymodule\API
class
:
Drupal\mymodule\API
arguments
:
[
'
@http_client'
]
arguments
:
[
'
@http_client'
]
mymodule.commands
:
class
:
\Drupal\mymodule\Commands\BatchCommands
tags
:
-
{
name
:
drush.command
}
app/docroot/modules/custom/mymodule/src/API.php
View file @
3ff49535
...
@@ -60,4 +60,43 @@ class API {
...
@@ -60,4 +60,43 @@ class API {
$queue_api
->
createItem
(
$item
);
$queue_api
->
createItem
(
$item
);
}
}
public
function
getNodeByUrl
(
$uri
)
{
$bundle
=
$this
->
getTypeNode
(
$uri
);
$swapi_id
=
$this
->
getSwapiId
(
$uri
);
$nodes
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'node'
)
->
loadByProperties
([
'type'
=>
$bundle
,
'field_swapi_id'
=>
$swapi_id
,
]);
foreach
(
$nodes
as
$value
)
{
$node
=
$value
;
}
return
$node
;
}
public
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'
and
$key
!=
'people_exclude'
);
if
(
$bool
and
!
is_array
(
$value
))
{
!
empty
(
$value
)
?
$node
->
set
(
"field_"
.
strtolower
(
$key
),
$value
)
:
''
;
}
if
(
is_array
(
$value
))
{
$node
->
set
(
$field_name
,
NULL
);
foreach
(
$value
as
$item
)
{
$nodeAdd
=
$this
->
getNodeByUrl
(
$item
);
!
empty
(
$nodeAdd
)
?
$node
->
$field_name
[]
=
$nodeAdd
:
''
;
}
}
if
(
$key
==
'homeworld'
and
!
empty
(
$value
))
{
$node
->
$field_name
=
$this
->
getNodeByUrl
(
$value
);
}
if
(
$key
==
'name'
or
$key
==
'title'
)
{
$node
->
setTitle
(
$value
);
}
}
$node
->
save
();
}
}
}
app/docroot/modules/custom/mymodule/src/Commands/BatchCommands.php
0 → 100644
View file @
3ff49535
<?php
namespace
Drupal\mymodule\Commands
;
use
Drush\Commands\DrushCommands
;
/**
* A Drush command file.
*
* @package Drupal\mymodule\Commands
*/
class
BatchCommands
extends
DrushCommands
{
/**
* Drush command that displays the given text.
*
* @param string $date
* @command mymodule:custom
* @aliases custom
* @usage drush custom $date
*/
public
function
custom
(
$date
=
'01.01.1970'
)
{
$api
=
\Drupal
::
service
(
'mymodule.custom_services'
);
$result_api
=
$api
->
getResponse
(
'https://swapi.dev/api'
);
$queue_api
=
\Drupal
::
queue
(
'mymodule_swapi_drush'
);
$count_create_node
=
0
;
$count_queue
=
0
;
$queue_api
->
deleteQueue
();
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
))
{
$swapi_edited
=
date_parse
(
$value
[
'edited'
]);
$swapi_edited
=
$swapi_edited
[
'day'
]
.
"."
.
$swapi_edited
[
'month'
]
.
"."
.
$swapi_edited
[
'year'
];
$swapi_edited
=
strtotime
(
$swapi_edited
);
$date
=
strtotime
(
$date
);
if
(
$date
-
$swapi_edited
<
0
)
{
$queue_api
->
createItem
(
$value
);
$count_queue
++
;
}
}
else
{
$api
->
createNode
(
$value
);
$title
=
!
empty
(
$value
[
'title'
])
?
$value
[
'title'
]
:
$value
[
'name'
];
$this
->
output
()
->
writeln
(
"Страница
$title
создана"
);
$count_create_node
++
;
$queue_api
->
createItem
(
$value
);
}
}
$uri
=
$result
[
'next'
];
}
}
else
{
$this
->
output
()
->
writeln
(
"Content type
$bundle
не создан"
);
}
}
while
(
$item
=
$queue_api
->
claimItem
())
{
if
(
!
empty
(
$item
->
data
[
'url'
]))
{
$node
=
$api
->
getNodeByUrl
(
$item
->
data
[
'url'
]);
$api
->
edited
(
$item
->
data
,
$node
);
$title
=
!
empty
(
$item
->
data
[
'title'
])
?
$item
->
data
[
'title'
]
:
$item
->
data
[
'name'
];
$message
=
"Страница
$title
обновлена"
;
$this
->
output
()
->
writeln
(
$message
);
$queue_api
->
deleteItem
(
$item
);
}
}
$this
->
output
()
->
writeln
(
"Страниц обновлено "
.
$count_queue
.
", страниц создано "
.
$count_create_node
);
}
}
app/docroot/modules/custom/mymodule/src/Plugin/QueueWorker/QueueSwapi.php
View file @
3ff49535
...
@@ -16,49 +16,8 @@ use Drupal\Core\Queue\QueueWorkerBase;
...
@@ -16,49 +16,8 @@ use Drupal\Core\Queue\QueueWorkerBase;
class
QueueSwapi
extends
QueueWorkerBase
{
class
QueueSwapi
extends
QueueWorkerBase
{
public
function
processItem
(
$data
)
{
public
function
processItem
(
$data
)
{
$node
=
$this
->
getNodeByUrl
(
$data
[
'url'
]);
$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'
and
$key
!=
'people_exclude'
);
if
(
$bool
and
!
is_array
(
$value
))
{
!
empty
(
$value
)
?
$node
->
set
(
"field_"
.
strtolower
(
$key
),
$value
)
:
''
;
}
if
(
is_array
(
$value
))
{
$node
->
set
(
$field_name
,
NULL
);
foreach
(
$value
as
$item
)
{
$nodeAdd
=
$this
->
getNodeByUrl
(
$item
);
!
empty
(
$nodeAdd
)
?
$node
->
$field_name
[]
=
$nodeAdd
:
''
;
}
}
if
(
$key
==
'homeworld'
and
!
empty
(
$value
))
{
$node
->
$field_name
=
$this
->
getNodeByUrl
(
$value
);
}
if
(
$key
==
'name'
or
$key
==
'title'
)
{
$node
->
setTitle
(
$value
);
}
}
$node
->
save
();
}
private
function
getNodeByUrl
(
$uri
)
{
$api
=
\Drupal
::
service
(
'mymodule.custom_services'
);
$api
=
\Drupal
::
service
(
'mymodule.custom_services'
);
$bundle
=
$api
->
getTypeNode
(
$uri
);
$node
=
$api
->
getNodeByUrl
(
$data
[
'url'
]);
$swapi_id
=
$api
->
getSwapiId
(
$uri
);
$api
->
edited
(
$data
,
$node
);
$nodes
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'node'
)
->
loadByProperties
([
'type'
=>
$bundle
,
'field_swapi_id'
=>
$swapi_id
,
]);
foreach
(
$nodes
as
$value
)
{
$node
=
$value
;
}
return
$node
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment