Skip to content

Commit 0f1a1f9

Browse files
jofitzBerdir
authored andcommitted
Issue #2715807 by Jo Fitzgerald: Migrate D7 redirects to D8
1 parent a6167de commit 0f1a1f9

File tree

8 files changed

+561
-1
lines changed

8 files changed

+561
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
id: d7_path_redirect
2+
label: Path Redirect
3+
migration_tags:
4+
- Drupal 7
5+
source:
6+
plugin: d7_path_redirect
7+
process:
8+
rid: rid
9+
uid: uid
10+
redirect_source/path: source
11+
redirect_source/query:
12+
plugin: d7_redirect_source_query
13+
source: source_options
14+
redirect_redirect/uri:
15+
plugin: d7_path_redirect
16+
source:
17+
- redirect
18+
- redirect_options
19+
language:
20+
plugin: default_value
21+
source: language
22+
default_value: und
23+
status_code: status_code
24+
destination:
25+
plugin: entity:redirect

src/Plugin/migrate/process/PathRedirect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
4444
return $uri;
4545
}
4646

47-
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\redirect\Plugin\migrate\process\d7\PathRedirect.
6+
*/
7+
8+
namespace Drupal\redirect\Plugin\migrate\process\d7;
9+
10+
use Drupal\migrate\MigrateExecutableInterface;
11+
use Drupal\migrate\ProcessPluginBase;
12+
use Drupal\migrate\Row;
13+
14+
/**
15+
* @MigrateProcessPlugin(
16+
* id = "d7_path_redirect"
17+
* )
18+
*/
19+
class PathRedirect extends ProcessPluginBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* Transform the field as required for an iFrame field.
25+
*/
26+
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
27+
28+
// Check if the url begins with http.
29+
if (preg_match('#^http#', $value[0])) {
30+
// Use it as is.
31+
$uri = $value[0];
32+
}
33+
else {
34+
// Make the link internal.
35+
$uri = 'internal:/' . $value[0];
36+
}
37+
38+
// Check if there are options.
39+
if (!empty($value[1])) {
40+
// Check if there is a query.
41+
$options = unserialize($value[1]);
42+
if (!empty($options['query'])) {
43+
// Add it to the end of the url.
44+
$uri .= '?' . http_build_query($options['query']);
45+
}
46+
}
47+
48+
return $uri;
49+
}
50+
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\redirect\Plugin\migrate\process\d7\RedirectSourceQuery.
6+
*/
7+
8+
namespace Drupal\redirect\Plugin\migrate\process\d7;
9+
10+
use Drupal\migrate\MigrateExecutableInterface;
11+
use Drupal\migrate\ProcessPluginBase;
12+
use Drupal\migrate\Row;
13+
14+
/**
15+
* @MigrateProcessPlugin(
16+
* id = "d7_redirect_source_query"
17+
* )
18+
*/
19+
class RedirectSourceQuery extends ProcessPluginBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* Transform the field as required for an iFrame field.
25+
*/
26+
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
27+
28+
// Check if there are options.
29+
if (!empty($value)) {
30+
// Check if there is a query.
31+
$options = unserialize($value);
32+
if (!empty($options['query'])) {
33+
// Add it to the end of the url.
34+
return serialize($options['query']);
35+
}
36+
}
37+
38+
return NULL;
39+
}
40+
41+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\redirect\Plugin\migrate\source\d7\PathRedirect.
6+
*/
7+
8+
namespace Drupal\redirect\Plugin\migrate\source\d7;
9+
10+
use Drupal\migrate\Row;
11+
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
12+
13+
/**
14+
* Drupal 7 path redirect source from database.
15+
*
16+
* @MigrateSource(
17+
* id = "d7_path_redirect",
18+
* source_provider = "redirect"
19+
* )
20+
*/
21+
class PathRedirect extends DrupalSqlBase {
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function query() {
27+
// Select path redirects.
28+
$query = $this->select('redirect', 'p')->fields('p');
29+
30+
return $query;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function prepareRow(Row $row) {
37+
static $default_status_code;
38+
if (!isset($default_status_code)) {
39+
$default_status_code = unserialize($this->getDatabase()
40+
->select('variable', 'v')
41+
->fields('v', ['value'])
42+
->condition('name', 'redirect_default_status_code')
43+
->execute()
44+
->fetchField());
45+
}
46+
$current_status_code = $row->getSourceProperty('status_code');
47+
$status_code = $current_status_code != 0 ? $current_status_code : $default_status_code;
48+
$row->setSourceProperty('status_code', $status_code);
49+
return parent::prepareRow($row);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function fields() {
56+
$fields = [
57+
'rid' => $this->t('Redirect ID'),
58+
'hash' => $this->t('Hash'),
59+
'type' => $this->t('Type'),
60+
'uid' => $this->t('UID'),
61+
'source' => $this->t('Source'),
62+
'source_options' => $this->t('Source Options'),
63+
'redirect' => $this->t('Redirect'),
64+
'redirect_options' => $this->t('Redirect Options'),
65+
'language' => $this->t('Language'),
66+
'status_code' => $this->t('Status Code'),
67+
'count' => $this->t('Count'),
68+
'access' => $this->t('Access'),
69+
];
70+
return $fields;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getIds() {
77+
$ids['rid']['type'] = 'integer';
78+
return $ids;
79+
}
80+
81+
}

0 commit comments

Comments
 (0)