diff --git a/src/Form/IconSetForm.php b/src/Form/IconSetForm.php index a20ae2f..2c63012 100644 --- a/src/Form/IconSetForm.php +++ b/src/Form/IconSetForm.php @@ -113,29 +113,39 @@ class IconSetForm extends EntityForm { $source = $form_state->getValue('source'); - // The source field cannot be empty - if (empty($source)) { - $form_state->setErrorByName('source', $this->t('The source cannot be empty.')); - } - // @See: http://php.net/manual/en/function.libxml-use-internal-errors.php - // Using libxml_use_internal_errors we can iterate into the errors generated - // by the $source loaded with simplexml_load_string. libxml_use_internal_errors(TRUE); $dom = simplexml_load_string($source); - if ($dom === FALSE || libxml_get_errors()) { - $form_state->setErrorByName('source', $this->t('The source have to be a valid svg.')); - } + // Array with LibXMLError objects if there are any errors in the buffer, or + // an empty array otherwise. + $svg_errors = libxml_get_errors(); - // Check if the current SVG contain at least one valid symbol definition - $symbols = $dom->defs->symbol; - if (!count($symbols)) { + if ($dom === FALSE || $svg_errors) { $form_state->setErrorByName('source', - $this->t('The source have to contain at least one valid symbol. - Check if the "symbol" tags are inside the "defs" tag') + $this->t('The value of source has to be a valid xml string.') ); - } + // Iterate over errors generated by an invalid svg source. + foreach ($svg_errors as $svg_error) { + $message = $this->t('%message [line %line, column %column]', [ + '%message' => $svg_error->message, + '%line' => $svg_error->line, + '%column' => $svg_error->column + ]); + + drupal_set_message($message, 'error'); + } + } + else { + // Check if the current SVG contain at least one valid symbol definition. + $symbols = $dom->defs->symbol; + if (!count($symbols)) { + $form_state->setErrorByName('source', + $this->t('The source have to contain at least one valid symbol. + Check if there are any valid "symbol" tags inside the "defs" tag.') + ); + } + } }