You are here

function panels_nid_autocomplete in Panels 5.2

Helper function for parsing an autocomplete node field.

Parameters

$string: A string in autocomplete syntax (e.g. ".... [nid: 123]"), or a typed-in nid, or a node title.

Return value

Either a valid nid or NULL.

3 calls to panels_nid_autocomplete()
panels_context_node_edit_form_settings_form_validate in contexts/node_edit_form.inc
Validate a node.
panels_context_node_settings_form_validate in contexts/node.inc
Validate a node.
panels_node_legacy_edit_validate in panels_node_legacy/panels_node_legacy.module
Validate a node.

File

./panels.module, line 1312
panels.module Core API for Panels. Provides display editing and rendering capabilities.

Code

function panels_nid_autocomplete($int) {
  $nid = NULL;
  if (is_numeric($int)) {

    // The user typed a NID outright.
    $nid = $int;
  }
  else {

    // Else, it might be an autocomplete syntax.
    $preg_matches = array();
    $match = preg_match('/\\[nid: (\\d+)\\]/', $int, $preg_matches) || preg_match('/^nid: (\\d+)/', $int, $preg_matches);
    if ($match) {
      $nid = $preg_matches[1];
    }
  }
  if (isset($nid)) {

    // Verify that node exists and we have access to it.
    $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $nid));
  }
  else {

    // Try to find a node having that title.
    $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE LOWER(n.title) = LOWER('%s')"), $int));
  }
  if ($node) {
    return $node->nid;
  }
}