You are here

public function ChangeLayoutForm::submitForm in Display Suite 8.2

Same name and namespace in other branches
  1. 8.4 src/Form/ChangeLayoutForm.php \Drupal\ds\Form\ChangeLayoutForm::submitForm()
  2. 8.3 src/Form/ChangeLayoutForm.php \Drupal\ds\Form\ChangeLayoutForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/ChangeLayoutForm.php, line 153

Class

ChangeLayoutForm
Provides a configuration form for configurable actions.

Namespace

Drupal\ds\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Prepare some variables.
  $old_layout = $form['#old_layout'];
  $new_layout = $form['#new_layout'];
  $old_layout_info = $form['#old_layout_info'];
  $new_layout_key = $form['#new_layout_key'];
  $entity_type = $form['#entity_type'];
  $bundle = $form['#entity_bundle'];
  $display_mode = $form['#mode'];

  // Create new third party settings.
  $third_party_settings = $old_layout;
  $third_party_settings['layout']['id'] = $new_layout_key;
  if (!empty($new_layout['library'])) {
    $third_party_settings['layout']['library'] = $new_layout['library'];
  }
  $third_party_settings['layout']['path'] = $new_layout['path'];
  unset($third_party_settings['regions']);

  // Map old regions to new ones.
  foreach ($old_layout_info['layout']['regions'] as $region => $region_title) {
    $new_region = $form_state
      ->getValue('ds_' . $region);
    if ($new_region != '' && isset($old_layout['regions'][$region])) {
      foreach ($old_layout['regions'][$region] as $field) {
        if (!isset($third_party_settings['regions'][$new_region])) {
          $third_party_settings['regions'][$new_region] = array();
        }
        $third_party_settings['regions'][$new_region][] = $field;
      }
    }
  }

  // Save configuration.

  /* @var $entity_display EntityDisplayInterface*/
  $entity_display = entity_load('entity_view_display', $entity_type . '.' . $bundle . '.' . $display_mode);
  foreach (array_keys($third_party_settings) as $key) {
    $entity_display
      ->setThirdPartySetting('ds', $key, $third_party_settings[$key]);
  }
  $entity_display
    ->save();

  // Clear entity info cache.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

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