Skip to content

Instantly share code, notes, and snippets.

@awolfey
Created January 8, 2015 16:42
Show Gist options
  • Save awolfey/5db0c89ef98878d07e70 to your computer and use it in GitHub Desktop.
Save awolfey/5db0c89ef98878d07e70 to your computer and use it in GitHub Desktop.
Make alternative text required in CKeditor image dialog.

This makes the alt text required in ckeditor and also adds Drupal's required span.

CKEDITOR.on('dialogDefinition', function(ev) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
var dialog = ev.data.definition.dialog;
switch (dialogName) {
case 'image': //Image Properties dialog
// Require alt text.
var altField = infoTab.get('txtAlt');
altField.label = 'Alternative text <span class="form-required" title="This field is required.">*</span>';
altField.validate = function() {
var alt = dialog.getContentElement('info', 'txtAlt');
if (alt.getValue() == '') {
return "Alternative text is required.";
}
}
break;
}
});
@vinylthieves
Copy link

You'll need to define 'infoTab' before line 10:

var infoTab = dialogDefinition.getContents( 'info' );

Otherwise great code! Thanks!

@jdiglesias
Copy link

for image2, just change 'image' to 'image2' and 'txtAlt' to 'alt'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment