You are here

function _freelinking_nodetitle_prepare_link in Freelinking 7.3

Grab the nid associated with the title. Attempt some degree of language sensibility.

Parameters

array $target:

Return value

array

1 call to _freelinking_nodetitle_prepare_link()
freelinking_nodetitle_callback in plugins/freelinking_nodetitle.inc
Replacement callback for nodetitle plugin

File

plugins/freelinking_nodetitle.inc, line 119

Code

function _freelinking_nodetitle_prepare_link($target, $tip) {
  $link_cache =& drupal_static(__FUNCTION__, array());
  $id = md5($target['dest']);
  if (!isset($link_cache[$id])) {
    $output = array();

    // First time accessing this node. Process it.
    // $contenttypes are allowed destination contenttypes.
    $contenttypes = variable_get('freelinking_nodetitle_linkcontenttype', array());

    // We only link to nodes that are published.
    $result = db_query("SELECT n.nid FROM {node} n WHERE n.title = :title AND n.status = 1", array(
      ":title" => $target['dest'],
    ));
    $all = $result
      ->fetchAll();
    $found = FALSE;
    foreach ($all as $item) {
      $nodex = node_load($item->nid);

      // Node must be allowed type and user must have view access.
      if ($contenttypes[$nodex->type] && node_access('view', $nodex)) {
        $found = TRUE;
        break;
      }
    }
    if ($found && isset($nodex)) {
      $tooltip = $target['tooltip'] ? $target['tooltip'] : $tip;
      $output = array(
        'title' => $nodex->title,
        'url' => 'node/' . $nodex->nid,
        'language' => isset($nodex->language) ? $nodex->language : LANGUAGE_NONE,
        'tooltip' => $tooltip,
      );
    }

    // Save processed data in static cache.
    $link_cache[$id] = $output;
  }
  return $link_cache[$id];
}