You are here

public function ParserBase::validateForm in Feeds XPath Parser 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::validateForm

1 call to ParserBase::validateForm()
ParserBase::feedFormValidate in lib/Drupal/feeds_xpathparser/ParserBase.php
Overrides parent::feedFormValidate().

File

lib/Drupal/feeds_xpathparser/ParserBase.php, line 455
Contains \Drupal\feeds_xpathparser\ParserBase.

Class

ParserBase
Base class for the HTML and XML parsers.

Namespace

Drupal\feeds_xpathparser

Code

public function validateForm(array &$form, array &$form_state) {
  $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . "\n<items></items>");
  $use_errors = $this
    ->errorStart();
  $form_state['values']['context'] = trim($form_state['values']['context']);
  if (!empty($form_state['values']['context'])) {
    $result = $xml
      ->xpath($form_state['values']['context']);
  }
  $error = libxml_get_last_error();

  // Error code 1219 is undefined namespace prefix.
  // Our sample doc doesn't have any namespaces let alone the one they're
  // trying to use.
  if ($error && $error->code != 1219) {
    form_error($form['context'], t('There was an error with the XPath selector: %error', array(
      '%error' => $error->message,
    )));
    libxml_clear_errors();
  }
  foreach ($form_state['values']['sources'] as $key => &$query) {
    $query = trim($query);
    if (!empty($query)) {
      $result = $xml
        ->xpath($query);
      $error = libxml_get_last_error();
      if ($error && $error->code != 1219) {
        $variable_present = FALSE;

        // Our variable substitution options can cause syntax errors, check
        // if we're doing that.
        if ($error->code == 1207) {
          foreach ($this
            ->getOwnMappings() as $target) {
            if (strpos($query, '$' . $target) !== FALSE) {
              $variable_present = TRUE;
              break;
            }
          }
        }
        if (!$variable_present) {
          form_error($form['sources'][$key], t('There was an error with the XPath selector: %error', array(
            '%error' => $error->message,
          )));
          libxml_clear_errors();
        }
      }
    }
  }
  $this
    ->errorStop($use_errors, FALSE);
}