Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @file
  5. * Contains \Drupal\modal\Form\TestModalForm.
  6. */
  7.  
  8. namespace Drupal\modal\Form;
  9.  
  10. use Drupal\Core\Form\FormBase;
  11. use Drupal\Core\Form\FormStateInterface;
  12. use Drupal\Core\Ajax\AjaxResponse;
  13. use Drupal\Core\Ajax;
  14. use Drupal\Core\Ajax\OpenModalDialogCommand;
  15. use Drupal\Core\Ajax\CssCommand;
  16.  
  17. /**
  18. * Class TestModalForm.
  19. *
  20. * @package Drupal\modal\Form
  21. */
  22. class TestModalForm extends FormBase {
  23.  
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getFormId() {
  28. return 'test_modal_form';
  29. }
  30.  
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function buildForm(array $form, FormStateInterface $form_state) {
  35. $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  36. $form['node_title'] = array(
  37. '#type' => 'textfield',
  38. '#title' => $this->t('Node`s title'),
  39. );
  40. $form['actions']['#type'] = 'actions';
  41. $form['actions']['submit'] = array(
  42. '#type' => 'submit',
  43. '#value' => $this->t('Load'),
  44. '#ajax' => array(
  45. 'callback' => '::open_modal',
  46. ),
  47. );
  48.  
  49. $form['#title'] = 'Load node ID';
  50. return $form;
  51. }
  52.  
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function submitForm(array &$form, FormStateInterface $form_state) {
  57.  
  58. }
  59.  
  60. public function open_modal(&$form, FormStateInterface $form_state) {
  61. $node_title = $form_state->getValue('node_title');
  62. $query = \Drupal::entityQuery('node')
  63. ->condition('title', $node_title);
  64. $entity = $query->execute();
  65. $title = 'Node ID';
  66. $key = array_keys($entity);
  67. $id = !empty($key[0]) ? $key[0] : NULL;
  68. $response = new AjaxResponse();
  69. if ($id !== NULL) {
  70. $content = '<div class="test-popup-content"> Node ID is: ' . $id . '</div>';
  71. $options = array(
  72. 'dialogClass' => 'popup-dialog-class',
  73. 'width' => '300',
  74. 'height' => '300',
  75. );
  76. $response->addCommand(new OpenModalDialogCommand($title, $content, $options));
  77.  
  78. }
  79. else {
  80. $content = 'No record found with this title <strong>' . $node_title .'</strong>';
  81. $response->addCommand(new OpenModalDialogCommand($title, $content));
  82. }
  83. return $response;
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement