You are here

function ds_field_ui_layouts_save in Display Suite 7.2

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 ds.field_ui.inc \ds_field_ui_layouts_save()

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

2 string references to 'ds_field_ui_layouts_save'
ds_field_ui_change_layout_validate in includes/ds.field_ui.inc
Form validation handler for _ds_field_ui_table_layouts_preview().
_ds_field_ui_table_layouts in includes/ds.field_ui.inc
Add the layouts fieldset on the Field UI screen.

File

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

Code

function ds_field_ui_layouts_save($form, &$form_state) {
  $weight = 0;

  // Get 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'];
  $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();
  $form_state['layout_saved'] = FALSE;

  // Remove old layout if necessary.
  if ($new_layout && !empty($old_layout)) {
    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_state['layout_saved'] = TRUE;

    // 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';
    $record->settings['layout_attributes'] = '';
    $record->settings['layout_attributes_merge'] = variable_get('ds_layout_attributes_merge', TRUE);
    $record->settings['layout_link_attribute'] = FALSE;
    $record->settings['layout_link_custom'] = '';
    $fields = _ds_sort_fields($form_state['values']['fields'], 'weight');
    foreach ($fields as $field_key => $field) {

      // Ignore new fieldgroup, new field or existing field.
      if (in_array($field_key, array(
        '_add_new_field',
        '_add_existing_field',
        '_add_new_group',
      ))) {
        continue;
      }

      // Can either be form or display.
      if (isset($field['type']) && $field['type'] != 'hidden' || $record->view_mode == 'form') {
        $record->settings['regions'][$first_region][$weight++] = $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)) {
    $form_state['layout_saved'] = TRUE;
    $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']][$weight++] = $key;
      $record->settings['fields'][$key] = $field['region'];

      // Save limit.
      $limit = isset($field['format']['limit']) ? trim($field['format']['limit']) : '';
      if (is_numeric($limit) || $limit === 'delta') {
        $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;
      }
      if (isset($form_state['values']['additional_settings']['layout_class'])) {
        $record->settings['classes']['layout_class'] = $form_state['values']['additional_settings']['layout_class'];
      }

      // 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];
        }
      }

      // Additional wrappers on regions.
      if (isset($form_state['values']['additional_settings']['region_wrapper'][$region])) {
        $record->settings['wrappers'][$region] = $form_state['values']['additional_settings']['region_wrapper'][$region];
      }
    }

    // Layout wrapper
    $record->settings['layout_wrapper'] = $form_state['values']['additional_settings']['region_wrapper']['layout_wrapper'];
    $record->settings['layout_attributes'] = filter_xss_admin($form_state['values']['additional_settings']['region_wrapper']['layout_attributes']);
    $record->settings['layout_attributes_merge'] = $form_state['values']['additional_settings']['region_wrapper']['layout_attributes_merge'];

    // Link attribute.
    $record->settings['layout_link_attribute'] = $form_state['values']['additional_settings']['region_wrapper']['layout_link_attribute'];
    $record->settings['layout_link_custom'] = $form_state['values']['additional_settings']['region_wrapper']['layout_link_custom'];

    // Additional settings
    if (isset($form_state['values']['additional_settings']['preview']['info']['settings']['disable_css'])) {
      $record->settings['layout_disable_css'] = $form_state['values']['additional_settings']['preview']['info']['settings']['disable_css'];
    }
    else {
      $record->settings['layout_disable_css'] = FALSE;
    }
    $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);
  }
}