View source
<?php
namespace Drupal\devel\Form;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SystemStateEdit extends FormBase {
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('state'));
}
public function getFormId() {
return 'devel_state_system_edit_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $state_name = '') {
$old_value = $this->state
->get($state_name);
if (!isset($old_value)) {
drupal_set_message(t('State @name does not exist in the system.', array(
'@name' => $state_name,
)), 'warning');
return;
}
$disabled = !$this
->checkObject($old_value);
if ($disabled) {
drupal_set_message(t('Only simple structures are allowed to be edited. State @name contains objects.', array(
'@name' => $state_name,
)), 'warning');
}
$form['value'] = array(
'#type' => 'item',
'#title' => $this
->t('Current value for %name', array(
'%name' => $state_name,
)),
'#markup' => kpr($old_value, TRUE),
);
$transport = 'plain';
if (!$disabled && is_array($old_value)) {
try {
$old_value = Yaml::encode($old_value);
$transport = 'yaml';
} catch (InvalidDataTypeException $e) {
drupal_set_message(t('Invalid data detected for @name : %error', array(
'@name' => $state_name,
'%error' => $e
->getMessage(),
)), 'error');
return;
}
}
$form['state_name'] = array(
'#type' => 'value',
'#value' => $state_name,
);
$form['transport'] = array(
'#type' => 'value',
'#value' => $transport,
);
$form['new_value'] = array(
'#type' => 'textarea',
'#title' => $this
->t('New value'),
'#default_value' => $disabled ? '' : $old_value,
'#disabled' => $disabled,
'#rows' => 15,
);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#disabled' => $disabled,
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => Url::fromRoute('devel.state_system_page'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if ($values['transport'] == 'yaml') {
try {
$parsed_value = Yaml::decode($values['new_value']);
$form_state
->setValue('parsed_value', $parsed_value);
} catch (InvalidDataTypeException $e) {
$form_state
->setErrorByName('new_value', $this
->t('Invalid input: %error', array(
'%error' => $e
->getMessage(),
)));
}
}
else {
$form_state
->setValue('parsed_value', $values['new_value']);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this->state
->set($values['state_name'], $values['parsed_value']);
$form_state
->setRedirectUrl(Url::fromRoute('devel.state_system_page'));
drupal_set_message($this
->t('Variable %variable was successfully edited.', array(
'%variable' => $values['state_name'],
)));
$this
->logger('devel')
->info('Variable %variable was successfully edited.', array(
'%variable' => $values['state_name'],
));
}
protected function checkObject($data) {
if (is_object($data)) {
return FALSE;
}
if (is_array($data)) {
foreach ($data as $value) {
if (!$this
->checkObject($value)) {
return FALSE;
}
}
}
return TRUE;
}
}