iLexN
9/27/2017 - 1:15 AM

node autocomplete setting

<?php

function _scmp_mobile_nid_validate( $nid, $tid){

  preg_match('/\[nid: (\d+)\]$/', $nid, $matches);
  // refs #11503: Check and prevent duplicate node ID being push only to non-admin users.
  if (isset($matches[1]) && !user_access('allow push duplicate node notification')) {
    $query = db_select('scmp_mobile_fcm_log', 'log')
      ->condition('log.nid', $matches[1])
      ->condition('app_tid',$tid)
      ->fields('log', array('nid'))
      ->execute();
    if ($query->rowCount() > 0) {
      form_set_error('nid', t('You are NOT allowed to push duplicate article: %nid', array('%nid' => $form_state['values']['nid'])));
    }
  }

  if (!isset($matches[1]) || !$node = node_load($matches[1])) {
    form_set_error('nid', t('Invalid article %nid', array('%nid' => $nid)));
    return false;
  }
  elseif ($node && $node->status != NODE_PUBLISHED) {
    form_set_error('nid', t('Selected article is not published: %nid', array('%nid' => $nid)));
    return false;
  }
  else {
    return $node;
  }
}
<?php

function scmp_mobile_thelift_notification_autocomplete($string){
  $matches = _scmp_mobile_find_nid_with_app_id($string, TERM_APPLICATION_DAILY);
  drupal_json_output(drupal_map_assoc($matches));
}

function _scmp_mobile_find_nid_with_app_id($string, $app_id){
  global $user;
  $matches = array();
  $query = db_select('node', 'n')
    ->addTag('node_access')
    ->addMetaData('app_id', $app_id)
    ->fields('n', array('nid', 'tnid', 'title'))
    ->condition('n.type', array('article'), 'IN')
    ->range(0, 10);
  if (!user_access('administer nodes', $user)) {
    $query->condition(db_or()->condition('n.status', 1)->condition('n.uid', $user->uid));
  }
  // Run a match to see if they're specifying by nid.
  $preg_matches = array();
  $match = preg_match('/\[nid: (\d+)\]/', $string, $preg_matches);
  if (!$match) {
    $match = preg_match('/^nid: (\d+)/', $string, $preg_matches);
  }

  if ($match) {
    // If it found a nid via specification, reduce our resultset to just that nid.
    $query->condition('n.nid', $preg_matches[1]);
  }
  else {
    // Build the constant parts of the query.
    $query->where('LOWER(n.title) LIKE LOWER(:string)', array(':string' => '%' . db_like($string) . '%'));
  }
  $result = $query->execute();

  foreach ($result as $node) {
    $matches[$node->nid] = check_plain($node->title) . ' [nid: ' . $node->nid . ']';
  }

  return $matches;
}
<?php

/**
 * Implementation of hook_menu().
 */
function scmp_mobile_menu() {
$items['admin/config/services/mobile_notification/the-lift/autocomplete/%'] = array(
    'title' => 'Autocomplete',
    'page callback' => 'scmp_mobile_thelift_notification_autocomplete',
    'page arguments' => array(6),
    'access arguments' => array('send fcm notification'),
    'file' => 'scmp_mobile.fcm.inc',
    'type' => MENU_CALLBACK
  );
  return $items;
}