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
e8f22fb4
Commit
e8f22fb4
authored
Jun 19, 2022
by
Gorodkov Denis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor batch
parent
7259d77e
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
143 additions
and
2 deletions
+143
-2
batch.info.yml
app/docroot/modules/custom/batch/batch.info.yml
+7
-0
batch.routing.yml
app/docroot/modules/custom/batch/batch.routing.yml
+6
-0
batch.services.yml
app/docroot/modules/custom/batch/batch.services.yml
+5
-0
ImportSwapi.php
app/docroot/modules/custom/batch/src/Batch/ImportSwapi.php
+62
-0
BatchCommand.php
...ocroot/modules/custom/batch/src/Commands/BatchCommand.php
+28
-0
BatchForm.php
app/docroot/modules/custom/batch/src/Form/BatchForm.php
+33
-0
DrushCommand.php
...ocroot/modules/custom/drush/src/Commands/DrushCommand.php
+2
-2
No files found.
app/docroot/modules/custom/batch/batch.info.yml
0 → 100755
View file @
e8f22fb4
name
:
Batch
description
:
Custom task
package
:
Tasks
type
:
module
core
:
8.x
core_version_requirement
:
^8 || ^9
app/docroot/modules/custom/batch/batch.routing.yml
0 → 100644
View file @
e8f22fb4
batch.batch_form
:
path
:
'
/admin/config/system/batch-form'
defaults
:
_form
:
'
\Drupal\batch\Form\BatchForm'
requirements
:
_permission
:
'
administer
site
configuration'
app/docroot/modules/custom/batch/batch.services.yml
0 → 100644
View file @
e8f22fb4
services
:
batch.drush_command
:
class
:
Drupal\batch\Commands\BatchCommand
tags
:
-
{
name
:
drush.command
}
app/docroot/modules/custom/batch/src/Batch/ImportSwapi.php
0 → 100644
View file @
e8f22fb4
<?php
namespace
Drupal\batch\Batch
;
class
ImportSwapi
{
private
$batch
;
const
STEP
=
1
;
public
function
__construct
(
$title
=
'Nodes import'
)
{
$this
->
batch
=
[
'title'
=>
$title
];
}
public
function
getOperations
(
$date
=
null
)
{
$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'
]);
}
}
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
)
{
$this
->
setOperation
([
$item
]);
}
}
}
public
function
processItem
(
$data
)
{
$swapi
=
\Drupal
::
service
(
'swapi.service_swapi'
);
if
(
!
empty
(
$data
[
'url'
]))
{
$node
=
$swapi
->
getNodeFromUrl
(
$data
[
'url'
]);
if
(
$node
)
{
$swapi
->
update
(
$data
,
$node
);
}
else
{
$node
=
$swapi
->
createNode
(
$data
);
$swapi
->
update
(
$data
,
$node
);
}
}
}
public
function
setOperation
(
$data
)
{
$this
->
batch
[
'operations'
][]
=
[[
$this
,
'processItem'
],
$data
];
}
public
function
setBatch
()
{
batch_set
(
$this
->
batch
);
}
}
app/docroot/modules/custom/batch/src/Commands/BatchCommand.php
0 → 100644
View file @
e8f22fb4
<?php
namespace
Drupal\batch\Commands
;
use
Drush\Commands\DrushCommands
;
use
Drupal\batch\Batch\ImportSwapi
;
/**
* A Drush command file.
*
* @package Drupal\batch\Commands
*/
class
BatchCommand
extends
DrushCommands
{
/**
* Drush command import from swapi
*
* @param string $date
* @command batch:ImportNodes
* @aliases import_nodes_batch
* @usage drush import_nodes_batch $date
*/
public
function
ImportNodes
(
$date
=
null
)
{
$import
=
new
ImportSwapi
();
$import
->
getOperations
();
$import
->
setBatch
();
drush_backend_batch_process
();
}
}
app/docroot/modules/custom/batch/src/Form/BatchForm.php
0 → 100644
View file @
e8f22fb4
<?php
namespace
Drupal\batch\Form
;
use
Drupal\Core\Form\FormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\batch\Batch\ImportSwapi
;
class
BatchForm
extends
FormBase
{
public
function
getFormId
()
{
return
'batch_form'
;
}
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$form
[
'date'
]
=
array
(
'#type'
=>
'datetime'
,
'#title'
=>
'Date and time'
,
);
$form
[
'actions'
][
'submit'
]
=
array
(
'#type'
=>
'submit'
,
'#value'
=>
'Import'
,
'#button_type'
=>
'primary'
,
);
return
$form
;
}
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$batch
=
new
ImportSwapi
();
$batch
->
getOperations
();
$batch
->
setBatch
();
}
}
app/docroot/modules/custom/drush/src/Commands/DrushCommand.php
View file @
e8f22fb4
...
...
@@ -14,11 +14,11 @@ class DrushCommand extends DrushCommands {
* Drush command for swapi
*
* @param string $date
* @command drush:
getNodes
* @command drush:
import
* @aliases import_nodes
* @usage drush import_nodes $date
*/
public
function
getNodes
(
$date
=
null
)
{
public
function
import
(
$date
=
null
)
{
/**
* @var Drupal\swapi\Service\swapi
*/
...
...
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