You are here

function _petreference_potential_references_views in Previewable email templates 6

Helper function for _petreference_potential_references(): case of Views-defined referenceable PETS.

1 call to _petreference_potential_references_views()
_petreference_potential_references in modules/petreference/petreference.module
Fetch an array of all candidate referenced pets.

File

modules/petreference/petreference.module, line 765
Defines a field type for referencing pet template to a node.

Code

function _petreference_potential_references_views($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
  $view_name = $field['advanced_view'];
  if ($view = views_get_view($view_name)) {

    // We add a display, and let it derive from the 'default' display.
    // TODO: We should let the user pick a display in the fields settings - sort of requires AHAH...
    $display = $view
      ->add_display('content_references');
    $view
      ->set_display($display);

    // TODO from merlinofchaos on IRC : arguments using summary view can defeat the style setting.
    // We might also need to check if there's an argument, and set *its* style_plugin as well.
    $view->display_handler
      ->set_option('style_plugin', 'content_php_array_autocomplete');
    $view->display_handler
      ->set_option('row_plugin', 'fields');

    // Used in content_plugin_style_php_array::render(), to get
    // the 'field' to be used as title.
    $view->display_handler
      ->set_option('content_title_field', 'title');

    // Additional options to let content_plugin_display_references::query()
    // narrow the results.
    $options = array(
      'table' => 'pets',
      'field_string' => 'title',
      'string' => $string,
      'match' => $match,
      'field_id' => 'pid',
      'ids' => $ids,
    );
    $view->display_handler
      ->set_option('content_options', $options);

    // TODO : for consistency, a fair amount of what's below
    // should be moved to content_plugin_display_references
    // Limit result set size.
    $limit = isset($limit) ? $limit : 0;
    $view->display_handler
      ->set_option('items_per_page', $limit);

    // Get arguments for the view.
    if (!empty($field['advanced_view_args'])) {

      // TODO: Support Tokens using token.module ?
      $view_args = array_map('trim', explode(',', $field['advanced_view_args']));
    }
    else {
      $view_args = array();
    }

    // We do need title field, so add it if not present (unlikely, but...)
    $fields = $view
      ->get_items('field', $display);
    if (!isset($fields['title'])) {
      $view
        ->add_item($display, 'field', 'pets', 'title');
    }

    // If not set, make all fields inline and define a separator.
    $options = $view->display_handler
      ->get_option('row_options');
    if (empty($options['inline'])) {
      $options['inline'] = drupal_map_assoc(array_keys($view
        ->get_items('field', $display)));
    }
    if (empty($options['separator'])) {
      $options['separator'] = '-';
    }
    $view->display_handler
      ->set_option('row_options', $options);

    // Make sure the query is not cached
    $view->is_cacheable = FALSE;

    // Get the results.
    $result = $view
      ->execute_display($display, $view_args);
  }
  else {
    $result = FALSE;
  }
  return $result;
}