You are here

public static function ProblemsStorage::save_problem in Node Accessibility 8

This stores a validation problem for a given node to the database.

Parameters

array $problem_data: An array of test data with the following keys:

  • nid: The node id of the node.
  • vid: The node revision id of the node.
  • test_id: The id of the problem.
  • test_severity: The severity of the problem.
  • line: The line number of the problem.
  • element: The html markup of the problem.

object|null $transaction: (optional) A valid database transaction object.

Return value

int|false The return states of either FALSE, SAVED_NEW, or SAVED_UPDATED.

1 call to ProblemsStorage::save_problem()
ProblemsStorage::replace_problem in src/ProblemsStorage.php
This stores validation problems for a single node to the database.

File

src/ProblemsStorage.php, line 488

Class

ProblemsStorage
Class DatabaseStorage.

Namespace

Drupal\node_accessibility

Code

public static function save_problem($problem_data, Transaction $transaction = NULL) {
  if (!is_array($problem_data)) {
    return FALSE;
  }
  $result = FALSE;
  $columns = [
    'nid',
    'vid',
    'test_id',
    'test_severity',
    'line',
    'element',
  ];
  foreach ($columns as $key) {
    if (empty($problem_data[$key])) {
      return FALSE;
    }
  }
  $data = [];
  $primary_key = [];
  $results = FALSE;
  if (!empty($problem_data['id'])) {
    if (!is_numeric($problem_data['id'])) {
      return FALSE;
    }
    $results = self::load_problems([
      'id' => $problem_data['id'],
    ], NULL);
    if (is_array($results) && !empty($results)) {
      $data['id'] = $problem_data['id'];
      $primary_key[] = 'id';
    }
    else {

      // if a specific id is requested but does not exist, then it cannot be updated.
      return FALSE;
    }
  }
  $data['nid'] = $problem_data['nid'];
  $data['vid'] = $problem_data['vid'];
  $data['test_id'] = $problem_data['test_id'];
  $data['test_severity'] = $problem_data['test_severity'];
  $data['line'] = $problem_data['line'];
  $data['element'] = $problem_data['element'];
  if (is_null($transaction)) {
    $transaction = \Drupal::database()
      ->startTransaction();
  }
  try {
    $result = \Drupal::database()
      ->insert('node_accessibility_problems')
      ->fields($data)
      ->execute();
  } catch (Exception $e) {
    $transaction
      ->rollback();
    \Drupal::logger('node_accessibility')
      ->error("Failed to save problem.");
    return FALSE;
  } catch (Error $e) {
    $transaction
      ->rollback();
    \Drupal::logger('node_accessibility')
      ->error("Failed to save problem.");
    return FALSE;
  }
  return $result;
}