You are here

public static function ExifWidgetBase::validateImageField in Exif 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/ExifWidgetBase.php \Drupal\exif\Plugin\Field\FieldWidget\ExifWidgetBase::validateImageField()

Validate field to ensure it is linked to a image field.

Use in settingsForm callback.

Parameters

array $element: A form element array containing basic properties for the widget.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $form: The form structure where widgets are being attached to.

File

src/Plugin/Field/FieldWidget/ExifWidgetBase.php, line 39

Class

ExifWidgetBase
Base class for 'Exif Field widget' plugin implementations.

Namespace

Drupal\exif\Plugin\Field\FieldWidget

Code

public static function validateImageField(array $element, FormStateInterface $form_state, array $form) {
  $elementSettings = $form_state
    ->getValue($element['#parents']);
  if (!$elementSettings) {
    $field_storage_definitions = Drupal::getContainer()
      ->get('entity_field.manager')
      ->getFieldStorageDefinitions($form['#entity_type']);
    $field_storage = $field_storage_definitions[$element['#field_name']];
    if ($field_storage) {
      $args = [
        '%field' => $field_storage
          ->getName(),
      ];
      $message = t('Field %field must be link to an image field.', $args);
    }
    else {
      $message = t('Field must be link to an image field.');
    }
    $form_state
      ->setErrorByName('image_field', $message);
  }
}