Skip to content

Commit 9ea69b0

Browse files
authored
Update 03_customlink.md
1 parent 950fe76 commit 9ea69b0

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

docs/03_customlink.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,97 @@ if (count($link) > 1) {
200200
}
201201
}
202202
```
203+
204+
### Custom-Link-Werte aus MBlock zum Repeater (MForm >=8) konvertieren
205+
206+
Das Datenformat der CustomLinks im Repeater unterscheidet sich von MBlock. Der nachfolgende Konverter hilft bei der Umstellung. Kopiere den Code direkt in das betroffene Modul und passe ihn an, oder führe den Code separat aus.
207+
208+
> **Hinweis: Nicht vergessen, ein Backup zu machen, um im Fall eines Fehlers eine Wiederherstellung zu ermöglichen.** Insb. ein Backup der Tabelle `rex_article_slice` und ggf. `rex_article`, damit diese bei einer Wiederherstellung zusammenpassen.
209+
210+
Es muss das Value angepasst werden in dem gesucht werden soll, die Bezeichnung des Feldes und die Modul-ID.
211+
212+
```php
213+
<?php
214+
// Define the parameters
215+
$column = 'value1'; // column: value1, ..., value10
216+
$node = 'customlink'; // node
217+
$moduleId = 48; // module_id
218+
219+
// Fetch all records from the rex_article_slice table where module_id is the specified value
220+
$sql = rex_sql::factory();
221+
$sql->setQuery("SELECT id, $column FROM rex_article_slice WHERE module_id = ?", [$moduleId]);
222+
223+
foreach ($sql as $row) {
224+
$id = $row->getValue('id');
225+
$jsonData = $row->getValue($column);
226+
227+
// Decode JSON data
228+
$data = json_decode($jsonData, true);
229+
230+
// Check if decoding was successful and if the data is an array
231+
if (json_last_error() === JSON_ERROR_NONE && is_array($data)) {
232+
$modified = false;
233+
234+
// Traverse the JSON array and modify customlink nodes
235+
foreach ($data as &$item) {
236+
if (isset($item[$node])) {
237+
$value = $item[$node];
238+
239+
// Check if the value is already in the desired format
240+
$isNewFormat = is_array($value) && isset($value['name']) && isset($value['id']);
241+
242+
if (!$isNewFormat) {
243+
if (is_numeric($value)) {
244+
245+
$articleId = (int)$value;
246+
247+
// Artikelobjekt laden
248+
$article = rex_article::get($articleId);
249+
$articleName = "article not found: redaxo://$value";
250+
if ($article) {
251+
// Artikelname abrufen
252+
$articleName = rex_escape($article->getName());
253+
}
254+
255+
$item[$node] = [
256+
'name' => "$articleName",
257+
'id' => "redaxo://$value"
258+
];
259+
} else {
260+
$item[$node] = [
261+
'name' => "$value",
262+
'id' => "$value"
263+
];
264+
}
265+
$modified = true;
266+
}
267+
}
268+
}
269+
270+
// If data was modified, update the database
271+
if ($modified) {
272+
$updatedJsonData = json_encode($data);
273+
$updateSql = rex_sql::factory();
274+
$updateSql->setTable('rex_article_slice');
275+
$updateSql->setWhere(['id' => $id]);
276+
$updateSql->setValue($column, $updatedJsonData);
277+
$updateSql->update();
278+
echo "Updated record ID $id<br>";
279+
}
280+
} else {
281+
// Log or handle invalid JSON data if necessary
282+
echo "Invalid JSON data in record ID $id<br>";
283+
}
284+
}
285+
286+
echo "Conversion completed.<br>";
287+
?>
288+
289+
<?php
290+
291+
echo '<pre>';
292+
print_r(rex_var::toArray("REX_VALUE[1]"));
293+
echo '</pre>';
294+
295+
?>
296+
```

0 commit comments

Comments
 (0)