1

Using Drupal 8, the Media module, and the CKEditor that comes with D8. When you add an image media item, you get your screen like this... enter image description here

But that caption field only allows 128 characters. Media (I believe) seems to be setting a maxlength of 128 (maxlength="128"). You can still add more characters if you Embed, then edit the source it supplies, but it's still going to chop off what you've added if you double click on that image (it's technically a drupal-entity element) to bring the "Embed media" window up again.

Since this "Caption" field is not dealing with any kind of data/database storage constraints, 1.) can I change this and 2.) does this even make any sense for it to have a maxlength set?

If anyone has a clue how to change it, that'd be most helpful.

2
  • 1
    What's the caption used for? Is it rendered in its own tag or does it just go in the alt attribute for the image? If the latter, general advice is to keep alt tags at a max of 125 chars so that might make sense (not sure why it would be rounded up to a nice factor of 2 though, so it might not). If the former, there's probably no good technical reason to limit it; might even be a default. If you've already checked that the constraint isn't also in the DB, then a simple hook_form_alter would be the way to go – Clive Dec 5 '16 at 20:11
  • As the text is immediately just being dumped into the editor after hitting Embed, I'm as certain as I know how to be that it's not a data constraint issue. I AM considering the hook_form_alter route though, and am hoping for some progress with that tomorrow when I get a chance. Thanks! – UnsettlingTrend Dec 5 '16 at 21:08
2

This should do the trick no need to install any module, substituting 500 with whatever you want the limit to be

 function mymodule_form_entity_embed_dialog_alter(&$form, $form_state, $form_id) {
      if(isset($form['attributes']['data-caption'])) {
        $form['attributes']['data-caption']['#maxlength'] = '500';
      }
    }
0

I ended up installing the maxlength module, and altering the form as such:

function mymodule_form_entity_embed_dialog_alter(&$form, $form_state, $form_id) {
  if(isset($form['attributes']['data-caption'])) {
    $form['attributes']['data-caption']['#maxlength'] = '524288';
    $form['attributes']['data-caption']['#maxlength_js'] = TRUE;
  }
}

Works great! It does create a dependency with the maxlength module, and puts the "limit" on the field (I went with what it said was the "default" of 524288), but other than that 👍

2
  • What does the maxlength module contribute to the solution? I was able to use the same hook as you and only needed to set the #maxlength attribute to increase the limit past the default. – gcalex5 Jun 5 '18 at 16:54
  • Sorry, it's been a while. For D7, doesn't the module give all of the associated JavaScript for counting down the number of characters left? And also enforce the validation of it on form submission? – UnsettlingTrend Jun 7 '18 at 16:43

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.