You are here

function ds_field_ui_layout_change_submit in Display Suite 7.2

Same name and namespace in other branches
  1. 7 ds.field_ui.inc \ds_field_ui_layout_change_submit()

Submit callback: save the layout change.

File

includes/ds.field_ui.inc, line 573
Field UI functions for Display Suite.

Code

function ds_field_ui_layout_change_submit($form, &$form_state) {

  // Prepare some variables.
  $old_layout = $form['#old_layout'];
  $new_layout = $form['#new_layout'];
  $new_layout_key = $form['#new_layout_key'];
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $view_mode = $form['#view_mode'];

  // Create new record.
  $record = new stdClass();
  $record->id = $form['#export_id'];
  $record->entity_type = $entity_type;
  $record->bundle = $bundle;
  $record->view_mode = $view_mode;
  $record->layout = $new_layout_key;
  $record->settings = $old_layout['settings'];
  unset($record->settings['regions']);
  unset($record->settings['fields']);

  // map old regions to new ones
  foreach ($old_layout['regions'] as $region => $region_title) {
    $new_region = $form_state['values']['ds_' . $region];
    if ($new_region != '' && isset($old_layout['settings']['regions'][$region])) {
      foreach ($old_layout['settings']['regions'][$region] as $field_key => $field) {
        if (!isset($record->settings['regions'][$new_region])) {
          $record->settings['regions'][$new_region] = array();
        }
        $record->settings['regions'][$new_region][] = $field;
        $record->settings['fields'][$field] = $new_region;
      }
    }
  }

  // Remove old record.
  db_delete('ds_layout_settings')
    ->condition('entity_type', $entity_type)
    ->condition('bundle', $bundle)
    ->condition('view_mode', $view_mode)
    ->execute();

  // Save new record.
  drupal_write_record('ds_layout_settings', $record);

  // Clear entity info cache.
  cache_clear_all('entity_info', 'cache', TRUE);

  // Show message.
  drupal_set_message(t('The layout change has been saved.'));
}