2

I've got the beginnings of a simple plugin to track views of an article page, just an entry with custom fields including 'dataViews' used below.

Within Twig:

{% do craft.demoPlugin.incrementView(entry) %}

Variable above set with:

<?php
namespace Craft;

class DemoPluginVariable
{
    /**
     * Increment count
     *
     * @param EntryModel $entry
     */
    public function incrementView(EntryModel $entry)
    {
        craft()->demoPlugin->incrementView($entry);
    }
}

Here's the service code which picks up the

<?php
namespace Craft;


class DemoPluginService extends BaseApplicationComponent
{
    /**
     * Increment view count
     *
     * @param EntryModel $entry
     */
    public function incrementView(EntryModel $entry)
    {
        $entry->setContentFromPost([
            'dataViews' => 1234,
        ]);


        $success = craft()->entries->saveEntry($entry);
        Craft::dd($success);
    }
}

The above fails when passed an entry, with $success reporting false. Logging the error this is due to other fields on the entry being required and not passed through.

Could anyone point me in the direction for updating just one field within a record?

3

Your Answer

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

Browse other questions tagged or ask your own question.