Skip to content

Instantly share code, notes, and snippets.

@drubb
Last active April 27, 2023 08:02
Show Gist options
  • Save drubb/e09da66693d3d3605bcb6e369193549b to your computer and use it in GitHub Desktop.
Save drubb/e09da66693d3d3605bcb6e369193549b to your computer and use it in GitHub Desktop.
Simple Drush Script Example, would be executed using "drush scr series.php"
<?php
/**
* Scans for discontinued series titles, sets the discontinued flag and
* rebuilds the title using the last broadcast episode.
*/
// Get all series entities.
$series = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(['type' => 'series']);
// Scan and update series records
foreach ($series as $series_node) {
// Get the series title.
/** @var \Drupal\node\NodeInterface $series_node */
$series_title = $series_node->getTitle();
// If the title doesn't contain our pattern then continue.
$pos1 = strpos($series_title, '(eingestellt');
$pos2 = strpos($series_title, 'eingestellt)');
if ($pos1 === FALSE && $pos2 === FALSE) {
$series_node->set('field_discontinued', FALSE);
$series_node->save();
continue;
}
$pos = strpos($series_title, '(');
// Update the title.
$series_title = substr($series_title, 0, $pos) . ' (eingestellt)';
$series_node->setTitle($series_title);
// Set the discontinued flag.
$series_node->set('field_discontinued', TRUE);
// Save the series record.
$series_node->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment