7

I would like to change some field settings in a given view created with Views programmatically.

For example, I would like to change the "Formatter" and "Image style" settings of an Image field. This is what the settings of this field look like when I export this view:

/* Field: Taxonomy term: Natural images */
$handler->display->display_options['fields']['field_gallery_natural_images']['id'] = 'field_gallery_natural_images';
$handler->display->display_options['fields']['field_gallery_natural_images']['table'] = 'field_data_field_gallery_natural_images';
$handler->display->display_options['fields']['field_gallery_natural_images']['field'] = 'field_gallery_natural_images';
$handler->display->display_options['fields']['field_gallery_natural_images']['click_sort_column'] = 'fid';
$handler->display->display_options['fields']['field_gallery_natural_images']['type'] = 'colorbox';
$handler->display->display_options['fields']['field_gallery_natural_images']['settings'] = array(
  'colorbox_node_style' => '',
  'colorbox_image_style' => '',
  'colorbox_gallery' => 'post',
  'colorbox_gallery_custom' => '',
  'colorbox_caption' => 'auto',
  'colorbox_caption_custom' => '',
  'display_empty' => '',
  'custom_text' => '',
  'empty_callback' => 'mymodule_empty_fields_no_data',
);

I would like to change the "Image style" of the Colorbox formatter somehow like this:

$handler->display->display_options['fields']['field_gallery_natural_images']['settings']['colorbox_node_style'] = 'thumbnail';

and save that. How should I do that?

Like here:

Views - change Image field settings


EDIT 1.

Maybe I should use views_save_view():
http://api.drupal.org/api/views/views.module/function/views_save_view/7

It saves a view - so maybe I should query the whole view, change that, and use this function. But how exactly?

EDIT 2.

I found in views/includes/admin.inc that the given view gets saved like this: $form_state['view']->save(); in views_ui_edit_view_form_submit().

I already know how to change "Image style" of an Image field in a given view mode programmatically (as on "Manage display" tab after clicking the gear), but now I would be happy if I could do something similar with a given view.

12
  • try changing it in on of the hooks, i'd suggest hook_views_pre_build
    – mojzis
    Nov 1, 2012 at 17:21
  • Just FYI. You blacked out your taxonomy term in the photo; but it's in the code dump you provided.
    – chrisjlee
    Nov 1, 2012 at 19:59
  • @mojzis: thanks, but as far as I know, it just changes the display of the given view, but it doesn't actually save these changes in the database. So I would like to save the changes the same way I do it via the Views UI, but programmatically.
    – Sk8erPeter
    Nov 2, 2012 at 20:47
  • @chrisjlee: yes, thanks, I know that, but later I realized it doesn't really matter. :)
    – Sk8erPeter
    Nov 2, 2012 at 20:48
  • I Expect an answer from @Clive here.
    – niksmac
    Nov 4, 2012 at 10:03

1 Answer 1

18
+50

UPDATED:

You can use this code snippet to change a view without views_alter:

$view = views_get_view($view_name, TRUE);
$view->display['default']->display_options['fields']['field_gallery_natural_images']['settings']['colorbox_node_style'] = 'thumbnail';
views_save_view($view);

Change default display ID if you want to use display other than the default.


If you have exported the view to code using hook_views_default_views() or the Features module, there is a hook to alter the views programmatically:

/**
 * Alter default views defined by other modules.
 *
 * This hook is called right before all default views are cached to the
 * database. It takes a keyed array of views by reference.
 *
 * Example usage to add a field to a view:
 * @code
 *   $handler =& $view->display['DISPLAY_ID']->handler;
 *   // Add the user name field to the view.
 *   $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;
 * @endcode
 */
function hook_views_default_views_alter(&$views) {
  if (isset($views['taxonomy_term'])) {
    $views['taxonomy_term']->display['default']->display_options['title'] = 'Categories';
  }
}

For example:

/**
 * Implements hook_views_default_views_alter()
 */
function MYMODULE_views_default_views_alter(&$views) {
  if (isset($views['VIEW_NAME'])) {
    $views['VIEW_NAME']->display['default']->display_options['fields']['field_gallery_natural_images']['settings']['colorbox_node_style'] = 'thumbnail';
  }
}

Remember to clear the cache and do revert of the view to apply changes.

8
  • thanks, but hook_views_default_views_alter() is just for altering "default views defined by other modules", which means views created (or even overridden?) via the Views UI (which are only saved to the database) can not be changed this way, only the views defined in code, but now my purpose is to alter some particular views created via Views UI without exporting them. :-( Any other ideas?
    – Sk8erPeter
    Nov 4, 2012 at 13:02
  • For the fields to be displayed on the Views UI, in views_ui_edit_form_get_bucket(), fields are getting added and linked to admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id, and can be changed, maybe I should go this way, and trace this one... I started it, but still didn't find the real solution.
    – Sk8erPeter
    Nov 4, 2012 at 13:13
  • @Sk8erPeter I updated the answer with an alternative solution.
    – smartinm
    Nov 4, 2012 at 15:16
  • 2
    wow, using views_get_view(), changing the appropriate display settings, and after that, saving the view with views_save_view() really solved it! Thank you very much, smartinm! :) You deserve the kudos. :) I also upvoted your answer for your efforts. I tested it this way: pastebin.com/ukSy0xqM, and it really worked.
    – Sk8erPeter
    Nov 4, 2012 at 18:41
  • I'm glad that I was able to help you :)
    – smartinm
    Nov 5, 2012 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.