3

I'm building a widget where I want to list all unpublished drafts (made by users without access to publish live changes), but I can't figure out how to get them.

3

Depending on what you want to do with them you can request them using craft.entries but you'll probably need to change the status to pending.

If this doesn't work you'll need to fetch them through a custom controller/service that goes in and fetches the entry model and returns a list that fit you criteria.

Hope this helps.

2

Drafts store rows in the craft_entrydrafts table, relating to an entry, providing some context (author, date, notes), and serializing all the pending content into a data column. Craft normally gets drafts only as they relate to a specific entry, so you'll need to provide your own method of retrieving them all.

The EntryRevisionsService class has a method called getDraftsByEntryId(), which can be modified slightly to return all those draft records rather than ones limited specifically to a given entry:

public function getEntryDrafts($localeId = null)
{
    if (!$localeId)
    {
        $localeId = craft()->i18n->getPrimarySiteLocale();
    }

    $drafts = array();

    $results = craft()->db->createCommand()
        ->select('*')
        ->from('entrydrafts')
        ->where(array('locale = :locale'), array(':locale' => $localeId))
        ->order('dateUpdated desc')
        ->queryAll();

    foreach ($results as $result)
    {
        $result['data'] = JsonHelper::decode($result['data']);

        // Don't initialize the content
        unset($result['data']['fields']);

        $drafts[] = EntryDraftModel::populateModel($result);
    }

    return $drafts;
}

Each result will be an EntryDraftModel, so use the draftId parameter if you'd like to link directly to that draft as you'd normally see it in the control panel. In a Twig template, for example, that'd look like {{ draft.cpEditUrl ~ "/drafts/" ~ draft.draftId }}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.