Craft 3,是否可以通过迁移来填充Global字段?

时间:2020-03-03 08:12:32

标签: craftcms

我想知道是否以及如何在Craft3中填充Global字段。

就像我建立新项目一样,正在运行的迁移会创建一些全局变量和字段。 例如,这已经为我提供了每个新项目的全局变量中的cookie策略字段和cookie表。现在,我想停止对所有项目始终存在的cookie重复工作,并通过迁移运行它们以预先填充它们。

关于这是否可行以及如何进行的任何想法?

现在我正朝这个方向前进:

//get the matrix field we want to populate
$fieldCookies = Craft::$app->getFields()->getFieldByHandle("fieldCookies");

// get the existing matrixField Value, keep in mind, this is a Query and not an array
$existingMatrixQuery = $element->getFieldValue('fieldCookies');

// serialize the data in order to get an array
$serializedMatrix = $field->serializeValue($existingMatrixQuery, $element);

// append the new blocks to the existing array
$serializedMatrix['item1'] = [
    'type' => 'blockTypeHandle',
    'fields' => [
    // the block's custom field values
        'itemTitle' => "Noodzakelijk", //Plaintext
        'itemHandle' => "necessary", //Plaintext
        'itemText' => "Cookies die noodzakelijk zijn voor een correcte werking van de website.", //Plaintext
        'itemRequired' => 1, //Lightswitch
        'itemCookies' => [
                    //This is item of the matrix is a table
                    //...
                ]
            ]
        ];

        $element->setFieldValue('matrixField', $serializedMatrix);

        // Save the entry
        Craft::$app->elements->saveElement($element); 

我想知道这是否可行,如果迁移是否知道它正在填充表,我是否只是用嵌套数组填充此矩阵中的表部分。

稍后,我将对不是矩阵而是超表的其他字段执行相同的操作。欢迎任何提示。

0 个答案:

没有答案
相关问题