Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
codit_batch_operations
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
codit_batch_operations
Commits
d123da17
Commit
d123da17
authored
6 months ago
by
Steve Wirt
Browse files
Options
Downloads
Plain Diff
Merge branch '3425031-add-script-example' into '1.0.x'
Issue
#3425031
: Add script example for node operation See merge request
!28
parents
e42a54e8
344cc651
No related branches found
No related tags found
No related merge requests found
Pipeline
#349823
passed with warnings
6 months ago
Stage: build
Stage: validate
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cbo_scripts/Example-ArchiveOldNodes.php.txt
+93
-0
93 additions, 0 deletions
src/cbo_scripts/Example-ArchiveOldNodes.php.txt
with
93 additions
and
0 deletions
src/cbo_scripts/Example-ArchiveOldNodes.php.txt
0 → 100644
+
93
−
0
View file @
d123da17
<?php
namespace Drupal\codit_batch_operations\cbo_scripts;
use Drupal\codit_batch_operations\BatchOperations;
use Drupal\codit_batch_operations\BatchScriptInterface;
/**
* An example Batch Operation to show archiving old nodes based on date by cron.
*/
class ExampleArchiveOldNodes extends BatchOperations implements BatchScriptInterface {
/**
* {@inheritdoc}
*/
public function getTitle():string {
return 'Archive old news node items';
}
/**
* {@inheritdoc}
*/
public function getDescription():string {
$description = <<<ENDHERE
News items that have not been updated in more than a year old should be
archived as they are no longer news. This handles that using cron.
ENDHERE;
return $description;
}
/**
* {@inheritdoc}
*/
public function getCompletedMessage(): string {
return 'Archived @completed out of @total news items.';
}
/**
* Optionally name the main type of thing being operated on.
*
* Suggestions: node, term, menu_item, block, bundle name, user, etc.
*
* If you do not define this, it defaults to 'item'.
*
* @return string
* The string that will be used to prefix items in the logging.
*/
public function getItemType(): string {
return 'node:news';
}
/**
* {@inheritdoc}
*/
public function gatherItemsToProcess(): array {
$time_to_archive_before = strtotime("- 1 year");
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->condition('type', 'news', '=')
// Only get published items that have not been touched in a year.
->condition('changed', $time_to_archive_before, '<=')
->condition( 'status' , 1)
->accessCheck(FALSE);
$results = $query->execute();
// Remove the revision ids (keys) from the Node ids (values).
$items = array_values($results);
return $items;
}
/**
* {@inheritdoc}
*/
public function processOne(string $key, mixed $item, array &$sandbox): string {
$node = $this->getNodeLatestRevision((int) $item);
$node->set('moderation_state', 'archived');
$revision_log_message = 'Archived this News item because it was over a year since the last update.';
$create_new_revision = TRUE;
$this->saveNodeRevision($node, $revision_log_message, $create_new_revision);
return "/node/{$item} Archived '{$node->getTitle()}'.";
}
/**
* {@inheritdoc}
*/
public function getCronTiming(): string | array {
// Consult the README for supported patterns.
// We only want this to run once per month.
return ['on the 1st of every 1 month'];
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment