You are here

public function PagererPresetEditForm::form in Pagerer 8

Same name and namespace in other branches
  1. 8.2 src/Form/PagererPresetEditForm.php \Drupal\pagerer\Form\PagererPresetEditForm::form()

Gets the actual form array to be built.

Overrides PagererPresetFormBase::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/PagererPresetEditForm.php, line 18

Class

PagererPresetEditForm
Base form handler for Pagerer Presets.

Namespace

Drupal\pagerer\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Fake pager for preview.
  $this->pagererFactory
    ->get(0)
    ->init(47884, 50);

  // Add admin UI library.
  $form['#attached']['library'][] = 'pagerer/admin.ui';

  // Deal with AJAX call.
  if ($form_state
    ->getValues()) {

    // Set style selection per pane.
    foreach ([
      'left',
      'center',
      'right',
    ] as $pane) {
      $this->entity
        ->setPaneData($pane, 'style', $form_state
        ->getUserInput()['panes_container'][$pane]['style']);
    }
  }
  $form['#title'] = $this
    ->t("Edit pager %preset_label", [
    '%preset_label' => $this->entity
      ->label(),
  ]);

  // AJAX messages.
  $form['ajax_messages'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'pagerer-ajax-messages',
    ],
  ];

  // List of the styles available for the panes.
  $options = [
    'none' => $this
      ->t('- None -'),
  ] + $this->styleManager
    ->getPluginOptions('base');

  // Panes configuration.
  $form['panes_container'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t("Panes configuration"),
    '#description' => $this
      ->t("Click 'Configure' to change style settings. Click 'Reset' to reset a pane configuration to its style's default."),
    '#tree' => TRUE,
  ];
  foreach ([
    'left',
    'center',
    'right',
  ] as $pane) {
    switch ($pane) {
      case 'left':
        $title = $this
          ->t("Left pane");
        break;
      case 'center':
        $title = $this
          ->t("Center pane");
        break;
      case 'right':
        $title = $this
          ->t("Right pane");
        break;
    }
    $form['panes_container'][$pane] = [
      '#type' => 'fieldset',
      '#title' => $title,
      '#attributes' => [
        'class' => [
          'pagerer-panes-container-pane',
        ],
      ],
      'style' => [
        '#type' => 'select',
        '#title' => $this
          ->t("Style"),
        '#options' => $options,
        '#default_value' => $this->entity
          ->getPaneData($pane, 'style'),
        '#ajax' => [
          'callback' => '::processStyleChange',
        ],
      ],
      'actions' => [
        '#type' => 'actions',
        'configure' => [
          '#type' => 'submit',
          '#name' => 'config_' . $pane,
          '#value' => $this
            ->t("Configure"),
          '#submit' => [
            '::submitForm',
            '::save',
          ],
        ],
        'reset' => [
          '#type' => 'submit',
          '#name' => 'reset_' . $pane,
          '#value' => $this
            ->t("Reset"),
          '#submit' => [
            '::submitForm',
            '::save',
          ],
        ],
      ],
    ];
  }

  // Pagerer's preview.
  $form['preview'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t("Pager preview"),
    '#id' => 'pagerer-pager-preview',
  ];
  $form['preview']['pagerer'] = [
    '#type' => 'pager',
    '#theme' => 'pagerer',
    '#config' => [
      'panes' => [
        'left' => $this->entity
          ->getPaneData('left'),
        'center' => $this->entity
          ->getPaneData('center'),
        'right' => $this->entity
          ->getPaneData('right'),
      ],
    ],
  ];
  return $form;
}