You are here

function ds_panels_layout_save in Display Suite 7

Submit callback: Save a layout.

1 string reference to 'ds_panels_layout_save'
ds_panels_layout in modules/ds_extras/ds_extras.panels.inc
Return the Panels layout settings form.

File

modules/ds_extras/ds_extras.panels.inc, line 444
Administrative functions for DS panels.

Code

function ds_panels_layout_save($form, &$form_state, $clone = FALSE) {

  // Setup some variables.
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $view_mode = $form['#view_mode'];

  // Prepare settings.
  $settings = array(
    'css_id' => $form_state['values']['css_id'],
    'class' => $form_state['values']['class'],
    'sticky' => $form_state['values']['sticky'],
    'layout' => $form_state['values']['layout'],
    'hide_sidebars' => $form_state['values']['hide_sidebars'],
    'pipeline' => 'standard',
    'hide_empty_regions' => FALSE,
    'ds_panels' => TRUE,
  );

  // Common panels functions.
  ctools_include('common', 'panels');

  // Create Panels display if this is the first
  // time we're saving the layout. We'll also add
  // common fields as panes to the layout.
  if (!$form['#ds_panels_update']) {

    // Content and context plugins.
    ctools_include('content');
    ctools_include('context');
    $id = 1;

    // Create new display.
    $display = panels_new_display();
    $display->layout = $settings['layout'];
    $display->context = ds_get_entity_context($entity_type);

    // Add default fields if requested.
    if (isset($form_state['values']['add_default_fields']) && $form_state['values']['add_default_fields']) {

      // Get fields to be added on the new display.
      $fields = module_invoke_all('ds_panels_default_fields', $entity_type, $bundle, $view_mode);
      foreach ($fields as $field_name => $field) {
        if (!$form_state['values'][$field_name . '_enable']) {
          continue;
        }

        // Get a random region.
        $region = $form_state['values'][$field_name . '_region'];

        // build pane_id.
        $pane_id = 'new-' . $id;

        // Create new pane.
        $type = isset($field['field_name']) ? 'entity_field' : $field['type'];
        $subtype = isset($field['field_name']) ? $entity_type . ':' . $field_name : $field['subtype'];
        $pane = panels_new_pane($type, $subtype, TRUE);
        $pane->pid = $pane_id;
        $pane->panel = $region;
        $pane->shown = TRUE;

        // Add configuration.
        $pane->configuration['context'] = 'argument_entity_id:' . $entity_type . '_1';
        if ($form_state['values'][$field_name . '_hide_title']) {
          $pane->configuration['override_title'] = TRUE;
          $pane->configuration['override_title_text'] = '';
        }
        if (isset($field['field_name'])) {
          $field_display = field_get_display($field, $view_mode, array());
          $pane->configuration['formatter'] = $field_display['type'];
        }

        // Add pane to content and panels key of display.
        $display->content[$pane_id] = $pane;
        $display->panels[$region][] = $pane_id;
        $id++;
      }
    }

    // Save display.
    panels_save_display($display);
    $did = $display->did;

    // Add the display id to the layout settings for DS.
    $settings['did'] = $did;
  }
  else {
    $settings['did'] = $form['#did'];
  }

  // Determine the theme function / template for this display.
  $panel_layout = panels_get_layout($form_state['values']['layout']);
  $settings['theme'] = $panel_layout['theme'];

  // 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.
  $record = new stdClass();
  $record->id = $form['#export_id'];
  $record->entity_type = $entity_type;
  $record->bundle = $bundle;
  $record->view_mode = $view_mode;
  $record->layout = $form_state['values']['layout'];
  $record->settings = $settings;

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

  // Save layout 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 has been saved.'));

  // Strip off layout.
  if ($form['#strip_off_layout_from_url']) {
    $form_state['redirect'] = str_replace('/' . $settings['layout'], '', $_GET['q']);
  }
}