You are here

function ctools_context_node_edit_form_settings_form_validate in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 plugins/contexts/node_edit_form.inc \ctools_context_node_edit_form_settings_form_validate()

Validate a node.

File

plugins/contexts/node_edit_form.inc, line 127
Plugin to provide a node_edit_form context.

Code

function ctools_context_node_edit_form_settings_form_validate($form, &$form_state) {

  // Validate the autocomplete.
  if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
    form_error($form['node'], t('You must select a node.'));
    return;
  }
  if (empty($form_state['values']['node'])) {
    return;
  }
  $nid = $form_state['values']['node'];
  $preg_matches = array();
  $match = preg_match('/\\[id: (\\d+)\\]/', $nid, $preg_matches);
  if (!$match) {
    $match = preg_match('/^id: (\\d+)/', $nid, $preg_matches);
  }
  if ($match) {
    $nid = $preg_matches[1];
  }
  if (is_numeric($nid)) {
    $node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(
      ':nid' => $nid,
    ))
      ->fetchObject();
  }
  else {
    $node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(
      ':title' => $nid,
    ))
      ->fetchObject();
  }

  // Do not allow unpublished nodes to be selected by unprivileged users.
  if (!$node || empty($node->status) && !user_access('administer nodes')) {
    form_error($form['node'], t('Invalid node selected.'));
  }
  else {
    form_set_value($form['nid'], $node->nid, $form_state);
  }
}