DnaForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Drupal\devel_node_access\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\devel_node_access\Plugin\Block\DnaBlock;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. /**
  8. * Defines the form to change the DNA settings.
  9. */
  10. class DnaForm extends FormBase {
  11. /**
  12. * Constructs a new DnaForm object.
  13. */
  14. public function __construct() {
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function create(ContainerInterface $container) {
  20. return new static();
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function getFormId() {
  26. return 'devel_dna_form';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function buildForm(array $form, FormStateInterface $form_state) {
  32. $form['#attached'] = array(
  33. 'library' => array(
  34. 'devel_node_access/devel_node_access'
  35. ),
  36. );
  37. $debug_mode = \Drupal::config('devel_node_access.settings')->get('debug_mode');
  38. $by_user_mode = \Drupal::config('devel_node_access.settings')->get('by_user_mode');
  39. $form['node_content'] = array(
  40. '#prefix' => '<div id="devel-node-access-node-content-div">',
  41. '#suffix' => '</div>',
  42. );
  43. $form['node_content'][0] = DnaBlock::buildNodeInfo($debug_mode);
  44. $form['user_content'] = array(
  45. '#prefix' => '<div id="devel-node-access-by-user-content-div">',
  46. '#suffix' => '</div>',
  47. );
  48. if ($by_user_mode) {
  49. $form['user_content'][0] = DnaBlock::buildByUserInfo();
  50. }
  51. $form['setup'] = array(
  52. '#markup' => t('Enable:'),
  53. '#prefix' => '<div class="devel-node-access-inline">',
  54. '#suffix' => '</div>',
  55. );
  56. $form['setup']['debug_mode'] = array(
  57. '#type' => 'checkbox',
  58. '#value' => $debug_mode,
  59. '#prefix' => ' &nbsp; &nbsp; ',
  60. '#title' => t('Debug Mode'),
  61. '#ajax' => array(
  62. 'callback' => '::toggleDebugMode',
  63. 'wrapper' => 'devel-node-access-node-content-div',
  64. ),
  65. '#disabled' => TRUE,
  66. );
  67. $form['setup']['by_user_mode'] = array(
  68. '#type' => 'checkbox',
  69. '#value' => $by_user_mode,
  70. '#prefix' => ' &nbsp; &nbsp; ',
  71. '#title' => t('By-User Analysis (slow!)'),
  72. '#ajax' => array(
  73. 'callback' => '::toggleByUserMode',
  74. 'wrapper' => 'devel-node-access-by-user-content-div',
  75. ),
  76. '#disabled' => TRUE,
  77. );
  78. return $form;
  79. }
  80. /**
  81. * AJAX handler for form_state.
  82. *
  83. * @param array $form
  84. * An associative array containing the structure of the form.
  85. * @param \Drupal\Core\Form\FormStateInterface $form_state
  86. * The current state of the form.
  87. *
  88. * @return array
  89. */
  90. public function toggleDebugMode($form, FormStateInterface $form_state) {
  91. $debug_mode = $form_state->getUserInput();
  92. $debug_mode = !empty($debug_mode['debug_mode']);
  93. \Drupal::configFactory()->getEditable('devel_node_access.settings')->set('debug_mode', $debug_mode)->save(TRUE);
  94. $form['node_content'][0] = DnaBlock::buildNodeInfo($debug_mode);
  95. return $form['node_content'];
  96. }
  97. /**
  98. * AJAX handler for by_user_state.
  99. *
  100. * @param array $form
  101. * An associative array containing the structure of the form.
  102. * @param \Drupal\Core\Form\FormStateInterface $form_state
  103. * The current state of the form.
  104. *
  105. * @return array
  106. */
  107. public function toggleByUserMode($form, FormStateInterface $form_state) {
  108. $by_user_mode = $form_state->getUserInput();
  109. $by_user_mode = !empty($by_user_mode['by_user_mode']);
  110. \Drupal::configFactory()->getEditable('devel_node_access.settings')->set('by_user_mode', $by_user_mode)->save(TRUE);
  111. $form['user_content'][0] = ($by_user_mode ? DnaBlock::buildByUserInfo() : []);
  112. return $form['user_content'];
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function submitForm(array &$form, FormStateInterface $form_state) {
  118. }
  119. }