You are here

function svg_image_field_widget_form_alter in Svg Image 7

Implements hook_field_widget_form_alter().

File

includes/svg_image.field.inc, line 10
Contains hooks and functions related the field settings.

Code

function svg_image_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['type'] === 'image') {
    $extensions_settings = $context['instance']['settings']['file_extensions'];
    $allowed_extensions = explode(' ', $extensions_settings);
    if (in_array('svg', $allowed_extensions)) {
      foreach ($element as $delta => $field_item) {

        // Field item could be a string for example.
        if (is_array($field_item) && !empty($field_item['#upload_validators']['file_validate_extensions'])) {

          // Checking that we have something in the validate options.
          $extensions_to_validate = !empty($field_item['#upload_validators']['file_validate_extensions'][0]) ? explode(' ', $field_item['#upload_validators']['file_validate_extensions'][0]) : [];

          // Add SVG as extension that we allow.
          $extensions_to_validate[] = 'svg';
          $field_item['#upload_validators']['file_validate_extensions'] = array(
            implode(' ', $extensions_to_validate),
          );

          // Rebuild #description with new extensions - it was built
          // in the image_field_widget_form().
          if (!empty($field_item['#description'])) {
            $field_item['#description'] = theme('file_upload_help', array(
              'description' => field_filter_xss($context['instance']['description']),
              'upload_validators' => $field_item["#upload_validators"],
            ));
          }
          $element[$delta] = $field_item;
        }
      }
    }
  }
}