Skip to content

Commit

Permalink
[4.0] Migrate data for media field in repeatable field (#33111)
Browse files Browse the repository at this point in the history
  • Loading branch information
joomdonation committed Apr 24, 2021
1 parent df9def4 commit a5195a0
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions administrator/components/com_admin/script.php
Expand Up @@ -296,6 +296,12 @@ protected function uninstallRepeatableFieldsPlugin()
*/
$mapping = [];

/**
* Store name of media fields which we need to convert data from old format (string) to new
* format (json) during the migration
*/
$mediaFields = [];

// If this repeatable fields actually had child-fields (normally this is always the case)
if (isset($oldFieldparams->fields) && is_object($oldFieldparams->fields))
{
Expand Down Expand Up @@ -342,6 +348,11 @@ protected function uninstallRepeatableFieldsPlugin()
$data['type'] = 'text';
}

if ($data['type'] == 'media')
{
$mediaFields[] = $oldField->fieldname;
}

// Reset the state because else \Joomla\CMS\MVC\Model\AdminModel will take an already
// existing value (e.g. from previous save) and do an UPDATE instead of INSERT.
$fieldModel->setState('field.id', 0);
Expand Down Expand Up @@ -402,7 +413,7 @@ protected function uninstallRepeatableFieldsPlugin()
$db->setQuery(
$db->getQuery(true)
->update('#__fields')
->set($db->quoteName('type') . ' = ' . $db->quote('subfields'))
->set($db->quoteName('type') . ' = ' . $db->quote('subform'))
->set($db->quoteName('fieldparams') . ' = ' . $db->quote(json_encode($newFieldparams)))
->where($db->quoteName('id') . ' = ' . $db->quote($row->id))
)->execute();
Expand Down Expand Up @@ -440,13 +451,21 @@ protected function uninstallRepeatableFieldsPlugin()
continue;
}

$rowIndex = 0;

foreach ($fieldValue as $rowKey => $rowValue)
{
$rowKey = str_replace('repeatable', 'row', $rowKey);
$rowKey = 'row' . ($rowIndex++);
$newFieldValue[$rowKey] = [];

foreach ($rowValue as $subFieldName => $subFieldValue)
{
// This is a media field, so we need to convert data to new format required in Joomla! 4
if (in_array($subFieldName, $mediaFields))
{
$subFieldValue = ['imagefile' => $subFieldValue, 'alt_text' => ''];
}

if (isset($mapping[$subFieldName]))
{
$newFieldValue[$rowKey][$mapping[$subFieldName]] = $subFieldValue;
Expand Down

0 comments on commit a5195a0

Please sign in to comment.