View source
<?php
namespace Drupal\tour_ui\Form;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tour\TipPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TourTipForm extends FormBase {
protected $pluginManager;
protected $messenger;
public function __construct(TipPluginManager $plugin_manager, MessengerInterface $messenger) {
$this->pluginManager = $plugin_manager;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.tour.tip'), $container
->get('messenger'));
}
public function getFormId() {
return 'tour_ui_tip_test_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
$tip = $storage['#tip'];
$form += $tip
->buildConfigurationForm($form, $form_state);
$actions = $this
->actionsElement($form, $form_state);
if (!empty($actions)) {
$form['actions'] = $actions;
}
return $form;
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
$element = $this
->actions($form, $form_state);
if (isset($element['delete'])) {
$delete = $element['delete'];
unset($element['delete']);
$element['delete'] = $delete;
$element['delete']['#button_type'] = 'danger';
}
if (isset($element['submit'])) {
$element['submit']['#button_type'] = 'primary';
}
$count = 0;
foreach (Element::children($element) as $action) {
$element[$action] += [
'#weight' => ++$count * 5,
];
}
if (!empty($element)) {
$element['#type'] = 'actions';
}
return $element;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => [
'::submitForm',
],
];
$actions['delete'] = [
'#type' => 'link',
'#title' => $this
->t('Delete'),
'#attributes' => [
'class' => [
'button',
'button--danger',
],
],
];
return $actions;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
$tour = $storage['#tour'];
$tips = $tour
->getTips();
if (empty($tips)) {
return;
}
$tip_ids = array_map(function ($data) {
return $data
->id();
}, $tips);
if (in_array($form_state
->getValue('id'), $tip_ids) && isset($storage['#new'])) {
$form_state
->setError($form['label'], $this
->t('A tip with the same identifier exists.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
$tour = $storage['#tour'];
$tip = $storage['#tip'];
$configuration = $tip
->getConfiguration();
$new_tip = $tip
->getConfiguration();
foreach ($configuration as $name => $configuration_value) {
$value = $form_state
->getValue($name);
$new_tip[$name] = is_array($value) ? array_filter($value) : $value;
}
$new_tip_list = $tour
->getTips();
$new_tips = [];
if (!empty($new_tip_list)) {
foreach ($new_tip_list as $tip) {
$new_tips[$tip
->id()] = $tip
->getConfiguration();
}
}
$input = $form_state
->getUserInput();
$selected = $input['attributes']['selector_type'];
$attributes =& $new_tip['attributes'];
if ($selected !== 'data-id') {
unset($attributes['data-id']);
}
if ($selected !== 'data-class') {
unset($attributes['data-class']);
}
if (!is_array($new_tip['attributes'])) {
$new_tip['attributes'] = [];
}
$new_tips[$new_tip['id']] = $new_tip;
$tour
->set('tips', $new_tips);
$tour
->save();
if (isset($storage['#new'])) {
$this->messenger
->addMessage($this
->t('The %tip tip has been created.', [
'%tip' => $new_tip['label'],
]));
}
else {
$this->messenger
->addMessage($this
->t('Updated the %tip tip.', [
'%tip' => $new_tip['label'],
]));
}
$form_state
->setRedirect('entity.tour.edit_form', [
'tour' => $tour
->id(),
]);
return $tour;
}
}