You are here

function oa_core_get_titles in Open Atrium Core 7.2

Helper function to retrieve an array of node titles and links given a list of node ids

Parameters

array $ids array of node ids to fetch:

string $type optional node type to filter:

array $fields a list of fields to fetch (link is a special fieldname):

integer $limit max number of return results:

Return value

array associative array: 'titles' is a list of node titles (clean) 'links' is a list of node links 'ids' is a list of the node ids

4 calls to oa_core_get_titles()
oa_core_og_group_ref_process in includes/oa_core.fields.inc
Process function of the og_group_ref field.
oa_core_visibility_data in includes/oa_core.access.inc
Utility function to return visibility data for a given node $data['public'] TRUE if node is public, FALSE if private $data['title'] either "Public" or "Private" $data['accessors']['group']…
OgSubspacesSelectionHandler::getReferencableEntities in plugins/entityreference/selection/OgSubspacesSelectionHandler.class.php
Implements EntityReferenceHandler::getReferencableEntities().
_oa_core_build_visibility_links in includes/oa_core.access.inc
Helper function, builds links for the various visibility fields on content.

File

includes/oa_core.util.inc, line 782
Code for Utility functions for OpenAtrium spaces

Code

function oa_core_get_titles($ids = array(), $type = '', $sort_field = 'title', $fields = array(
  'title',
  'link',
  'id',
  'type',
), $sanitize = TRUE, $limit = 100) {
  $query_fields = array(
    'nid',
    'title',
  );
  $return = array();
  foreach ($fields as $field) {

    // for backwards compatibility the array keys of the $return are
    // ids, titles, links, types.  So we append an 's' to the field name
    $return[$field . 's'] = array();

    // the 'link' and 'id' field names are handled specially and are not
    // direct query field names
    if (!in_array($field, array(
      'link',
      'id',
    )) && !in_array($field, $query_fields)) {
      $query_fields[] = $field;
    }
  }
  if (!empty($ids)) {
    $query = db_select('node', 'n');
    if (!empty($sort_field) && $sort_field != 'title') {
      $query
        ->leftJoin('field_data_' . $sort_field, 's', "n.nid = s.entity_id AND s.entity_type = 'node'");
    }
    $query
      ->fields('n', $query_fields)
      ->condition('n.nid', $ids, 'IN');
    if ($sort_field == 'title') {
      $query
        ->orderBy('title');
    }
    elseif (!empty($sort_field)) {
      $query
        ->orderBy('s.' . $sort_field . '_value');
    }
    if (!empty($type)) {
      $query
        ->condition('n.type', $type);
    }
    if ($limit) {
      $query
        ->range(0, $limit);
    }
    $result = $query
      ->execute();

    // if $sort_field is empty, maintain the order of the original $ids
    // so create a lookup table.
    $nid_row = array();
    $index = 0;
    while ($row = $result
      ->fetchAssoc()) {
      $id = empty($sort_field) ? $row['nid'] : $index;
      $nid_row[$id] = $row;
      $index++;
    }
    $index = 0;
    foreach ($ids as $nid) {
      $id = empty($sort_field) ? $nid : $index;
      if (!empty($nid_row[$id])) {
        foreach ($fields as $field) {
          $field_name = $field == 'id' ? 'nid' : $field;
          $value = isset($nid_row[$id][$field_name]) ? $nid_row[$id][$field_name] : NULL;
          if ($field == 'title' && $sanitize) {
            $value = check_plain($value);
          }
          if ($field == 'link') {
            $value = l($nid_row[$id]['title'], 'node/' . $nid_row[$id]['nid']);
          }
          $return[$field . 's'][$nid_row[$id]['nid']] = $value;
        }
      }
      $index++;
    }
  }
  return $return;
}