View source
<?php
namespace Drupal\yamlform_ui\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\yamlform\Utility\YamlFormDialogHelper;
use Drupal\yamlform\YamlFormDialogTrait;
use Drupal\yamlform\YamlFormElementManagerInterface;
use Drupal\yamlform\YamlFormEntityElementsValidator;
use Drupal\yamlform\YamlFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class YamlFormUiElementFormBase extends FormBase implements YamlFormUiElementFormInterface {
use YamlFormDialogTrait;
protected $renderer;
protected $elementManager;
protected $elementsValidator;
protected $yamlform;
protected $element = [];
protected $originalType;
protected $action;
public function getFormId() {
return 'yamlform_ui_element_form';
}
public function __construct(RendererInterface $renderer, YamlFormElementManagerInterface $element_manager, YamlFormEntityElementsValidator $elements_validator) {
$this->renderer = $renderer;
$this->elementManager = $element_manager;
$this->elementsValidator = $elements_validator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('plugin.manager.yamlform.element'), $container
->get('yamlform.elements_validator'));
}
public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $key = NULL, $parent_key = '') {
$this->yamlform = $yamlform;
$yamlform_element = $this
->getYamlFormElement();
$form['properties'] = $yamlform_element
->buildConfigurationForm([], $form_state);
if (isset($form['properties']['messages'])) {
$form['messages'] = $form['properties']['messages'];
$form['messages']['#weight'] = -100;
unset($form['properties']['messages']);
}
$form['parent_key'] = [
'#type' => 'value',
'#value' => $parent_key,
];
$form['properties']['element']['type'] = [
'#type' => 'item',
'#title' => $this
->t('Type'),
'label' => [
'#markup' => $yamlform_element
->getPluginLabel(),
],
'#weight' => -100,
'#parents' => [
'type',
],
];
if ($key && $yamlform_element
->getRelatedTypes($this->element)) {
$route_parameters = [
'yamlform' => $yamlform
->id(),
'key' => $key,
];
if ($this->originalType) {
$original_yamlform_element = $this->elementManager
->createInstance($this->originalType);
$route_parameters = [
'yamlform' => $yamlform
->id(),
'key' => $key,
];
$form['properties']['element']['type']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => new Url('entity.yamlform_ui.element.edit_form', $route_parameters),
'#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
'button',
'button--small',
]),
];
$form['properties']['element']['type']['#description'] = '(' . $this
->t('Changing from %type', [
'%type' => $original_yamlform_element
->getPluginLabel(),
]) . ')';
}
else {
$form['properties']['element']['type']['change_type'] = [
'#type' => 'link',
'#title' => $this
->t('Change'),
'#url' => new Url('entity.yamlform_ui.change_element', $route_parameters),
'#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
'button',
'button--small',
]),
];
}
}
if (!$key) {
$reserved_keys = [
'form_build_id',
'form_token',
'form_id',
'data',
'op',
];
$reserved_keys = array_merge($reserved_keys, array_keys(\Drupal::service('entity_field.manager')
->getBaseFieldDefinitions('yamlform_submission')));
$form['#attached']['drupalSettings']['yamlform_ui']['reserved_keys'] = $reserved_keys;
$form['#attached']['library'][] = 'yamlform_ui/yamlform_ui.element';
$form['properties']['element']['key_warning'] = [
'#type' => 'yamlform_message',
'#message_type' => 'warning',
'#message_message' => $this
->t("Please avoid using the reserved word '@key' as the element's key."),
'#weight' => -99,
'#attributes' => [
'style' => 'display:none',
],
];
}
$form['properties']['element']['key'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Key'),
'#machine_name' => [
'label' => $this
->t('Key'),
'exists' => [
$this,
'exists',
],
'source' => [
'title',
],
],
'#required' => TRUE,
'#parents' => [
'key',
],
'#disabled' => $key ? TRUE : FALSE,
'#default_value' => $key,
'#weight' => -98,
];
if ($key) {
$form['properties']['element']['key']['#description'] = NULL;
}
if (isset($form['properties']['element']['title'])) {
$form['properties']['element']['key']['#machine_name']['source'] = [
'properties',
'element',
'title',
];
$form['properties']['element']['title']['#id'] = 'title';
}
if (isset($form['properties']['flex']) && !$this
->isParentElementFlexbox($key, $parent_key)) {
$form['properties']['flex']['#access'] = FALSE;
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
'#_validate_form' => TRUE,
];
$form = $this
->buildDialog($form, $form_state);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
if (empty($button['#_validate_form'])) {
return;
}
$element_form_state = clone $form_state;
$element_form_state
->setValues($form_state
->getValue('properties'));
$yamlform_element = $this
->getYamlFormElement();
$yamlform_element
->validateConfigurationForm($form, $element_form_state);
$element_errors = $element_form_state
->getErrors();
foreach ($element_errors as $element_error) {
$form_state
->setErrorByName(NULL, $element_error);
}
if ($form_state
->hasAnyErrors()) {
return;
}
$properties = $yamlform_element
->getConfigurationFormProperties($form, $element_form_state);
$parent_key = $form_state
->getValue('parent_key');
$key = $form_state
->getValue('key');
if ($key) {
$this->yamlform
->setElementProperties($key, $properties, $parent_key);
if ($messages = $this->elementsValidator
->validate($this->yamlform)) {
$t_args = [
':href' => Url::fromRoute('entity.yamlform.source_form', [
'yamlform' => $this->yamlform
->id(),
])
->toString(),
];
$form_state
->setErrorByName('elements', $this
->t('There has been error validating the elements. You may need to edit the <a href=":href">YAML source</a> to resolve the issue.', $t_args));
foreach ($messages as $message) {
drupal_set_message($message, 'error');
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$yamlform_element = $this
->getYamlFormElement();
if ($response = $this
->validateDialog($form, $form_state)) {
return $response;
}
$element_form_state = clone $form_state;
$element_form_state
->setValues($form_state
->getValue('properties'));
$yamlform_element
->submitConfigurationForm($form, $element_form_state);
$this->yamlform
->save();
$properties = $form_state
->getValue('properties');
$t_args = [
'%title' => !empty($properties['title']) ? $properties['title'] : $form_state
->getValue('key'),
'@action' => $this->action,
];
drupal_set_message($this
->t('%title has been @action.', $t_args));
return $this
->redirectForm($form, $form_state, $this->yamlform
->toUrl('edit-form'));
}
public function exists($key) {
$elements = $this->yamlform
->getElementsInitializedAndFlattened();
return isset($elements[$key]) ? TRUE : FALSE;
}
public function isNew() {
return $this instanceof YamlFormUiElementAddForm ? TRUE : FALSE;
}
public function getYamlForm() {
return $this->yamlform;
}
public function getYamlFormElement() {
return $this->elementManager
->getElementInstance($this->element);
}
protected function isParentElementFlexbox($key = NULL, $parent_key = NULL) {
$elements = $this->yamlform
->getElementsInitializedAndFlattened();
if ($key && isset($elements[$key])) {
return $elements[$key]['#yamlform_parent_flexbox'];
}
if ($parent_key && isset($elements[$parent_key]) && isset($elements[$parent_key]['#type'])) {
return $elements[$parent_key]['#type'] == 'yamlform_flexbox' ? TRUE : FALSE;
}
return FALSE;
}
}