View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class StateToAbbrev extends TamperBase {
protected static function getStateList() {
$states = [
'alabama' => 'AL',
'alaska' => 'AK',
'arizona' => 'AZ',
'american samoa' => 'AS',
'arkansas' => 'AR',
'california' => 'CA',
'colorado' => 'CO',
'connecticut' => 'CT',
'delaware' => 'DE',
'district of columbia' => 'DC',
'federated states of micronesia' => 'FM',
'florida' => 'FL',
'georgia' => 'GA',
'guam' => 'GU',
'hawaii' => 'HI',
'idaho' => 'ID',
'illinois' => 'IL',
'indiana' => 'IN',
'iowa' => 'IA',
'kansas' => 'KS',
'kentucky' => 'KY',
'louisiana' => 'LA',
'maine' => 'ME',
'maryland' => 'MD',
'massachusetts' => 'MA',
'marshall islands' => 'MH',
'michigan' => 'MI',
'minnesota' => 'MN',
'mississippi' => 'MS',
'missouri' => 'MO',
'montana' => 'MT',
'nebraska' => 'NE',
'nevada' => 'NV',
'new hampshire' => 'NH',
'new jersey' => 'NJ',
'new mexico' => 'NM',
'new york' => 'NY',
'north carolina' => 'NC',
'north dakota' => 'ND',
'northern mariana islands' => 'MP',
'ohio' => 'OH',
'oklahoma' => 'OK',
'oregon' => 'OR',
'pennsylvania' => 'PA',
'palau' => 'PW',
'puerto rico' => 'PR',
'rhode island' => 'RI',
'south carolina' => 'SC',
'south dakota' => 'SD',
'tennessee' => 'TN',
'texas' => 'TX',
'utah' => 'UT',
'vermont' => 'VT',
'virginia' => 'VA',
'virgin islands' => 'VI',
'washington' => 'WA',
'west virginia' => 'WV',
'wisconsin' => 'WI',
'wyoming' => 'WY',
];
return $states;
}
public function tamper($data, TamperableItemInterface $item = NULL) {
$states = self::getStateList();
if (in_array($data, $states)) {
return;
}
$state = mb_strtolower(trim($data));
return isset($states[$state]) ? $states[$state] : '';
}
}