PanelizerWizardGeneralForm.php in Panelizer 8.3
File
src/Form/PanelizerWizardGeneralForm.php
View source
<?php
namespace Drupal\panelizer\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\panelizer\PanelizerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelizerWizardGeneralForm extends FormBase {
protected $machine_name;
protected $panelizer;
protected $entityTypeId;
protected $bundle;
protected $viewMode;
public function __construct(RouteMatchInterface $route_match, PanelizerInterface $panelizer) {
$this->routeMatch = $route_match;
if ($route_match
->getRouteName() == 'panelizer.wizard.add') {
$this->entityTypeId = $route_match
->getParameter('entity_type_id');
$this->bundle = $route_match
->getParameter('bundle');
$this->viewMode = $route_match
->getParameter('view_mode_name');
}
$this->panelizer = $panelizer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('current_route_match'), $container
->get('panelizer'));
}
public function getFormId() {
return 'panelizer_wizard_general_form';
}
public static function validateMachineName($machine_name, $element) {
if (isset($element['#machine_name']['prefix'])) {
$panelizer = \Drupal::service('panelizer');
$full_machine_name = $element['#machine_name']['prefix'] . '__' . $machine_name;
return $panelizer
->getDefaultPanelsDisplayByMachineName($full_machine_name);
}
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$plugin = $cached_values['plugin'];
$form_state = new FormState();
$form_state
->setValues($form_state
->getValue('variant_settings', []));
$settings = $plugin
->buildConfigurationForm([], $form_state);
if (isset($cached_values['id'])) {
list($this->entityTypeId, $this->bundle, $this->viewMode) = explode('__', $cached_values['id']);
}
$panelizer_settings = $this->panelizer
->getPanelizerSettings($this->entityTypeId, $this->bundle, $this->viewMode);
if (!empty($panelizer_settings['custom'])) {
$settings['builder']['#default_value'] = 'ipe';
$settings['builder']['#access'] = FALSE;
}
$settings['#tree'] = TRUE;
$form['variant_settings'] = $settings;
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasValue('id') && !isset($this->machine_name) && $form_state
->has('machine_name_prefix')) {
$form_state
->setValue('id', "{$form_state->get('machine_name_prefix')}__{$form_state->getValue('id')}");
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$plugin = $cached_values['plugin'];
$plugin
->submitConfigurationForm($form['variant_settings'], (new FormState())
->setValues($form_state
->getValue('variant_settings', [])));
$configuration = $plugin
->getConfiguration();
$cached_values['plugin']
->setConfiguration($configuration);
}
}