Guest User

Untitled

a guest
Dec 8th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. $form['node_items'] = array(
  2. '#type' => 'textfield',
  3. '#autocomplete_route_name' => 'mymodulenode.autocomplete',
  4. '#autocomplete_route_parameters' => array('skip_node_id_string' => $skip_node_id_string),
  5. );
  6.  
  7. public function autocomplete(Request $request,$skip_node_id_string) {
  8.  
  9. $skip_node_id = explode("&", $skip_node_id_string);
  10.  
  11. $string = $request->query->get('q');
  12. if ($string) {
  13. $nids = Drupal::entityQuery('node')
  14. ->condition('status', 'NODE_PUBLISHED')
  15. ->condition('nid', $skip_node_id , 'NOT IN')
  16. ->condition('title', $string, 'CONTAINS')
  17. ->execute();
  18. $nodes = Node::loadMultiple($nids);
  19. foreach($nodes as $node){
  20. $path_alias = 'node/'.$node->get('nid')->value;
  21. $alias = Drupal::service('path.alias_manager')->getPathByAlias($path_alias);
  22. //$alias = drupal_get_path_alias('node/'.$type['nid']);
  23. $alias = (strlen($alias) > 23) ? substr($alias,0,20).'...' : $alias;
  24. $matches[] = array('label' => $node->get('title')->value.'['. $node->get('nid')->value .', '.$alias .']', 'value' => $node->get('title')->value . '[nid:' . $node->get('nid')->value . ']');
  25. }
  26. }
  27. return new JsonResponse($matches);
  28. }
Add Comment
Please, Sign In to add comment