You are here

public function TagSetForm::save in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/Form/TagSetForm.php \Drupal\xbbcode\Form\TagSetForm::save()

Throws

\Drupal\Core\Entity\EntityMalformedException

\Drupal\Core\Entity\Exception\UndefinedLinkTemplateException

\Drupal\Core\Entity\EntityStorageException

Overrides EntityForm::save

File

src/Form/TagSetForm.php, line 305

Class

TagSetForm
Base form for tag sets.

Namespace

Drupal\xbbcode\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $result = parent::save($form, $form_state);
  $old = $form['formats']['#default_value'];
  $new = array_filter($form_state
    ->getValue('formats'));
  $update = [
    '' => array_diff_assoc($old, $new),
    $this->entity
      ->id() => array_diff_assoc($new, $old),
  ];
  foreach ($update as $tag_set => $formats) {

    /** @var \Drupal\filter\FilterFormatInterface $format */
    foreach ($this->formatStorage
      ->loadMultiple($formats) as $id => $format) {
      $filter = $format
        ->filters('xbbcode');
      $config = $filter
        ->getConfiguration();
      $config['settings']['tags'] = $tag_set;
      $filter
        ->setConfiguration($config);
      $format
        ->save();
    }
  }
  if ($result === SAVED_NEW) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The BBCode tag set %set has been created.', [
      '%set' => $this->entity
        ->label(),
    ]));
  }
  elseif ($result === SAVED_UPDATED) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The BBCode tag set %set has been updated.', [
      '%set' => $this->entity
        ->label(),
    ]));
  }
  $form_state
    ->setRedirectUrl($this->entity
    ->toUrl('collection'));
}