You are here

public static function TypeSettingsStorage::merge in Node Accessibility 8

Insert or Update an entry to the database.

All existing values are reset to defaults before assigning fields.

Parameters

array|TypeSettingsStorage $fields: An array of fields to assign or update the node type settings.

bool: TRUE on success, FALSE otherwise.

1 call to TypeSettingsStorage::merge()
node_accessibility_node_type_form_submit in ./node_accessibility.module
Handles submitting the accessible content specific node type settings.

File

src/TypeSettingsStorage.php, line 280

Class

TypeSettingsStorage
Class TypeSettingsStorage.

Namespace

Drupal\node_accessibility

Code

public static function merge($fields) {
  if (is_array($fields)) {
    $sanitized = static::sanitizeFields($fields);
  }
  elseif ($fields instanceof TypeSettingsStorage) {
    $fields_array = $fields
      ->toArray();
    $sanitized = static::sanitizeFields($fields_array);
  }
  else {
    return FALSE;
  }
  if (is_null($sanitized['node_type'])) {
    return FALSE;
  }
  $existing = static::load($sanitized['node_type']);
  try {
    if (empty($existing
      ->getNodeType())) {
      \Drupal::database()
        ->insert('node_accessibility_type_settings')
        ->fields($sanitized)
        ->execute();
    }
    else {
      \Drupal::database()
        ->update('node_accessibility_type_settings')
        ->condition('node_type', $sanitized['node_type'])
        ->fields($sanitized)
        ->execute();
    }
  } catch (Exception $e) {
    \Drupal::logger('node_accessibility')
      ->error("Failed to insert or update on {node_accessibility_type_settings} table, exception: @exception.", [
      '@exception' => $e
        ->getMessage(),
    ]);
    return FALSE;
  } catch (Error $e) {
    \Drupal::logger('node_accessibility')
      ->error("Failed to insert or update on {node_accessibility_type_settings} table, exception: @exception.", [
      '@exception' => $e
        ->getMessage(),
    ]);
    return FALSE;
  }
  return TRUE;
}