HtmlCommand example

'name' => 'my-data',
      'value' => ['key' => 'value'],
    ];

    $this->assertEquals($expected$command->render());
  }

  /** * @covers \Drupal\Core\Ajax\HtmlCommand */
  public function testHtmlCommand() {
    $command = new HtmlCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);

    $expected = [
      'command' => 'insert',
      'method' => 'html',
      'selector' => '#page-title',
      'data' => '<p>New Text!</p>',
      'settings' => ['my-setting' => 'setting'],
    ];

    $this->assertEquals($expected$command->render());
  }

  
$selected_bundles[$bundle_id] = $bundle['label'];
      }
    }
    $selected_bundles_list = [
      '#theme' => 'item_list',
      '#items' => $selected_bundles,
      '#context' => ['list_style' => 'comma-list'],
      '#empty' => $this->t('none'),
    ];
    $response = new AjaxResponse();
    $response->addCommand(new CloseDialogCommand());
    $response->addCommand(new HtmlCommand('#selected-' . $this->entityType->id()$selected_bundles_list));
    return $response;
  }

  /** * Route title callback. */
  public function getTitle(WorkflowInterface $workflow = NULL, $entity_type_id) {
    $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);

    $title = $this->t('Select the @entity_type types for the @workflow workflow', ['@entity_type' => $this->entityType->getLabel(), '@workflow' => $workflow->label()]);
    if ($bundle_entity_type_id = $this->entityType->getBundleEntityType()) {
      

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    if ($form_state->getErrors()) {
      unset($form['#prefix']$form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response->addCommand(new HtmlCommand('#editor-link-dialog-form', $form));
    }
    else {
      $response->addCommand(new EditorDialogSave($form_state->getValues()));
      $response->addCommand(new CloseModalDialogCommand());
    }

    return $response;
  }

}
/** * Simple object for testing methods as Ajax callbacks. */
class Callbacks {

  /** * Ajax callback triggered by select. */
  public function selectCallback($form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response->addCommand(new HtmlCommand('#ajax_selected_color', $form_state->getValue('select')));
    $response->addCommand(new DataCommand('#ajax_selected_color', 'form_state_value_select', $form_state->getValue('select')));
    return $response;
  }

  /** * Ajax callback triggered by date. */
  public function dateCallback($form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $date = $form_state->getValue('date');
    $response->addCommand(new HtmlCommand('#ajax_date_value', sprintf('<div>%s</div>', $date)));
    
    // attribute.     if (!empty($form_state->get('filter_default_view_mode')) && $form_state->getValue(['attributes', 'data-view-mode']) === $form_state->get('filter_default_view_mode')) {
      $form_state->setValue(['attributes', 'data-view-mode'], FALSE);
    }

    if ($form_state->getErrors()) {
      unset($form['#prefix']$form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response->addCommand(new HtmlCommand('#editor-media-dialog-form', $form));
    }
    else {
      // Only send back the relevant values.       $values = [
        'hasCaption' => $form_state->getValue('hasCaption'),
        'attributes' => $form_state->getValue('attributes'),
      ];
      $response->addCommand(new EditorDialogSave($values));
      $response->addCommand(new CloseModalDialogCommand());
    }

    
$assets = AttachedAssets::createFromRenderArray($build);
    $css_render_array = $css_collection_renderer->render($asset_resolver->getCssAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage()));
    $expected_commands[1] = new AddCssCommand(array_column($css_render_array, '#attributes'));
    $build['#attached']['library'][] = 'ajax_test/order-header-js-command';
    $build['#attached']['library'][] = 'ajax_test/order-footer-js-command';
    $assets = AttachedAssets::createFromRenderArray($build);
    [$js_assets_header$js_assets_footer] = $asset_resolver->getJsAssets($assets, FALSE, \Drupal::languageManager()->getCurrentLanguage());
    $js_header_render_array = $js_collection_renderer->render($js_assets_header);
    $js_footer_render_array = $js_collection_renderer->render($js_assets_footer);
    $expected_commands[2] = new AddJsCommand(array_column($js_header_render_array, '#attributes'), 'head');
    $expected_commands[3] = new AddJsCommand(array_column($js_footer_render_array, '#attributes'));
    $expected_commands[4] = new HtmlCommand('body', 'Hello, world!');

    // Verify AJAX command order — this should always be the order:     // 1. CSS files     // 2. JavaScript files in the header     // 3. JavaScript files in the footer     // 4. Any other AJAX commands, in whatever order they were added.     $commands = $this->drupalGetAjax('ajax-test/order');
    $this->assertCommand(array_slice($commands, 0, 1)$expected_commands[1]->render());
    $this->assertCommand(array_slice($commands, 1, 1)$expected_commands[2]->render());
    $this->assertCommand(array_slice($commands, 2, 1)$expected_commands[3]->render());
    $this->assertCommand(array_slice($commands, 3, 1)$expected_commands[4]->render());
  }

  public function rebuildCurrentTab(ViewUI $view, AjaxResponse $response$display_id) {
    $this->displayID = $display_id;
    if (!$view->getExecutable()->setDisplay('default')) {
      return;
    }

    // Regenerate the main display area.     $build = $this->getDisplayTab($view);
    $response->addCommand(new HtmlCommand('#views-tab-' . $display_id$build));

    // Regenerate the top area so changes to display names and order will appear.     $build = $this->renderDisplayTop($view);
    $response->addCommand(new ReplaceCommand('#views-display-top', $build));
  }

  /** * Render the top of the display so it can be updated during ajax operations. */
  public function renderDisplayTop(ViewUI $view) {
    $display_id = $this->displayID;
    

  }

  /** * Returns an AjaxResponse; settings command set last. * * Helps verifying AjaxResponse reorders commands to ensure correct execution. */
  public function order() {
    $response = new AjaxResponse();
    // HTML insertion command.     $response->addCommand(new HtmlCommand('body', 'Hello, world!'));
    $build['#attached']['library'][] = 'ajax_test/order';
    $response->setAttachments($build['#attached']);

    return $response;
  }

  /** * Returns an AjaxResponse with alert command. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return \Drupal\Core\Ajax\AjaxResponse * The JSON response object. */
/** * AJAX callback. */
  public function ajaxCallback($form, FormStateInterface $form_state) {
    $item = [
      '#type' => 'item',
      '#title' => $this->t('Ajax value'),
      '#markup' => microtime(),
    ];
    $response = new AjaxResponse();
    $response->addCommand(new HtmlCommand('#ajax-value', $item));
    return $response;
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form$form_state);

    $this->configuration['test_parameter'] = $form_state->getValue('test_parameter');
  }

  
    // empty string: two double quotes signify "empty alt attribute". See above.     if (trim($form_state->getValue(['attributes', 'alt'])) === '""') {
      $form_state->setValue(['attributes', 'alt'], '');
    }

    if ($form_state->getErrors()) {
      unset($form['#prefix']$form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response->addCommand(new HtmlCommand('#editor-image-dialog-form', $form));
    }
    else {
      $response->addCommand(new EditorDialogSave($form_state->getValues()));
      $response->addCommand(new CloseModalDialogCommand());
    }

    return $response;
  }

}
Home | Imprint | This part of the site doesn't use cookies.