You are here

function image_field_caption_field_widget_form_alter in Image Field Caption 8

Same name and namespace in other branches
  1. 7.2 image_field_caption.module \image_field_caption_field_widget_form_alter()

Implements hook_field_widget_form_alter().

File

./image_field_caption.module, line 29
Provides a caption textarea for image fields.

Code

function image_field_caption_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  /** @var \Drupal\field\Entity\FieldConfig $field */
  $field = $context['items']
    ->getFieldDefinition();

  // If the current field is an image field.
  if ($field
    ->getType() == 'image') {

    // Get the current field settings.
    $settings = $field
      ->getSettings();

    // Check if the current field has the caption.
    if (!empty($settings['caption_field'])) {
      $element['#caption_field_required'] = $settings['caption_field_required'];
      $element['#process'][] = '_image_field_caption_widget_process';
    }
  }
}