SubmitButtonLabel.php in Flexiform 8
File
src/Plugin/FormEnhancer/SubmitButtonLabel.php
View source
<?php
namespace Drupal\flexiform\Plugin\FormEnhancer;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\flexiform\FormEnhancer\ConfigurableFormEnhancerBase;
use Drupal\flexiform\FormEnhancer\SubmitButtonFormEnhancerTrait;
class SubmitButtonLabel extends ConfigurableFormEnhancerBase {
use SubmitButtonFormEnhancerTrait;
use StringTranslationTrait;
protected $supportedEvents = [
'process_form',
];
public function configurationForm(array $form, FormStateInterface $form_state) {
foreach ($this
->locateSubmitButtons() as $path => $label) {
$original_path = $path;
$path = str_replace('][', '::', $path);
$form['label'][$path] = [
'#type' => 'textfield',
'#title' => $this
->t('@label Button Text', [
'@label' => $label,
]),
'#description' => 'Array Parents: ' . $original_path,
'#default_value' => !empty($this->configuration[$path]) ? $this->configuration[$path] : '',
];
}
return $form;
}
public function configurationFormSubmit(array $form, FormStateInterface $form_state) {
$this->configuration = $form_state
->getValue($form['#parents']);
}
public function processForm($element, FormStateInterface $form_state, $form) {
foreach (array_filter($this->configuration) as $key => $label) {
$array_parents = explode('::', $key);
$button = [];
$button = NestedArray::getValue($element, $array_parents, $exists);
if ($exists) {
$button['#value'] = $label;
NestedArray::setValue($element, $array_parents, $button);
}
}
return $element;
}
}