thierry-b
12/16/2014 - 8:41 AM

Adding node type and node id to the link field in WYSIWYG.

Adding node type and node id to the link field in WYSIWYG.

//----------------------------------------
//------------- CKEDITOR -----------------
//----------------------------------------
/* 
 * alter cklink to add language 
*/
function YOUR_MODULE_ckeditor_link_autocomplete_alter(&$results, &$string){
  if ($string !== '') {
    $types = ckeditor_link_get_types();
    $results = array();
    foreach ($types as $type) {
      $func = $type['module'] .'_ckeditor_link_'. $type['type'] .'_autocomplete';
      if($type['type']=="node"){
        $func="ckeditor_link_ckeditor_link_node_autocomplete_custom";
      }     
      if (function_exists($func)) {
        $results += $func($string);
        if (count($results) > 10) {
          break;
        }
      }
    }
  }
}

function ckeditor_link_ckeditor_link_node_autocomplete_custom($string) {
  $matches = array();
  $node_types = array_keys(array_filter(variable_get('ckeditor_link_autocomplete_node_types', array('- any -' => '- any -'))));
  if (count($node_types)) {
    //we assume i18n is enabled
    global $language;
    $db_or = db_or();
    $db_or->condition('n.language', 'und');
    $db_or->condition('n.language', $language->language);
    $query = db_select('node', 'n')
    ->fields('n', array('nid', 'title','language'))
    ->condition('n.title', '%'. db_like($string) .'%', 'LIKE')
    ->condition($db_or)
    ->orderBy('n.title')
    ->orderBy('n.type')
    ->range(0, 10)
    ->addTag('node_access');
    if (!in_array('- any -', $node_types)) {
      $query->condition('n.type', $node_types, 'IN');
    }
    $result = $query->execute();
    foreach ($result as $node) {
      $matches['node/'. $node->nid] = "TAAL:".$node->language." ".drupal_get_path_alias("node/".$node->nid);
    }
  }
  return $matches;
}