You are here

function _disclaimer_validate_title in Disclaimer 7

Validation function for the node autocomplete widget.

1 call to _disclaimer_validate_title()
disclaimer_admin_settings_validate in ./disclaimer.admin.inc
Validation function for the general configuration form.

File

./disclaimer.admin.inc, line 415
Admin page callbacks for the Disclaimer module.

Code

function _disclaimer_validate_title($form, $form_state, $key = '') {
  $title = $form_state['values'][$key];
  $matches = array();
  $nid = 0;

  // This preg_match() looks for the last pattern like [33334] and if found extracts the numeric portion.
  $result = preg_match('/\\[([0-9]+)\\]$/', $title, $matches);
  if ($result > 0) {

    // If $result is nonzero, we found a match and can use it as the index into $matches.
    $nid = $matches[$result];

    // Verify that it's a valid nid.
    $node = node_load($nid);
    if (empty($node)) {
      form_error($form[$key], t('Sorry, no node with nid %nid can be found', array(
        '%nid' => $nid,
      )));
      return;
    }
  }

  // Now, if we somehow found a nid, assign it to the node. If we failed, emit an error.
  if (!empty($nid)) {
    return $nid;
  }
  else {
    form_error($form[$key], t('Sorry, no node starting with %title can be found', array(
      '%title' => $title,
    )));
  }
}