SU-SWS/stanford_migrate

View on GitHub
src/Plugin/migrate/process/NameField.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace Drupal\stanford_migrate\Plugin\migrate\process;

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

/**
 * Parse a string from common name formats.
 *
 * Examples:
 *
 * @code
 * process:
 *   plugin: name_field
 *   source: some_text_field
 * @endcode
 *
 * @MigrateProcessPlugin(
 *   id = "name_field"
 * )
 */
class NameField extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $name = FullNameParser::parse($value);
    $name = [
      'title' => $name['salutation'],
      'given' => !empty($name['fname']) ? $name['fname'] : $name['initials'],
      'family' => trim($name['lname'] . ' ' . $name['suffix']),
    ];
    if (empty($name['family']) && $name['given']) {
      $name['family'] = $name['given'];
      $name['given'] = '';
    }
    return $name;
  }

}