You are here

function viewfield_update_7202 in Viewfield 7.3

Update the 'allowed views' settings for viewfield fields so that they include the views' displays.

File

./viewfield.install, line 86
Installation functions for Viewfield module.

Code

function viewfield_update_7202() {

  // Retrieve all currently available views.
  $views = views_get_enabled_views();

  // Cycle through all the fields of type 'viewfield'.
  foreach (field_read_fields(array(
    'type' => 'viewfield',
  )) as $field_name => $info) {

    // Load the field instance.
    foreach (field_read_instances(array(
      'field_name' => $field_name,
    )) as $field_instance) {
      if (!empty($field_instance['settings']['allowed_views'])) {

        // If one or more views were selected prior to this update, we convert
        // the settings so that it includes all the displays for that view.
        $options = array();

        // Generate a list of all view|display combinations for the allowed
        // views.
        foreach (array_keys($field_instance['settings']['allowed_views']) as $view) {
          if ($field_instance['settings']['allowed_views'][$view] !== 0) {
            foreach ($views[$view]->display as $display) {
              $options[$views[$view]->name . '|' . $display->id] = $views[$view]->name . '|' . $display->id;
            }
          }
        }
        $field_instance['settings']['allowed_views'] = $options;
        field_update_instance($field_instance);
      }
    }
  }
}