Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Migrate data for media field in repeatable field #33111

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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