public function saveBlockType(SuperTableBlockTypeModel $blockType, bool $runValidation = true): bool
    {
        if ($runValidation && !$blockType->validate()) {
            return false;
        }

        $fieldsService = Craft::$app->getFields();

        /** @var Field $parentField */
        $parentField = $fieldsService->getFieldById($blockType->fieldId);
        $isNewBlockType = $blockType->getIsNew();

        $projectConfig = Craft::$app->getProjectConfig();

        $configData = [
            'field' => $parentField->uid,
        ];

        // Now, take care of the field layout for this block type
        // -------------------------------------------------------------
        $fieldLayoutFields = [];
        $sortOrder = 0;

        $configData['fields'] = [];

        foreach ($blockType->getFields() as $field) {
            $configData['fields'][$field->uid] = $fieldsService->createFieldConfig($field);

            $field->sortOrder = ++$sortOrder;
            $fieldLayoutFields[] = $field;
        }

        $fieldLayoutTab = new FieldLayoutTab();
        $fieldLayoutTab->name = 'Content';
        $fieldLayoutTab->sortOrder = 1;
        $fieldLayoutTab->setFields($fieldLayoutFields);

        $fieldLayout = $blockType->getFieldLayout();

        if ($fieldLayout->uid) {
            $layoutUid = $fieldLayout->uid;
        } else {
            $layoutUid = StringHelper::UUID();
            $fieldLayout->uid = $layoutUid;
        }

        $fieldLayout->setTabs([$fieldLayoutTab]);
        $fieldLayout->setFields($fieldLayoutFields);

        $fieldLayoutConfig = $fieldLayout->getConfig();

        $configData['fieldLayouts'] = [
            $layoutUid => $fieldLayoutConfig
        ];

        $configPath = self::CONFIG_BLOCKTYPE_KEY . '.' . $blockType->uid;
        $projectConfig->set($configPath, $configData);

        if ($isNewBlockType) {
            $blockType->id = Db::idByUid('{{%supertableblocktypes}}', $blockType->uid);
        }

        return true;
    }
public function saveBlockType(SuperTableBlockTypeModel $blockType, bool $runValidation = true): bool
    {
        if ($runValidation && !$blockType->validate()) {
            return false;
        }

        $fieldsService = Craft::$app->getFields();

        /** @var Field $parentField */
        $parentField = $fieldsService->getFieldById($blockType->fieldId);
        $isNewBlockType = $blockType->getIsNew();

        $projectConfig = Craft::$app->getProjectConfig();

        $configData = [
            'field' => $parentField->uid,
        ];

        // Now, take care of the field layout for this block type
        // -------------------------------------------------------------
        $fieldLayoutFields = [];
        $sortOrder = 0;

        $configData['fields'] = [];

        foreach ($blockType->getFields() as $field) {
            $configData['fields'][$field->uid] = $fieldsService->createFieldConfig($field);

            $field->sortOrder = ++$sortOrder;
            $fieldLayoutFields[] = $field;
        }

        $fieldLayoutTab = new FieldLayoutTab();
        $fieldLayoutTab->name = 'Content';
        $fieldLayoutTab->sortOrder = 1;
        $fieldLayoutTab->setFields($fieldLayoutFields);

        $fieldLayout = $blockType->getFieldLayout();

        if ($fieldLayout->uid) {
            $layoutUid = $fieldLayout->uid;
        } else {
            $layoutUid = StringHelper::UUID();
            $fieldLayout->uid = $layoutUid;
        }

        $fieldLayout->setTabs([$fieldLayoutTab]);
        $fieldLayout->setFields($fieldLayoutFields);

        $fieldLayoutConfig = $fieldLayout->getConfig();

        $configData['fieldLayouts'] = [
            $layoutUid => $fieldLayoutConfig
        ];

        $configPath = self::CONFIG_BLOCKTYPE_KEY . '.' . $blockType->uid;
        $projectConfig->set($configPath, $configData);

        if ($isNewBlockType) {
            $blockType->id = Db::idByUid('{{%supertableblocktypes}}', $blockType->uid);
        }

        return true;
    }
public function saveBlockType ( SuperTableBlockTypeModel $ blockType , bool $ runValidation = true ) : bool { if ( $ runValidation && ! $ blockType -> validate ( ) ) { return false ; } $ fieldsService = Craft :: $ app -> getFields ( ) ; /** @var Field $parentField */ $ parentField = $ fieldsService -> getFieldById ( $ blockType -> fieldId ) ; $ isNewBlockType = $ blockType -> getIsNew ( ) ; $ projectConfig = Craft :: $ app -> getProjectConfig ( ) ; $ configData = [ 'field' => $ parentField -> uid , ] ; // Now, take care of the field layout for this block type // ------------------------------------------------------------- $ fieldLayoutFields = [ ] ; $ sortOrder = 0 ; $ configData [ 'fields' ] = [ ] ; foreach ( $ blockType -> getFields ( ) as $ field ) { $ configData [ 'fields' ] [ $ field -> uid ] = $ fieldsService -> createFieldConfig ( $ field ) ; $ field -> sortOrder = ++ $ sortOrder ; $ fieldLayoutFields [ ] = $ field ; } $ fieldLayoutTab = new FieldLayoutTab ( ) ; $ fieldLayoutTab -> name = 'Content' ; $ fieldLayoutTab -> sortOrder = 1 ; $ fieldLayoutTab -> setFields ( $ fieldLayoutFields ) ; $ fieldLayout = $ blockType -> getFieldLayout ( ) ; if ( $ fieldLayout -> uid ) { $ layoutUid = $ fieldLayout -> uid ; } else { $ layoutUid = StringHelper :: UUID ( ) ; $ fieldLayout -> uid = $ layoutUid ; } $ fieldLayout -> setTabs ( [ $ fieldLayoutTab ] ) ; $ fieldLayout -> setFields ( $ fieldLayoutFields ) ; $ fieldLayoutConfig = $ fieldLayout -> getConfig ( ) ; $ configData [ 'fieldLayouts' ] = [ $ layoutUid => $ fieldLayoutConfig ] ; $ configPath = self :: CONFIG_BLOCKTYPE_KEY . '.' . $ blockType -> uid ; $ projectConfig -> set ( $ configPath , $ configData ) ; if ( $ isNewBlockType ) { $ blockType -> id = Db :: idByUid ( '{{%supertableblocktypes}}' , $ blockType -> uid ) ; } return true ; }
Saves a block type.

@param SuperTableBlockTypeModel $blockType    The block type to be saved.
@param bool            $validate       Whether the block type should be validated before being saved.
Defaults to `true`.

@return bool
@throws Exception if an error occurs when saving the block type
@throws \Throwable if reasons
Saves a block type .