Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / src / Plugin / migrate / process / ConvertTokens.php
1 <?php
2
3 namespace Drupal\user\Plugin\migrate\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\ProcessPluginBase;
8 use Drupal\migrate\Row;
9
10 /**
11  * Plugin to replace !tokens with [tokens].
12  *
13  * @MigrateProcessPlugin(
14  *   id = "convert_tokens",
15  *   handle_multiples = TRUE
16  * )
17  */
18 class ConvertTokens extends ProcessPluginBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
24     $tokens = [
25       '!site' => '[site:name]',
26       '!username' => '[user:name]',
27       '!mailto' => '[user:mail]',
28       '!login_uri' => '[site:login-url]',
29       '!uri_brief' => '[site:url-brief]',
30       '!edit_uri' => '[user:edit-url]',
31       '!login_url' => '[user:one-time-login-url]',
32       '!uri' => '[site:url]',
33       '!date' => '[date:medium]',
34       '!password' => '',
35     ];
36
37     // Given that our source is a database column that could hold a NULL
38     // value, sometimes that filters down to here. str_replace() cannot
39     // handle NULLs as the subject, so we reset to an empty string.
40     if (is_null($value)) {
41       $value = '';
42     }
43
44     if (is_string($value)) {
45       return str_replace(array_keys($tokens), $tokens, $value);
46     }
47     else {
48       throw new MigrateException('Value must be a string.');
49     }
50   }
51
52 }