You are here

function ds_field_ui_layouts_save in Display Suite 7

Same name and namespace in other branches
  1. 8.4 includes/field_ui.inc \ds_field_ui_layouts_save()
  2. 8.2 includes/field_ui.inc \ds_field_ui_layouts_save()
  3. 8.3 includes/field_ui.inc \ds_field_ui_layouts_save()
  4. 7.2 includes/ds.field_ui.inc \ds_field_ui_layouts_save()

Save the layout settings from the 'Manage display' screen.

1 string reference to 'ds_field_ui_layouts_save'
_ds_field_ui_table_layouts in ./ds.field_ui.inc
Add the layouts fieldset on the Field UI screen.

File

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

Code

function ds_field_ui_layouts_save($form, &$form_state) {

  // Get some default values.
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $view_mode = $form['#view_mode'];

  // Determine layout variables.
  $layout = $form_state['values']['additional_settings']['layout'];
  $hide_empty_regions = $form_state['values']['additional_settings']['hide_empty_regions'];
  $hide_sidebars = $form_state['values']['additional_settings']['hide_sidebars'];
  $old_layout = $form_state['values']['additional_settings']['old_layout'];
  $new_layout = $layout != $old_layout || empty($old_layout);

  // Save layout and add regions if necessary.
  $record = new stdClass();
  $record->id = $form['#export_id'];
  $record->entity_type = $entity_type;
  $record->bundle = $bundle;
  $record->view_mode = $view_mode;
  $record->layout = $layout;
  $record->settings = array(
    'hide_empty_regions' => $hide_empty_regions,
    'hide_sidebars' => $hide_sidebars,
  );

  // Remove old layout if necessary.
  if ($new_layout && !empty($old_layout) || $form['#panel']) {
    db_delete('ds_layout_settings')
      ->condition('entity_type', $entity_type)
      ->condition('bundle', $bundle)
      ->condition('view_mode', $view_mode)
      ->execute();
  }
  if ($new_layout && !empty($layout) || $form['#panel'] && !empty($layout)) {

    // Save new layout.
    $record->settings = $record->settings;

    // Let other modules alter the layout settings.
    drupal_alter('ds_layout_settings', $record, $form_state);

    // Move current visible fields into a default region, so
    // we keep their current settings.
    $layouts = ds_get_layout_info();
    $sl = $layouts[$layout];
    $first_region = key($sl['regions']);
    $record->settings['regions'] = array();
    $record->settings['fields'] = array();
    $record->settings['classes'] = array();
    $record->settings['wrappers'] = array();
    $record->settings['layout_wrapper'] = 'div';
    $fields = _ds_sort_fields($form_state['values']['fields'], 'weight');
    foreach ($fields as $field_key => $field) {
      if (isset($field['type']) && $field['type'] != 'hidden') {
        $record->settings['regions'][$first_region][] = $field_key;
        $record->settings['fields'][$field_key] = $first_region;
      }
    }

    // In case this is the full node view mode and if the comment module
    // is enabled for this content type, add it as well.
    if ($record->entity_type == 'node' && $record->view_mode == 'full' && module_exists('comment')) {
      $record->settings['regions'][$first_region][] = 'comments';
      $record->settings['fields']['comments'] = $first_region;
    }

    // Save the record.
    drupal_write_record('ds_layout_settings', $record);
  }
  elseif (!empty($layout)) {
    $fields = _ds_sort_fields($form_state['values']['fields'], 'weight');
    foreach ($fields as $key => $field) {

      // Make sure we need to save anything for this field.
      if (_ds_field_valid($key, $field, $form_state, $view_mode)) {
        continue;
      }
      if (!isset($record->settings['regions'][$field['region']])) {
        $record->settings['regions'][$field['region']] = array();
      }
      $record->settings['regions'][$field['region']][] = $key;
      $record->settings['fields'][$key] = $field['region'];

      // Save limit.
      $limit = isset($field['format']['limit']) ? trim($field['format']['limit']) : '';
      if (is_numeric($limit)) {
        $record->settings['limit'][$key] = $limit;
      }
    }

    // Save the region classes.
    $record->settings['classes'] = array();
    foreach (array_keys($form['fields']['#regions']) as $region) {

      // Ignore hidden region.
      if ($region == 'hidden') {
        continue;
      }

      // Additional classes on regions.
      if (isset($form_state['values']['additional_settings'][$region])) {

        // Do not save empty string.
        $classes = is_array($form_state['values']['additional_settings'][$region]) ? implode(' ', $form_state['values']['additional_settings'][$region]) : array();
        if (!empty($classes)) {
          $record->settings['classes'][$region] = $form_state['values']['additional_settings'][$region];
        }
      }
    }
    $record->settings = $record->settings;

    // Let other modules alter the layout settings.
    drupal_alter('ds_layout_settings', $record, $form_state);
    $l = $form['#ds_layout'];
    if ($l->export_type == 2) {
      drupal_write_record('ds_layout_settings', $record);
    }
    else {
      drupal_write_record('ds_layout_settings', $record, array(
        'id',
      ));
    }

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