public function ParagraphsTypeForm::save

File

paragraphs/src/Form/ParagraphsTypeForm.php, line 200

Class

ParagraphsTypeForm
Form controller for paragraph type forms.

Namespace

Drupal\paragraphs\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $paragraphs_type = $this->entity;
  if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager
    ->getApplicableDefinitions($paragraphs_type)) {
    foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
      $behavior_plugin = $paragraphs_type
        ->getBehaviorPlugin($id);

      // If the behavior is enabled, initialize the configuration with the
      // enabled key and then let it process the form input.
      if ($form_state
        ->getValue([
        'behavior_plugins',
        $id,
        'enabled',
      ])) {
        $behavior_plugin
          ->setConfiguration([
          'enabled' => TRUE,
        ]);
        if (isset($form['behavior_plugins'][$id]['settings'])) {
          $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
          $behavior_plugin
            ->submitConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
        }
      }
      else {

        // The plugin is not enabled, remove it from the paragraphs type.
        $paragraphs_type
          ->getBehaviorPlugins()
          ->removeInstanceId($id);
      }
    }
  }
  $status = $paragraphs_type
    ->save();
  $this->messenger
    ->addMessage($this
    ->t('Saved the %label Paragraphs type.', array(
    '%label' => $paragraphs_type
      ->label(),
  )));
  if ($status == SAVED_NEW && $this->moduleHandler
    ->moduleExists('field_ui') && ($route_info = FieldUI::getOverviewRouteInfo('paragraph', $paragraphs_type
    ->id()))) {
    $form_state
      ->setRedirectUrl($route_info);
  }
  else {
    $form_state
      ->setRedirect('entity.paragraphs_type.collection');
  }
}