PageRenderTime 67ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Form/SystemStateEdit.php

https://gitlab.com/Drulenium-bot/devel
PHP | 189 lines | 112 code | 27 blank | 50 comment | 10 complexity | 58600a7139956b801ae2e16df578bf35 MD5 | raw file
  1. <?php
  2. namespace Drupal\devel\Form;
  3. use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
  4. use Drupal\Component\Serialization\Yaml;
  5. use Drupal\Core\Form\FormBase;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\State\StateInterface;
  8. use Drupal\Core\Url;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Form API form to edit a state.
  12. */
  13. class SystemStateEdit extends FormBase {
  14. /**
  15. * The state store.
  16. *
  17. * @var \Drupal\Core\State\StateInterface
  18. */
  19. protected $state;
  20. /**
  21. * Constructs a new SystemStateEdit object.
  22. *
  23. * @param \Drupal\Core\State\StateInterface $state
  24. * The state service.
  25. */
  26. public function __construct(StateInterface $state) {
  27. $this->state = $state;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function create(ContainerInterface $container) {
  33. return new static(
  34. $container->get('state')
  35. );
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getFormId() {
  41. return 'devel_state_system_edit_form';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function buildForm(array $form, FormStateInterface $form_state, $state_name = '') {
  47. // Get the old value
  48. $old_value = $this->state->get($state_name);
  49. if (!isset($old_value)) {
  50. drupal_set_message(t('State @name does not exist in the system.', array('@name' => $state_name)), 'warning');
  51. return;
  52. }
  53. // Only simple structures are allowed to be edited.
  54. $disabled = !$this->checkObject($old_value);
  55. if ($disabled) {
  56. drupal_set_message(t('Only simple structures are allowed to be edited. State @name contains objects.', array('@name' => $state_name)), 'warning');
  57. }
  58. // First we will show the user the content of the variable about to be edited.
  59. $form['value'] = array(
  60. '#type' => 'item',
  61. '#title' => $this->t('Current value for %name', array('%name' => $state_name)),
  62. '#markup' => kprint_r($old_value, TRUE),
  63. );
  64. $transport = 'plain';
  65. if (!$disabled && is_array($old_value)) {
  66. try {
  67. $old_value = Yaml::encode($old_value);
  68. $transport = 'yaml';
  69. }
  70. catch (InvalidDataTypeException $e) {
  71. drupal_set_message(t('Invalid data detected for @name : %error', array('@name' => $state_name, '%error' => $e->getMessage())), 'error');
  72. return;
  73. }
  74. }
  75. // Store in the form the name of the state variable
  76. $form['state_name'] = array(
  77. '#type' => 'value',
  78. '#value' => $state_name,
  79. );
  80. // Set the transport format for the new value. Values:
  81. // - plain
  82. // - yaml
  83. $form['transport'] = array(
  84. '#type' => 'value',
  85. '#value' => $transport,
  86. );
  87. $form['new_value'] = array(
  88. '#type' => 'textarea',
  89. '#title' => $this->t('New value'),
  90. '#default_value' => $disabled ? '' : $old_value,
  91. '#disabled' => $disabled,
  92. '#rows' => 15,
  93. );
  94. $form['actions'] = array('#type' => 'actions');
  95. $form['actions']['submit'] = array(
  96. '#type' => 'submit',
  97. '#value' => $this->t('Save'),
  98. '#disabled' => $disabled,
  99. );
  100. $form['actions']['cancel'] = array(
  101. '#type' => 'link',
  102. '#title' => $this->t('Cancel'),
  103. '#url' => Url::fromRoute('devel.state_system_page')
  104. );
  105. return $form;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function validateForm(array &$form, FormStateInterface $form_state) {
  111. $values = $form_state->getValues();
  112. if ($values['transport'] == 'yaml') {
  113. // try to parse the new provided value
  114. try {
  115. $parsed_value = Yaml::decode($values['new_value']);
  116. $form_state->setValue('parsed_value', $parsed_value);
  117. }
  118. catch (InvalidDataTypeException $e) {
  119. $form_state->setErrorByName('new_value', $this->t('Invalid input: %error', array('%error' => $e->getMessage())));
  120. }
  121. }
  122. else {
  123. $form_state->setValue('parsed_value', $values['new_value']);
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function submitForm(array &$form, FormStateInterface $form_state) {
  130. // Save the state
  131. $values = $form_state->getValues();
  132. $this->state->set($values['state_name'], $values['parsed_value']);
  133. $form_state->setRedirectUrl(Url::fromRoute('devel.state_system_page'));
  134. drupal_set_message($this->t('Variable %variable was successfully edited.', array('%variable' => $values['state_name'])));
  135. $this->logger('devel')->info('Variable %variable was successfully edited.', array('%variable' => $values['state_name']));
  136. }
  137. /**
  138. * Helper function to determine if a variable is or contains an object.
  139. *
  140. * @param $data
  141. * Input data to check
  142. *
  143. * @return bool
  144. * TRUE if the variable is not an object and does not contain one.
  145. */
  146. protected function checkObject($data) {
  147. if (is_object($data)) {
  148. return FALSE;
  149. }
  150. if (is_array($data)) {
  151. // If the current object is an array, then check recursively.
  152. foreach ($data as $value) {
  153. // If there is an object the whole container is "contaminated"
  154. if (!$this->checkObject($value)) {
  155. return FALSE;
  156. }
  157. }
  158. }
  159. // All checks pass
  160. return TRUE;
  161. }
  162. }