/**
  * Returns the form element for caption settings.
  *
  * @param \Drupal\Core\Field\FormatterBase $formatter
  * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  *
  * @return array
  */
 protected function captionSettings(FormatterBase $formatter, FieldDefinitionInterface $field_definition)
 {
     $field_settings = $field_definition->getSettings();
     // Set the caption options.
     $caption_options = array(0 => $formatter->t('None'), 1 => $formatter->t('Image title'), 'alt' => $formatter->t('Image ALT attribute'));
     // Remove the options that are not available.
     $action_fields = array();
     if ($field_settings['title_field'] == FALSE) {
         unset($caption_options[1]);
         // User action required on the image title.
         $action_fields[] = 'title';
     }
     if ($field_settings['alt_field'] == FALSE) {
         unset($caption_options['alt']);
         // User action required on the image alt.
         $action_fields[] = 'alt';
     }
     // Create the caption element.
     $element['caption'] = array('#title' => $formatter->t('Choose a caption source'), '#type' => 'select', '#options' => $caption_options);
     // If the image field doesn't have all of the suitable caption sources, tell the user.
     if ($action_fields) {
         $action_text = $formatter->t('enable the @action_field field', array('@action_field' => join(' and/or ', $action_fields)));
         /* This may be a base field definition (e.g. in Views UI) which means it
          * is not associated with a bundle and will not have the toUrl() method.
          * So we need to check for the existence of the method before we can
          * build a link to the image field edit form.
          */
         if (method_exists($field_definition, 'toUrl')) {
             // Build the link to the image field edit form for this bundle.
             $rel = "{$field_definition->getTargetEntityTypeId()}-field-edit-form";
             $action = $field_definition->toLink($action_text, $rel, array('fragment' => 'edit-settings-alt-field', 'query' => \Drupal::destination()->getAsArray()))->toRenderable();
         } else {
             // Just use plain text if we can't build the field edit link.
             $action = ['#markup' => $action_text];
         }
         $element['caption']['#description'] = $formatter->t('You need to @action for this image field to be able to use it as a caption.', array('@action' => render($action)));
         // If there are no suitable caption sources, disable the caption element.
         if (count($action_fields) >= 2) {
             $element['caption']['#disabled'] = TRUE;
         }
     } else {
         $element['caption']['#default_value'] = $formatter->getSetting('caption');
     }
     return $element;
 }