You are here

public function FieldBlockConfigForm::submitForm in Field as Block 8.2

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/FieldBlockConfigForm.php, line 161

Class

FieldBlockConfigForm
Configuration for select Entity types and delete blocks of unused types.

Namespace

Drupal\fieldblock\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $clear_cache = FALSE;
  $previous_entity_types = $this->fieldblockController
    ->getEnabledEntityTypes();
  $new_entity_types = $form_state
    ->getValue('enabled_entity_types');
  if ($previous_entity_types != $new_entity_types) {
    $clear_cache = TRUE;
  }

  // Remove all blocks for entity types that are no longer enabled.
  if ($cleanup = $form_state
    ->getValue('cleanup')) {
    foreach ($cleanup as $entity_type => $value) {
      if ($value !== 0) {

        // Find and delete the remaining blocks for this entity type.
        $this->storage
          ->deleteBlocksForEntityType($entity_type);
        $clear_cache = TRUE;
        $this
          ->messenger()
          ->addStatus($this
          ->t('Remaining field blocks for the %type entity have been deleted.', [
          '%type' => $entity_type,
        ]));
      }
      else {
        if (in_array($entity_type, $this
          ->getAllEntityTypes())) {

          // Keep the entity type in the settings if it still exists.
          $new_entity_types[$entity_type] = $entity_type;
        }
      }
    }
  }
  $this
    ->config('fieldblock.settings')
    ->set('enabled_entity_types', $new_entity_types)
    ->save();
  if ($clear_cache) {

    // Invalidate the block cache to update fieldblock derivatives.
    if ($this->moduleHandler
      ->moduleExists('block')) {
      $this->blockManager
        ->clearCachedDefinitions();
    }
  }
}