You are here

function ds_field_ui_layout_change_submit in Display Suite 7

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

Submit callback: save the layout change.

File

./ds.field_ui.inc, line 457
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'];
  $mappings = $form_state['mappings'];

  // 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']);

  // Remap.
  foreach ($mappings as $new_region => $old_regions) {
    foreach ($old_regions as $region_key) {
      if (isset($old_layout['settings']['regions'][$region_key])) {
        foreach ($old_layout['settings']['regions'][$region_key] 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.'));
}