3 namespace Drupal\devel\Form;
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Drupal\Component\Serialization\Yaml;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\State\StateInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Form API form to edit a state.
16 class SystemStateEdit extends FormBase {
21 * @var \Drupal\Core\State\StateInterface
26 * Constructs a new SystemStateEdit object.
28 * @param \Drupal\Core\State\StateInterface $state
31 public function __construct(StateInterface $state) {
32 $this->state = $state;
38 public static function create(ContainerInterface $container) {
40 $container->get('state')
47 public function getFormId() {
48 return 'devel_state_system_edit_form';
54 public function buildForm(array $form, FormStateInterface $form_state, $state_name = '') {
56 $old_value = $this->state->get($state_name);
58 if (!isset($old_value)) {
59 drupal_set_message(t('State @name does not exist in the system.', array('@name' => $state_name)), 'warning');
63 // Only simple structures are allowed to be edited.
64 $disabled = !$this->checkObject($old_value);
67 drupal_set_message(t('Only simple structures are allowed to be edited. State @name contains objects.', array('@name' => $state_name)), 'warning');
70 // First we will show the user the content of the variable about to be edited.
71 $form['value'] = array(
73 '#title' => $this->t('Current value for %name', array('%name' => $state_name)),
74 '#markup' => kpr($old_value, TRUE),
79 if (!$disabled && is_array($old_value)) {
81 $old_value = Yaml::encode($old_value);
84 catch (InvalidDataTypeException $e) {
85 drupal_set_message(t('Invalid data detected for @name : %error', array('@name' => $state_name, '%error' => $e->getMessage())), 'error');
90 // Store in the form the name of the state variable
91 $form['state_name'] = array(
93 '#value' => $state_name,
95 // Set the transport format for the new value. Values:
98 $form['transport'] = array(
100 '#value' => $transport,
103 $form['new_value'] = array(
104 '#type' => 'textarea',
105 '#title' => $this->t('New value'),
106 '#default_value' => $disabled ? '' : $old_value,
107 '#disabled' => $disabled,
111 $form['actions'] = array('#type' => 'actions');
112 $form['actions']['submit'] = array(
114 '#value' => $this->t('Save'),
115 '#disabled' => $disabled,
117 $form['actions']['cancel'] = array(
119 '#title' => $this->t('Cancel'),
120 '#url' => Url::fromRoute('devel.state_system_page')
129 public function validateForm(array &$form, FormStateInterface $form_state) {
130 $values = $form_state->getValues();
132 if ($values['transport'] == 'yaml') {
133 // try to parse the new provided value
135 $parsed_value = Yaml::decode($values['new_value']);
136 $form_state->setValue('parsed_value', $parsed_value);
138 catch (InvalidDataTypeException $e) {
139 $form_state->setErrorByName('new_value', $this->t('Invalid input: %error', array('%error' => $e->getMessage())));
143 $form_state->setValue('parsed_value', $values['new_value']);
151 public function submitForm(array &$form, FormStateInterface $form_state) {
153 $values = $form_state->getValues();
154 $this->state->set($values['state_name'], $values['parsed_value']);
156 $form_state->setRedirectUrl(Url::fromRoute('devel.state_system_page'));
158 drupal_set_message($this->t('Variable %variable was successfully edited.', array('%variable' => $values['state_name'])));
159 $this->logger('devel')->info('Variable %variable was successfully edited.', array('%variable' => $values['state_name']));
163 * Helper function to determine if a variable is or contains an object.
166 * Input data to check
169 * TRUE if the variable is not an object and does not contain one.
171 protected function checkObject($data) {
172 if (is_object($data)) {
175 if (is_array($data)) {
176 // If the current object is an array, then check recursively.
177 foreach ($data as $value) {
178 // If there is an object the whole container is "contaminated"
179 if (!$this->checkObject($value)) {