Geta / InstantTemplates

Apache License 2.0
5 stars 5 forks source link

Instant Templates Created and Published dates #63

Open soria2020 opened 4 years ago

soria2020 commented 4 years ago

Hi,

I have a question regarding “Created” and “Published” dates when creating a page from a template. I have tested this in an alloy example site and I will try my best to describe it.

This is my test template: image

When I create a page based on my template: image image

The question is: right now the “Published date” & “Created date” is the same dates as the template page, maybe this is how it is supposed to be ?

Is it possible for me to get a new “Published date” & “Created date” for this new created page based on the time when it was first created and published ? image

Thank you in advance! /Soria

patkleef commented 4 years ago

Hi @soria2020,

We are using IContentRepository.Copy to create a new content item based on a template. Probably it uses the dates from the template: https://github.com/Geta/InstantTemplates/blob/develop/src/InstantTemplatesStore.cs#L110

I'll mark this issue as an enhancement, perhaps we could make an interface that is called when the Copy method is executed so that you can add your own logic before the content item is saved.

For now, you could listen to IContentEvents.OnCreated (I believe it was) and set the dates/

soria2020 commented 4 years ago

Hi @patkleef
Thank you for your answer, I have tried implementing changing the created and published date for the page that is created from the instance template with the following code and still get the same date as the template date , I dont know what I am doing wrong...

private void ContentEvents_CreatedContent(object sender, ContentEventArgs e) { var page = (e.Content as PageData) != null ? (e.Content as PageData).CreateWritableClone() : null; var contentRepository = ServiceLocator.Current.GetInstance();

        if (page != null)
        {

            PageData writableArticlePage = page.CreateWritableClone();
            writableArticlePage.Created = DateTime.Now;
            writableArticlePage.StartPublish = null;
            contentRepository.Save(writableArticlePage, SaveAction.ForceCurrentVersion);
        }

    }
patkleef commented 4 years ago

Can you try it like this:

private void CreatedContent(object sender, ContentEventArgs e)
{
    var copyContentEventArgs = e as CopyContentEventArgs;

    if (copyContentEventArgs != null)
    {
        var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

        if (contentRepository.TryGet<ArticlePage>(copyContentEventArgs.ContentLink, out var articlePage))
        {
            var writableArticlePage = articlePage.CreateWritableClone();
            writableArticlePage.SortIndex -= 5;
            contentRepository.Save(writableArticlePage, SaveAction.ForceCurrentVersion);
        }
    }
}
soria2020 commented 4 years ago

Hi again, @patkleef I have created this solution and it works but the problem is it made the site slow, do you have any idea why ?

private void ContentEvents_CreatedContent(object sender, ContentEventArgs e) { if (e.ContentLink != null) { var copyContentEventArgs = new CopyContentEventArgs(e.ContentLink, null, null); if (copyContentEventArgs != null) { var contentRepository = ServiceLocator.Current.GetInstance();

                if (contentRepository.TryGet<SitePageData>(copyContentEventArgs.ContentLink, out var pageData))
                {
                    var writablePage = pageData.CreateWritableClone();
                    writablePage.Created = DateTime.Now;
                    writablePage.StartPublish = null;
                    contentRepository.Save(writablePage, SaveAction.ForceCurrentVersion);
                }
            }
        }

    }
patkleef commented 3 years ago

@soria2020 sorry for not getting back to you. Do you still have this issue? Do you have any other event listeners that are fired when content is saved? Perhaps that could slow down the site.