You are here

function noderelationships_customize_noderef_view in Node Relationships 6

Apply custom configuration to the given search and reference view.

1 call to noderelationships_customize_noderef_view()
noderelationships_views_pre_view in ./noderelationships.module
Implementation of hook_views_pre_view().

File

./noderelationships.inc, line 1131
Common functions for the noderelationships module.

Code

function noderelationships_customize_noderef_view(&$view, $display_id, &$view_args, $reset_current_display = TRUE) {

  // Nothing to do if the view has already been processed.
  if (!empty($view->noderelationships_processed)) {
    return;
  }

  // Mark the view as being already processed.
  $view->noderelationships_processed = TRUE;

  // If the view is not executed with the expected number of arguments, then
  // we should abort this view.
  if (count($view_args) < 2) {
    noderelationships_abort_view($view, $view_args);
    return FALSE;
  }

  // We expect to see the following arguments:
  // 0 - type of the referrer node.
  // 1 - name of the nodereference field in the referrer type.
  list($type_name, $field_name) = $view_args;

  // Load settings of the referrer field.
  $referrer_field = content_fields($field_name, $type_name);
  if (empty($referrer_field)) {
    drupal_set_message(t('Could not load field information to properly customize the view %view-name.', array(
      '%view-name' => $view->name,
    )), 'error');
    watchdog('noderelationships', 'Could not load field information to properly customize the view %view-name (field: @field-name, type: @type-name).', array(
      '%view-name' => $view->name,
      '@field-name' => $field_name,
      '@type-name' => $type_name,
    ), WATCHDOG_ERROR);
    noderelationships_abort_view($view, $view_args);
    return FALSE;
  }

  // Load the related node type.
  $referrer_type = noderelationships_get_localized_content_type($type_name);

  // Reset the current page display so that the changes take effect.
  // This is necessary for AJAX requests and page displays.
  if ($reset_current_display && isset($view->current_display)) {
    unset($view->current_display);
  }

  // Activate the specified display.
  $view
    ->set_display($display_id);

  // Get view overrides for the given nodereference field.
  $view_overrides = noderelationships_get_noderef_view_overrides($view, $referrer_field);

  // Build custom title, if nothing exists.
  $view_title = $view
    ->get_title();
  if (empty($view_title)) {

    // Prepare default view title.
    $arguments = array(
      '%referrer-label' => $referrer_field['widget']['label'],
    );
    $view_title = t('Search and reference %referrer-label', $arguments);

    // Allow external modules alter the view title.
    $context = array(
      'referrer_type' => $referrer_type,
      'referrer_field' => $referrer_field,
      'field_name' => $field_name,
    );
    noderelationships_alter_label($view_title, 'noderef_search_page_title', $context, $arguments);
    $view_overrides['title'] = $view_title;
  }

  // Allow external modules alter the view.
  drupal_alter('noderelationships_view', $view_overrides, $view, $display_id, $view_args);

  // Apply dynamic customization to the view display.
  foreach ($view_overrides as $option => $definition) {
    $view->display_handler
      ->override_option($option, $definition);
  }
  return TRUE;
}