You are here

function notifications_node_title2nid in Notifications 7

Same name and namespace in other branches
  1. 6.4 includes/node.inc \notifications_node_title2nid()
  2. 6 notifications.node.inc \notifications_node_title2nid()
  3. 6.2 notifications.node.inc \notifications_node_title2nid()
  4. 6.3 notifications.node.inc \notifications_node_title2nid()

Reverse mapping from node title to nid

We also handle autocomplete values (title [nid:x]) and validate the form

1 call to notifications_node_title2nid()
Notifications_Node_Field::autocomplete_parse in ./notifications.field.inc
Parse value from autocomplete

File

includes/node.inc, line 28
Notifications node API for use by plug-in modules providing node related features

Code

function notifications_node_title2nid($name, $field = NULL, $types = array()) {
  if (!empty($name)) {
    preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $name, $matches);
    if (!empty($matches)) {

      // Explicit [nid:n].
      list(, $title, $nid) = $matches;
      if (!empty($title) && ($node = node_load($nid)) && $title != $node->title) {
        if ($field) {
          form_set_error($field, t('Node title mismatch. Please check your selection.'));
        }
        $nid = NULL;
      }
    }
    else {

      // No explicit nid.
      $reference = _notifications_node_references($name, 'equals', $types, 1);
      if (!empty($reference)) {
        $nid = key($reference);
      }
      elseif ($field) {
        form_set_error($field, t('Found no valid post with that title: %title', array(
          '%title' => $name,
        )));
      }
    }
  }
  return !empty($nid) ? $nid : NULL;
}