You are here

function ctools_context_node_settings_form_validate in Chaos Tool Suite (ctools) 7

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

Validate a node.

File

plugins/contexts/node.inc, line 107
Plugin to provide a node context. A node context is a node wrapped in a context object that can be utilized by anything that accepts contexts.

Code

function ctools_context_node_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);
  }
}