You are here

public function LayoutParagraphsSectionsSettingsForm::submitForm in Layout Paragraphs 1.0.x

Same name and namespace in other branches
  1. 2.0.x src/Form/LayoutParagraphsSectionsSettingsForm.php \Drupal\layout_paragraphs\Form\LayoutParagraphsSectionsSettingsForm::submitForm()

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/LayoutParagraphsSectionsSettingsForm.php, line 150

Class

LayoutParagraphsSectionsSettingsForm
Class LayoutParagraphsSectionsSettingsForm.

Namespace

Drupal\layout_paragraphs\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $paragraph_bundles = $this->entityTypeBundleInfo
    ->getBundleInfo('paragraph');
  $paragraphs_type_storage = $this->entityTypeManager
    ->getStorage('paragraphs_type');
  $layouts = $this->layoutPluginManager
    ->getSortedDefinitions();
  foreach (array_keys($paragraph_bundles) as $name) {

    /** @var \Drupal\paragraphs\Entity\ParagraphsType $paragraphs_type */
    $paragraphs_type = $paragraphs_type_storage
      ->load($name);
    $layout_paragraphs_behavior = $paragraphs_type
      ->getBehaviorPlugin('layout_paragraphs');
    if ($form_state
      ->getValue($name)) {
      $layout_paragraphs_behavior
        ->setConfiguration([
        'enabled' => TRUE,
      ]);
      $config = [
        'enabled' => TRUE,
        'available_layouts' => [],
      ];
      foreach ($form_state
        ->getValue($name . '_layouts') as $layout_id) {
        $config['available_layouts'][$layout_id] = $layouts[$layout_id]
          ->getLabel();
      }
      $layout_paragraphs_behavior
        ->setConfiguration($config);
    }
    else {
      $layout_paragraphs_behavior
        ->setConfiguration([
        'enabled' => FALSE,
      ]);
    }
    $paragraphs_type
      ->save();
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t('The Layout Paragraphs settings have been saved.'));
}