Skip to content

Instantly share code, notes, and snippets.

@acbramley
Last active May 6, 2020 01:12
Show Gist options
  • Save acbramley/8934bff1263268eab15cb6af7c32cdc7 to your computer and use it in GitHub Desktop.
Save acbramley/8934bff1263268eab15cb6af7c32cdc7 to your computer and use it in GitHub Desktop.
CSV Report of Nodes from a given time with published on dates
<?php
use League\Csv\Writer;
$entityStorage = \Drupal::entityTypeManager()->getStorage('node');
$entityTypeStorage = \Drupal::entityTypeManager()->getStorage('node_type');
$db = \Drupal::database();
$nids = $entityStorage->getQuery()
// Everything since Jan 1st 2020
->condition('created', '1577836800', '>=')
->sort('created', 'asc')
// Only published content.
->condition('status', 1)
->execute();
$date_formatter =\Drupal::service('date.formatter');
$csv = Writer::createFromFileObject(new SplTempFileObject());
$csv->insertOne(['Title', 'Authored on', 'Published on', 'Content type', 'URL']);
foreach ($entityStorage->loadMultiple($nids) as $node) {
// Find the first published revision.
$query = \Drupal::database()->select('content_moderation_state_field_revision', 'c')
->condition('content_entity_type_id', 'node')
->condition('content_entity_id', $node->id())
->condition('moderation_state', 'published');
$query->addExpression('MIN(content_entity_revision_id)', 'vid');
$vid = reset($query->execute()->fetchCol());
$revision = $entityStorage->loadRevision($vid);
$csv->insertOne([
$node->label(),
$date_formatter->format($node->getCreatedTime(), 'custom', 'd F Y'),
$date_formatter->format($revision->getRevisionCreationTime(), 'custom', 'd F Y'),
$entityTypeStorage->load($node->bundle())->label(),
'https://www.example.com' . $node->get('path')->alias,
]);
}
echo (string) $csv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment