You are here

public function SmartTitleConfigForm::submitForm in Smart Title 8

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 ConfigFormBase::submitForm

File

modules/smart_title_ui/src/Form/SmartTitleConfigForm.php, line 145

Class

SmartTitleConfigForm
SmartTitleConfigForm.

Namespace

Drupal\smart_title_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $values = $form_state
    ->getValues();
  $smart_title_bundles_setting = $smart_title_bundles = [];
  foreach ($values as $key => $bundle_values) {
    if (strpos($key, '_bundles')) {
      foreach ($bundle_values as $bundle_key => $bundle_value) {
        if ($bundle_value) {
          $smart_title_bundles_setting[] = $bundle_key;
        }
        $smart_title_bundles[] = $bundle_key;
      }
    }
  }

  // Updating entity view displays:
  // Remove smart title where it's not available anymore.
  $evd_storage = $this->entityTypeManager
    ->getStorage('entity_view_display');
  $evds = $evd_storage
    ->loadMultiple();
  $not_smart_title_capable_bundles = array_diff($smart_title_bundles, $smart_title_bundles_setting);
  foreach ($evds as $evd_id => $evd) {
    assert($evd instanceof EntityViewDisplayInterface);
    list($target_entity_type_id, $target_bundle) = explode('.', $evd_id);
    if (in_array("{$target_entity_type_id}:{$target_bundle}", $not_smart_title_capable_bundles)) {
      $evd
        ->unsetThirdPartySetting('smart_title', 'enabled')
        ->unsetThirdPartySetting('smart_title', 'settings')
        ->save();
    }
  }
  $this
    ->config('smart_title.settings')
    ->set('smart_title', $smart_title_bundles_setting)
    ->save();
  Cache::invalidateTags([
    'entity_field_info',
  ]);
}