8: Disable Caption on Specific Entity Embed Types in CkEditor

 

I had a devil of a time figuring out where captioning was coming from on my entity embeds.

Another team member had set it up, and I was just kind of baffled as to why captions were being offered on my entity embed forms.

It turns out that if you turn on captioning, you just get it for free on all of your embeds, and it's not configurable.

But... I don't want it for free!

Bah. Humbug. This free software doesn't have the features I think it should.

Alright, so here's how you turn off captioning:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
   // Disable captioning on paragraph embeds
  if (isset($form['attributes']['data-caption'])) {
    if ($form['attributes']['data-entity-type']['#value'] == 'paragraph') {
      unset($form['attributes']['data-caption']);
    }
  }
}

Or, you could do it this way, only allowing captioning on Media embeds:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // Disable captioning on non-media embeds
  if (isset($form['attributes']['data-caption'])) {
    if ($form['attributes']['data-entity-type']['#value'] !== 'media') {
      unset($form['attributes']['data-caption']);
    }
  }
}

If you want to find a particular bundle, dump this out (with kint from devel module):

ksm($form['attributes']['data-entity-type']);

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.