You are here

public function RecentlyReadConfigForm::submitForm in Recently Read 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

src/Form/RecentlyReadConfigForm.php, line 158

Class

RecentlyReadConfigForm
Class RecentlyReadConfig.

Namespace

Drupal\recently_read\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $selectedEntityTypes = $form_state
    ->getValue('entity_types');

  // Remove non-selected items.
  $selectedEntityTypes = array_filter($selectedEntityTypes);
  $enabledEntityTypes = array_keys(RecentlyReadType::loadMultiple());
  $toAdd = array_diff($selectedEntityTypes, $enabledEntityTypes);
  $toRemove = array_diff($enabledEntityTypes, $selectedEntityTypes);
  foreach ($toAdd as $entityType) {
    RecentlyReadType::create([
      'id' => $entityType,
      'label' => $this->entityTypeManager
        ->getDefinition($entityType)
        ->getLabel(),
    ])
      ->save();
  }
  foreach ($toRemove as $entityType) {
    $loadedRecentlyReadType = RecentlyReadType::load($entityType);
    if ($loadedRecentlyReadType) {
      $loadedRecentlyReadType
        ->delete();
    }
  }
  $config = $this->configFactory
    ->getEditable('recently_read.configuration');
  $config
    ->set('delete_config', $form_state
    ->getValue('delete_config'));
  $config
    ->set('delete_time', $form_state
    ->getValue('delete_time'));
  $config
    ->set('count', $form_state
    ->getValue('count'));
  $config
    ->save();

  // Clear caches to register the new relationship.
  $this->cache
    ->invalidateAll();
}