-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Craft::$app->elementIndexes->saveSettings sources node not working as expected #3484
Comments
Can you post the full PHP code you're using in steps 5 and 6? |
Thank you for your reply. Quick explanation. I loop the $i18nCoreSection = Craft::$app->sections->getSectionByHandle(self::SECTION_I18NCORE_HANDLE);
$i18nCoreSectionKey = 'section:' . $i18nCoreSection->id;
$i18nCoreSectionSourceData = null;
// The new section ID
$sectionKey = 'section:' . $this->_newSectionModel->id;
// Get sources datas
$newSources = $elementsIndex['sources'];
// Find i18n_core_module info
foreach ($newSources as $key => $source) {
if ($key === $i18nCoreSectionKey) {
$i18nCoreSectionSourceData = $source;
break;
}
}
// Append new section source data to all section data
$elementsIndex['sources'][$sectionKey] = $i18nCoreSectionSourceData;
// Save the element Index
return Craft::$app->getElementIndexes()->saveSettings('craft\elements\Entry', $elementsIndex); |
Looks right to me. Try adding this right before that last line: Craft::dd($elementsIndex); To verify that the data all looks correct. There should be a reference to the section in the |
Here is the dump. Sorry for dirty dump, i'm dumping with the ./craft command :
|
OK, and does the output of |
// The dump is the same as the previous message // After saveSettings, the sources node is now empty To debug the problem, i look into /vendor/craftcms/cms/src/services/ElementIndexes.php. The problem seem to been related with this method : line: 117 // Prune out any settings for sources that don't exist Here is the dump of $baseSources :
Like I said, the problem is that $baseSources not contains the existing sections + the new one. So it it unset by the loop just after that. I don't know if the problem is cause by running the migration via the craft console vs normal http request ? The only way to make it work is to comment this method. But it's not a solution because I have to change the craft core and this will be overriden by composer update. |
OK I see what’s happening here. Fixed for the next release. |
Description
I'm working on migrations for create section and fields related to that section. What I want to do is to automatically add that section under a tab in CP panel and show related fields in the listing automatically. (vs showing default fields -> title, date, etc.)
I can add the section under tab without trouble. The problem is the
[sources]
nodes that is not saved correctly with thesaveSettings
method. By looking at the source code of the service in the method, this linecause the problem. By removing this line and the loop after, all works correctly.
I'm not sure to understand the reason why
$baseSources
are use. It sure that$baseSources
will never contain the key section I'm trying to add. Maybe i'm not understand something with the settings...Steps to reproduce
$elementsIndex = Craft::$app->elementIndexes->getSettings('craft\elements\Entry');
$elementsIndex['sourceOrder'];
Craft::$app->elementIndexes->saveSettings('craft\elements\Entry', $elementsIndex);
$newSources = $elementsIndex['sources'];
$newSources
array :Craft::$app->elementIndexes->saveSettings('craft\elements\Entry', $elementsIndex);
Craft::$app->elementIndexes->getSettings('craft\elements\Entry');
['sources']
is now empty.Additional info
The text was updated successfully, but these errors were encountered: