Skip to content

Commit

Permalink
Add a generic Boolean migrate process plugin that uses filter_var($va…
Browse files Browse the repository at this point in the history
…lue, FILTER_VALIDATE_BOOLEAN).
  • Loading branch information
mstenta committed Mar 27, 2024
1 parent a4e13c9 commit 6cf3c1b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions modules/core/migrate/src/Plugin/migrate/process/Boolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Drupal\farm_migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
* This plugin converts values to a boolean.
*
* @codingStandardsIgnoreStart
*
* Example usage:
* @code
* destination:
* plugin: 'entity:log'
* process:
* is_movement:
* plugin: boolean
* source: is_movement
* @endcode
* @codingStandardsIgnoreEnd
*
* @MigrateProcessPlugin(
* id = "boolean"
* )
*/
class Boolean extends ProcessPluginBase {

/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

// If the trimmed value is an empty string, return NULL so that the field's
// default value will be used.
if (trim($value) === '') {
return NULL;
}

// Use PHP's filter_var() with FILTER_VALIDATE_BOOLEAN constant.
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}

}

0 comments on commit 6cf3c1b

Please sign in to comment.