You are here

function autocomplete_widgets_field_formatter_view in Autocomplete Widgets for Text and Number Fields 7

Implements hook_field_formatter_view().

File

./autocomplete_widgets.module, line 201
Provides autocomplete widgets for Text and Number fields.

Code

function autocomplete_widgets_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $result = array();
  switch ($display['type']) {
    case 'autocomplete_widgets_nr':
      switch ($field['type']) {
        case 'text':
          if (isset($items[0]['value']) && isset($instance['widget']['settings']['allowed_node_types'])) {

            // If there exists a node with that title, display it as a link.
            $query = new EntityFieldQuery();
            $nodes = $query
              ->entityCondition('entity_type', 'node')
              ->entityCondition('bundle', $instance['widget']['settings']['allowed_node_types'], 'IN')
              ->propertyCondition('title', $items[0]['value'])
              ->propertyCondition('status', 1);
            $nodes = $nodes
              ->execute();
            $nid = array();
            if (isset($nodes['node'])) {
              foreach ($nodes['node'] as $key => $obj) {
                $nid[] = $key;

                // If there are more than one nodes having this title we
                // only care about the first one.
                break;
              }
            }
            if (isset($nid[0])) {
              $node = node_load($nid[0]);
              return array(
                array(
                  '#markup' => l($node->title, 'node/' . $node->nid),
                ),
              );
            }

            // If no node found with this title, display name as plain text.
            return array(
              array(
                '#markup' => $items[0]['value'],
              ),
            );
          }
          break;
      }
      break;
  }
  return $result;
}