View source
<?php
namespace Drupal\pagerer\Form;
use Drupal\Core\Form\FormStateInterface;
class PagererPresetPaneEditForm extends PagererPresetFormBase {
protected $paneLabels;
protected $pane;
protected $style;
protected $config;
protected $plugin;
public function buildForm(array $form, FormStateInterface $form_state, $pane = NULL) {
$this->paneLabels = [
'left' => $this
->t('left'),
'center' => $this
->t('center'),
'right' => $this
->t('right'),
];
$this->pane = $pane;
$this->style = $this->entity
->getPaneData($pane, 'style');
$this->config = $this->entity
->getPaneData($pane, 'config') ?: [];
$this->plugin = $this->styleManager
->createInstance($this->style, $this->config);
return parent::buildForm($form, $form_state);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['#title'] = $this
->t("%preset_name - @pane pane settings", [
'%preset_name' => $this->entity
->label(),
'@pane' => $this->paneLabels[$this->pane],
]);
$form['id']['#type'] = 'hidden';
$form['label']['#type'] = 'hidden';
$plugin_definition = $this->styleManager
->getDefinition($this->style);
$form['style_label'] = [
'#type' => 'item',
'#title' => $this
->t("Pane style"),
'#markup' => !empty($plugin_definition) ? $plugin_definition['short_title'] : NULL,
'#description' => $this
->t("To change the pane style, go back to the 'Edit pager' form."),
];
$form['config'] = $this->plugin
->buildConfigurationForm($form, $form_state);
return $form;
}
public function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['cancel'] = [
'#type' => 'link',
'#title' => $this
->t("Cancel"),
'#attributes' => [
'class' => [
'button',
],
],
] + $this->entity
->toUrl('edit-form')
->toRenderArray();
unset($actions['delete']);
return $actions;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->plugin
->validateConfigurationForm($form, $form_state);
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->plugin
->setConfigurationContext($this->entity, $this->pane);
$this->plugin
->submitConfigurationForm($form, $form_state);
parent::submitForm($form, $form_state);
$form_state
->setRedirectUrl($this->entity
->toUrl('edit-form'));
}
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$this->messenger
->addMessage($this
->t('The @pane pane configuration has been saved.', [
'@pane' => $this->paneLabels[$this->pane],
]), 'status');
}
}