Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevin T Reynolds 15 posts 158 karma points
    Jul 07, 2020 @ 19:34
    Kevin T Reynolds
    0

    Default property editor value not published when creating content using Content Service

    I have a checkbox property editor on a document type that is checked by default.

    I'm using the ContentService to create and publish the node.

    var node = _contentService.Create(nodeName, parent.Id, contentTypeAlias);
    _contentService.SaveAndPublish(node);
    

    It seems to work as expected, if I go to the node in the content tree everything looks exactly as I would expect. The checkboxes are checked there is no green + icon indicating a saved draft.

    enter image description here

    But my razor view for my sitemap is showing those values as false.

    enter image description here

    And if I rollback, there is only one version and the diff shows that the current true values would be removed if I rollback to it

    enter image description here

    If I click "Save and Publish" the values all update to the expected state.

    Since these folders are being generated I'd like to make sure that they are hidden by default instead of having to navigate to them and click save and publish. I could explicitly set the values after I create the content and publish it but is there something I can/have to do to make sure the default values are set when content is created?

  • Kevin T Reynolds 15 posts 158 karma points
    Jul 10, 2020 @ 19:12
    Kevin T Reynolds
    0

    For now I'm retrieving the configuration using the DataTypeService and setting the default manually

    foreach (var prop in node.Properties)
    {
        var dataType = _dataTypeService.GetDataType(prop.PropertyType.DataTypeId);
    
        if (dataType.EditorAlias == Constants.PropertyEditors.Aliases.Boolean)
        {
            TrueFalseConfiguration config = (TrueFalseConfiguration)dataType.Configuration;
            prop.SetValue(config.Default);
        }
    }
    
  • Amir Khan 1287 posts 2744 karma points
    Jul 10, 2020 @ 21:08
    Amir Khan
    0

    I can't remember exactly and not in a place where I can look at past projects, but I remember something funky about how the cache gets updated with the SaveAndPublish event.

  • Amir Khan 1287 posts 2744 karma points
    Jul 10, 2020 @ 21:10
  • Kevin T Reynolds 15 posts 158 karma points
    Jul 10, 2020 @ 21:16
    Kevin T Reynolds
    0

    I don't think it's cache related it seems to not be saving any value in the database. The angular controller has some logic to apply the default if there is no value on the model. So the frontend is showing the default value even though the db has nothing stored.

    function booleanEditorController($scope, angularHelper) {
        function setupViewModel() {
            $scope.renderModel = { value: false };
            if ($scope.model.config && $scope.model.config.default && Object.toBoolean($scope.model.config.default) && $scope.model && !$scope.model.value) {
                $scope.renderModel.value = true;
            }
            if ($scope.model && $scope.model.value && Object.toBoolean($scope.model.value)) {
                $scope.renderModel.value = true;
            }
        }
    
        ...
    
    }
    

    I think it's related to this https://github.com/umbraco/Umbraco-CMS/issues/6252#issuecomment-526917401

  • Amir Khan 1287 posts 2744 karma points
    Jul 10, 2020 @ 22:06
    Amir Khan
    0

    Ah interesting.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jul 11, 2020 @ 06:51
    Marc Goodson
    0

    Hi Kevin

    The 'default' value for the Checkbox property editor, is only a 'starting position' for that editor, when it is opened in the backoffice on a new document type.

    So if an editor creates a new page in the backoffice, and it has a checkbox with a 'default value' set to be true, then, it will appear to the editor as being already checked.

    There is an event that fires in the backoffice where these kind of 'starting positions' for editors can be set:

    https://our.umbraco.com/documentation/reference/events/EditorModel-Events/

    But if you are creating content programatically, this is outside the UI of the backoffice and these events do not fire, and so you need to take responsibility for setting any default values yourself.

    The grey area then is if you are writing some code to create a content item, and it has a checkbox, and you want this default state to always reflect what has been selected in the backoffice as the default value for the checkbox, and you have no control over whether this value changes?

    It all depends on the context of what you are trying to do! - but if I wanted the nodes that were being created automatically to always have those checkboxes checked, I would put that logic in the creation of them.

            var node = _contentService.Create(nodeName, parent.Id, contentTypeAlias);
    //can't remember if this is 'true' or '1' that you need to set!
        node.SetValue("hideFromSitemap",true)
     node.SetValue("hideFromNavigation",true)
            _contentService.SaveAndPublish(node);
    

    instead of trying to use the configuration values of the property in the CMS - I can see niche circumstance where you might 'have to', (and it would be your code below) , but it would be an unnecessary overhead if pretty much the values were ALWAYS true here and never changed!

    Anyway this is why you are seeing what you are seeing, you are creating the object in code, no value is set for hideFromSiteMap or hideFromNavigation - you open the node in Umbraco, and because no value is set in the database, the 'default' values appears for the checkboxes, saving and publishing then updates the database with these 'default' values, but if you don't save and publish, they are not in the database, and not in the published cache - hence you can't use them in razor!

    I think the problem is calling them 'default' values in the configuration, as they don't behave how you would think 'default' values to behave, but they are a handy configuration for the starting position of a property editor...

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft