Override a CKEditor notification message

There are some applications that needs changing the default notification message of a CKEditor plugin. Like this case, I have application that needs to check the size of a file being uploaded against the maximum file size limit allowed on a Drupal site and the upload should abort if it exceeds. Using this upload.abort(); method will display "Upload aborted by the user." notification message which obviously not applicable to the situation. The following code snippet will override the notification generated by uploadwidget plugin:

  
if (upload.total > maxImageFilesize && maxImageFilesize != 0) {
  editor.on('notificationShow', function(evt) {
    if (drupalSettings.DrupalCKeditorUploadImage.listenNow) {
      // Override the "Upload aborted by the user." notification
      // generated by uploadwidget plugin.
      evt.data.notification.message = Drupal.t(
        'The image file size (@size bytes) exceeds the maximum limit (@max bytes).', {
          '@size': upload.total,
          '@max': maxImageFilesize
        });
      evt.data.notification.type = 'warning';
      evt.data.notification.element = evt.data.notification._createElement();
    }
    evt.removeListener();
  });
  // Cancel the upload.
  upload.abort();
}
  

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.