I'm trying to use hook_views_default_views_alter() to add fields to the view defined by another module.

I've tried:

1.
$views[$view_name]->add_item('default', 'field', $table_name, 'edit_link');

-- causes an out of memory error (on 256Mb)

2.

    $views[$view_name]->display['default']->handler->options['fields']['edit_link'] = array(
      'label' => 'edit link',
      'id' => 'edit_link',
      'table' => $table_name,
      'field' => 'edit_link',
      'exclude' => 0,
      'relationship' => 'none',
    );

-- seems to do nothing

3.

    $fields = $views[$view_name]->display['default']->handler->options['fields'];
    $fields['edit_link'] = array(
      'label' => 'edit link',
      'id' => 'edit_link',
      'table' => $table_name,
      'field' => 'edit_link',
      'exclude' => 0,
      'relationship' => 'none',
    );
    $views[$view_name]->display['default']->handler->override_option('fields', $fields);

-- out of memory error, it appears theme_status_messages() is full.

What's the correct way to do this?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

Status: Active » Fixed

This advice from Dereine on IRC works:

$handler =& $view->display['foo']->handler;
  /* Field: User: Name */
  $handler->display->display_options['fields']['name']['id'] = 'name';
  $handler->display->display_options['fields']['name']['table'] = 'users';
  $handler->display->display_options['fields']['name']['field'] = 'name';
  $handler->display->display_options['fields']['name']['label'] = 'Author';
  $handler->display->display_options['fields']['name']['link_to_user'] = 1;
joachim’s picture

Title: how to use hook_views_default_views_alter() to add fields to default views? » document how to use hook_views_default_views_alter() to add fields to default views
Component: Code » Documentation
Category: support » bug
Status: Fixed » Needs review
FileSize
1.03 KB

Here's a patch for the docs.

joachim’s picture

Version: 7.x-3.0-rc1 » 7.x-3.x-dev

Setting correct version.

dawehner’s picture

Status: Needs review » Fixed

Thanks man!

Commited to 7.x-3.x and 6.x-3.x

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.