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
7259d77e
Commit
7259d77e
authored
Jun 18, 2022
by
Gorodkov Denis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Drush command
parent
9a418fb3
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
79 additions
and
25 deletions
+79
-25
drush.info.yml
app/docroot/modules/custom/drush/drush.info.yml
+7
-0
drush.services.yml
app/docroot/modules/custom/drush/drush.services.yml
+5
-0
DrushCommand.php
...ocroot/modules/custom/drush/src/Commands/DrushCommand.php
+62
-0
mymodule.module
app/docroot/modules/custom/mymodule/mymodule.module
+0
-19
BatchCommands.php
...ot/modules/custom/mymodule/src/Commands/BatchCommands.php
+3
-3
swapi.php
app/docroot/modules/custom/swapi/src/Service/swapi.php
+2
-3
No files found.
app/docroot/modules/custom/drush/drush.info.yml
0 → 100755
View file @
7259d77e
name
:
Drush
description
:
Custom task
package
:
Tasks
type
:
module
core
:
8.x
core_version_requirement
:
^8 || ^9
app/docroot/modules/custom/drush/drush.services.yml
0 → 100644
View file @
7259d77e
services
:
drush.commands
:
class
:
Drupal\drush\Commands\DrushCommand
tags
:
-
{
name
:
drush.command
}
app/docroot/modules/custom/drush/src/Commands/DrushCommand.php
0 → 100644
View file @
7259d77e
<?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
);
}
}
app/docroot/modules/custom/mymodule/mymodule.module
View file @
7259d77e
...
@@ -50,25 +50,6 @@ function mymodule_cron() {
...
@@ -50,25 +50,6 @@ function mymodule_cron() {
Drupal
::
state
()
->
set
(
'date_queue_swapi'
,
$time
);
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
)
{
function
mymodule_preprocess_field_multiple_value_form
(
&
$variables
)
{
$path
=
\Drupal
::
service
(
'path.current'
)
->
getPath
();
$path
=
\Drupal
::
service
(
'path.current'
)
->
getPath
();
...
...
app/docroot/modules/custom/mymodule/src/Commands/BatchCommands.php
View file @
7259d77e
...
@@ -12,7 +12,7 @@ use Drupal\mymodule\Batch\Import;
...
@@ -12,7 +12,7 @@ use Drupal\mymodule\Batch\Import;
*/
*/
class
BatchCommands
extends
DrushCommands
{
class
BatchCommands
extends
DrushCommands
{
/**
/**
* Drush command
that displays the given text.
* Drush command
import from swapi
*
*
* @param string $time
* @param string $time
* @command mymodule:custom
* @command mymodule:custom
...
@@ -20,10 +20,10 @@ class BatchCommands extends DrushCommands {
...
@@ -20,10 +20,10 @@ class BatchCommands extends DrushCommands {
* @usage drush custom $time
* @usage drush custom $time
*/
*/
public
function
custom
(
$time
=
'01.01.1970'
)
{
public
function
custom
(
$time
=
'01.01.1970'
)
{
$import
=
new
Import
();
/*
$import = new Import();
$import->getNodes();
$import->getNodes();
batch_set($import->batch);
batch_set($import->batch);
drush_backend_batch_process
();
drush_backend_batch_process();
*/
/*$api = \Drupal::service('mymodule.custom_services');
/*$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
$result_api = $api->getResponse('https://swapi.dev/api');
...
...
app/docroot/modules/custom/swapi/src/Service/swapi.php
View file @
7259d77e
...
@@ -62,9 +62,7 @@ class swapi {
...
@@ -62,9 +62,7 @@ class swapi {
'field_swapi_id'
=>
$swapi_id
,
'field_swapi_id'
=>
$swapi_id
,
]);
]);
$node
=
current
(
$nodes
);
return
!
empty
(
$nodes
)
?
current
(
$nodes
)
:
null
;
return
isset
(
$node
)
?
$node
:
null
;
}
}
public
function
update
(
$data
,
$node
)
{
public
function
update
(
$data
,
$node
)
{
...
@@ -107,6 +105,7 @@ class swapi {
...
@@ -107,6 +105,7 @@ class swapi {
]);
]);
$node
->
setUnpublished
();
$node
->
setUnpublished
();
$node
->
save
();
$node
->
save
();
return
$node
;
}
}
public
function
getSwapiId
(
$url
)
{
public
function
getSwapiId
(
$url
)
{
...
...
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