naala

my module ajax custom template

Nov 23rd, 2017
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. my_module.links.menu.yml
  2.  
  3. my_module.main:
  4.   route_name: my_module.main
  5.   title: My Module
  6.   parent: system.admin
  7.   weight: -6
  8. my_module.form_page:
  9.   route_name: my_module.form_page
  10.   title: My Module Form
  11.   parent: my_module.main
  12.   weight: -6
  13.  
  14. my_module.routing.yml
  15.  
  16. my_module.main:
  17.   path: '/admin/my_module'
  18.   defaults:
  19.     _controller: 'Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
  20.     _title: 'My Module'
  21.   requirements:
  22.     _permission: 'administrator'
  23. my_module.form_page:
  24.   path: '/admin/my_module/form'
  25.   defaults:
  26.     _form: 'Drupal\my_module\Form\MyModuleForm'
  27.     _title: 'My Module Form'
  28.   requirements:
  29.     _permission: 'administrator'
  30.  
  31. my_module.module
  32.  
  33. <?php
  34. /**
  35.  * Implements hook_theme_registry_alter
  36. */
  37. function my_module_theme($existing, $type, $theme, $path) {
  38.   return [
  39.     'my_module_form' => [
  40.       'render element' => 'form',
  41.     ],
  42.   ];
  43. }
  44.  
  45. templates/my-module-form.html.twig
  46.  
  47. <form {{ attributes }}>
  48.   <div class="layout-column layout-column--half">
  49.     {{ form.user_view }}
  50.     {{ form.submit }}
  51.   </div>
  52.   <div class="layout-column layout-column--half">
  53.     {{ form.user_list_wrapper }}
  54.   </div>
  55. </form>
  56.  
  57.  
  58. src/Form/MyModuleForm.php
  59.  
  60. <?php
  61. /**
  62.  * @file
  63.  * Contains \Drupal\my_module\Form\MyModuleForm.
  64.  */
  65. namespace Drupal\my_module\Form;
  66.  
  67. use Drupal\Core\Ajax\AjaxResponse;
  68. use Drupal\Core\Ajax\HtmlCommand;
  69. use Drupal\Core\Form\FormBase;
  70. use Drupal\Core\Form\FormStateInterface;
  71. use Drupal\views\Views;
  72.  
  73. /**
  74.  * Configure custom_rest settings for this site.
  75.  */
  76. class MyModuleForm extends FormBase {
  77.  
  78.   /**
  79.    * {@inheritdoc}
  80.    */
  81.   public function getFormId() {
  82.     return 'my_module_form';
  83.   }
  84.  
  85.   /**
  86.    * {@inheritdoc}
  87.    */
  88.   public function buildForm(array $form, FormStateInterface $form_state) {
  89.     ...
  90.  
  91.     $form['#theme'] = 'my_module_form';
  92.  
  93.     $form['user_view'] = [
  94.       '#type' => 'select',
  95.       '#title' => $this->t('Select element'),
  96.       '#options' => $userViews,
  97.       '#ajax' => [
  98.         'callback' => '::findUsers',
  99.         'event' => 'change',
  100.         'wrapper' => 'edit-user-list',
  101.         'progress' => array(
  102.           'type' => 'throbber',
  103.           'message' => t('Searching Users...'),
  104.         ),
  105.       ],
  106.     ];
  107.     $form['user_list_wrapper'] = [
  108.       '#type' => 'container',
  109.       '#attributes' => array(
  110.         'class' => array(
  111.           'user-list-wrapper',
  112.         ),
  113.       ),
  114.     ];
  115.     $form['user_list_wrapper']['user_list'] = [
  116.       '#type' => 'item',
  117.       '#attributes' => [
  118.         'id' => ['user_list'],
  119.       ],
  120.       '#markup' => '<ul><li><a href="#">None</a></li></ul>'
  121.     ];
  122.     $form['submit'] = [
  123.       '#type' => 'submit',
  124.       '#value' => t('Submit'),
  125.     ];
  126.  
  127.     return $form;
  128.   }
  129.  
  130.   /**
  131.  * Ajax callback to list users.
  132.  */
  133.   public function findUsers(array &$form, FormStateInterface $form_state) {
  134.     // Create the user list HTML
  135.     $selected = $form_state->getValue('user_view');
  136.     ...
  137.     $user_list = '';
  138.     ...
  139.     if (strlen($user_list) == 0) {
  140.       $user_list = 'None';
  141.     } else {
  142.       $user_list = "<ul>$user_list</ul>";
  143.     }
  144.  
  145.     // Generate the AJAX response
  146.     $ajax_response = new AjaxResponse();
  147.     $ajax_response->addCommand(new HtmlCommand('#edit-user-list', $user_list));
  148.     return $ajax_response;
  149.   }
  150.  
  151.   public function submitForm(array &$form, FormStateInterface $form_state) {
  152.     drupal_set_message('Nothing Submitted. Just an Example.');
  153.   }
  154. }
Add Comment
Please, Sign In to add comment