Naviga Writer
6.3.3
6.3.3
  • Naviga Writer
  • Release notes
    • 6.3.3
    • 6.3.2
    • 6.3.1
    • 6.2.1
    • 6.2.0
    • 6.1.2
    • 6.1.1
    • 6.1.0
    • 6.0.0
    • 5.3.0
  • Introduction
    • Authoring
    • Developing
    • Publishing
    • User stories and reference cases
  • User Guide
    • Writer User Guide
      • The Content Area and Tabs
        • Help menu
        • Language menu
        • Article name
        • Writing teasers
        • Content area
        • Information
        • Search and replace words
        • History Button
        • Save and publish flow
        • Tabs
          • Meta Tab
            • Author
            • Channels
          • Integrations Tab
          • Image and Article search Tab
        • Locked article
      • Text management
      • Writer Keyboard Shortcuts
      • Personal Writer template
      • Channels to control access
      • Images User Guide
      • Common questions when starting using Writer
  • Admin Guide
    • Configuration files
    • Configurations Guide
    • Configuration of Writer templates
    • Publish Flow
      • Default configuration definitions
      • Starting part of the configuration
      • Publish flow config details
      • Configuration of calendarLocale
      • Preconditions for roles in the publish flow
    • Generic properties for Objects
      • Generic properties configuration
    • Image Services
      • Photo uploader
      • Binary Service Light
      • ImEngine
      • Imgix
    • Available Plugins
      • Naviga developed plugins
        • Plugins in earlier versions of Writer
      • Third-party plugins
  • Developer guide
    • Writer plugin development
      • Quickstart
      • Plugin overview
      • Creating an content object plugin
      • Validation and hooks
      • Interacting with external resources
    • Writer Plugin Building Blocks
      • Package
      • Component
      • Node
      • Converter
      • Events
    • Writer plugin style guide
      • CSS Guidelines
      • CSS variables, colors and fonts
      • UI Components
    • Tutorials
      • Popover & text analysis
      • Search & replace
      • Concept interaction
    • Infomaker NewsML
      • Overview
      • Document relations and types
      • Extensions XSD
      • NewsItem
      • ConceptItem
      • PlanningItem
      • Examples
        • NewsItem - Text
        • NewsItem - Picture
        • NewsItem - PDF
        • ConceptItem - Author
        • ConceptItem - Category
        • ConceptItem - Channel
        • ConceptItem - Content Profile
        • ConceptItem - Event
        • ConceptItem - Organisation
        • ConceptItem - Person
        • ConceptItem - Place (point)](point)](point)](point)](point)](point)](point)](point)](point)](point)]
        • ConceptItem - Place (polygon)](polygon)](polygon)](polygon)](polygon)](polygon)](polygon)](polygon)]
        • ConceptItem - Section
        • ConceptItem - Story
        • ConceptItem - Topic
        • PlanningItem
    • Media Enrichment
      • Images
  • API Reference
    • Writer Api
      • Api
      • Article
      • Browser
      • Concept
      • ConceptService
      • Document
      • Events
      • NewsItem
      • Router
      • Ui
      • Upload
      • User
      • settings
    • UI Components
      • UIAvatar
      • UIButton
      • UIByline
      • UICheckbox
      • UIChip
      • UIDatePicker
      • UIDatetimeFieldEditor
      • UIDropdown
      • UIFieldEditor
      • UIIconButton
      • UIInlineImage
      • UIPagination
      • UISelect
      • UITimePicker
      • UIToggle
      • UITooltip
      • UIInputText
      • UITextarea
Powered by GitBook
On this page
  • Overview
  • Before validation
  • Validation
  • Before save hook
  • After save hook
  • Example

Was this helpful?

  1. Developer guide
  2. Writer plugin development

Validation and hooks

Overview

It is possible to add hooks in various stages of the saving process. There are three general hooks in addition to the more special validation. All hooks must return a Promise() that is either resolved or rejected by the hook. Any reject() will cancel the process and show an error message to the user.

Any plugin can provide hooks or validation. The order of multiple hooks in the same stage is not guaranteed so no assumption should be made on the execution order.

All plugins must register their hooks and validations in PluginPackage.js when the plugin registers.

The general stages of the saving process is

  1. Before Validation hooks

  2. Validation

  3. Before save hooks

  4. Saving...

  5. After save hooks

Before validation

In the before validation hooks it is allowed to alter the article programmatically through the API. This is useful if you want to change something automatically before the validation and saving process continues.

Validation

These are not technically hooks. The NPWriter executes one or many provided validation methods. These methods add messages, warnings and errors which are then presented to the user. Any error message will stop the process.

Any changes done programmatically to the article in the validation 
process - or later - will not be saved and potentially lost.

Before save hook

Before save hooks are executed After potential changes of the article and when the valitation passed.

After save hook

When the article has been successfully saved the after save hooks are executed.

Example

A compact example with one hook of each type and a validation in one package file.

import {Validator, hook, api} from 'writer'

class MyValidation extends Validator {
    // Required
    constructor(...args) {
        super(...args)
    }

    validate() {
        const headlines = this.newsItem.querySelectorAll('idf > group element[type="headline"]')
        const headline = headlines[0].childNodes.length === 0 ? '' : headlines[0].firstChild.textContent.trim()

        if (headlines.length === 0) {
            // Informative message
            this.addValidationMessage('The article is missing a headline!')
        }
        else if (headline === '') {
            // Warning
            this.addWarning('The article has an empty headline')
        }

        if (api.newsItem.getAuthors().length === 0) {
            // Error - will cancel saving
            this.addError('The article must have an author')
        }
    }
}

class MyHookBeforeValidate {
    execute(type) {
        return new Promise((resolve, reject) => {
            // Do something, async or not
            resolve()
        })
    }
}

class MyHookBeforeSave {
    execute(type) {
        return new Promise((resolve, reject) => {
            // Do something, async or not
            resolve()
        })
    }
}

class MyHookAfterSave {
    execute(type) {
        return new Promise((resolve, reject) => {
            // Do something, async or not
            resolve()
        })
    }
}

export default {
    name: 'myplugin',
    version: '1.0',
    configure: function (configurator, configObject) {

        configurator.addValidator(MyValidation)

        configurator.addHook(hook.BEFORE_VALIDATE, MyHookBeforeValidate)
        configurator.addHook(hook.BEFORE_SAVE, MyHookBeforeSave)
        configurator.addHook(hook.AFTER_SAVE, MyHookAfterSave)
    }
}
PreviousCreating an content object pluginNextInteracting with external resources

Last updated 5 years ago

Was this helpful?