You are here

public function ParagraphsTypeForm::save in Paragraphs 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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');
  }
}