You are here

public static function ForwardLinkFormatter::validateIconPath in Forward 4.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/Field/FieldFormatter/ForwardLinkFormatter.php \Drupal\forward\Plugin\Field\FieldFormatter\ForwardLinkFormatter::validateIconPath()

Ensure the custom icon path is valid if provided.

Parameters

array $element: The validated element.

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

File

src/Plugin/Field/FieldFormatter/ForwardLinkFormatter.php, line 158

Class

ForwardLinkFormatter
Plugin implementation of the Forward Link formatter.

Namespace

Drupal\forward\Plugin\Field\FieldFormatter

Code

public static function validateIconPath(array $element, FormStateInterface $form_state) {
  $plugin_name = $form_state
    ->get('plugin_settings_edit');
  $key = [
    'fields',
    $plugin_name,
    'settings_edit_form',
    'settings',
    'icon',
  ];
  $icon = $form_state
    ->getValue($key);
  if ($icon) {
    $image = File::create();
    $image
      ->setFileUri($icon);
    $filename = \Drupal::service('file_system')
      ->basename($image
      ->getFileUri());
    $image
      ->setFilename($filename);
    $errors = file_validate_is_image($image);
    if (count($errors)) {
      $form_state
        ->setError($element, t('The specified icon is not a valid image. Please double check the path.'));
    }
  }
}