Created
November 19, 2021 12:09
-
-
Save rosemaryreilman/97ff945ebe9af5aa8582b78b4001c181 to your computer and use it in GitHub Desktop.
Moderation State Process Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\my_module\Plugin\migrate\process; | |
| use \Drupal\Core\Database\Database; | |
| use Drupal\migrate\MigrateExecutableInterface; | |
| use Drupal\migrate\ProcessPluginBase; | |
| use Drupal\migrate\Row; | |
| /** | |
| * Custom process plugin to set a proper moderation state based on what's current on the site | |
| * | |
| * @MigrateProcessPlugin( | |
| * id = "moderation_state" | |
| * ) | |
| * | |
| * Example: | |
| * moderation_state: | |
| plugin: moderation_state | |
| source: status | |
| */ | |
| class ModerationState extends ProcessPluginBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
| $publishedStatus = $row->getSourceProperty('status'); | |
| $nid = $row->getSourceProperty('nid'); | |
| $vid = $row->getSourceProperty('vid'); | |
| $state = $this->query($nid, $vid); | |
| // https://www.drupal.org/project/drupal/issues/3164497 | |
| // If there's no state available use the publish status to set moderation state. | |
| if ($publishedStatus === '1' && !$state) { | |
| $state = 'published'; | |
| } elseif ($publishedStatus === '0' && !$state) { | |
| $state = 'draft'; | |
| } | |
| return $state; | |
| } | |
| public function query($nid, $vid) { | |
| $return = null; | |
| Database::setActiveConnection('migrate'); | |
| $migrateDB = \Drupal\Core\Database\Database::getConnection(); | |
| $query = $migrateDB->select('workbench_moderation_node_history', 'w') | |
| ->fields('w', ['state', 'published', 'is_current']) | |
| ->condition('w.nid', $nid) | |
| ->condition('w.vid', $vid) | |
| ->orderBy('w.stamp', 'DESC') | |
| ->range(0, 1); | |
| $data = $query->execute()->fetchAssoc(); | |
| Database::setActiveConnection(); | |
| if (!empty($data)) { | |
| $return = $data['state']; | |
| // In case the final state is published but the revision is not the | |
| // published one, set the state to draft. | |
| if ($data['state'] == 'published' && $data['published'] == '0') { | |
| $return = 'draft'; | |
| } | |
| } | |
| return $return; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment