本文整理汇总了PHP中Drupal\Core\Ajax\AjaxResponse::addCommand方法的典型用法代码示例。如果您正苦于以下问题:PHP AjaxResponse::addCommand方法的具体用法?PHP AjaxResponse::addCommand怎么用?PHP AjaxResponse::addCommand使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Ajax\AjaxResponse
的用法示例。
在下文中一共展示了AjaxResponse::addCommand方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: ajaxOperation
/**
* Calls a method on an entity queue and reloads the listing page.
*
* @param \Drupal\entityqueue\EntityQueueInterface $entity_queue
* The view being acted upon.
* @param string $op
* The operation to perform, e.g., 'enable' or 'disable'.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return \Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse
* Either returns a rebuilt listing page as an AJAX response, or redirects
* back to the listing page.
*/
public function ajaxOperation(EntityQueueInterface $entity_queue, $op, Request $request) {
// Perform the operation.
$entity_queue->$op()->save();
// If the request is via AJAX, return the rendered list as JSON.
if ($request->request->get('js')) {
$list = $this->entityManager()->getListBuilder('entity_queue')->render();
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#entity-queue-list', $list));
return $response;
}
// Otherwise, redirect back to the page.
return $this->redirect('entity.entity_queue.collection');
}
示例2: submitEmailAjax
/**
* Ajax callback to validate the email field.
*/
public function submitEmailAjax(array &$form, FormStateInterface $form_state)
{
$valid = $this->validateEmail($form, $form_state);
$response = new AjaxResponse();
if ($valid) {
$css = ['border' => '1px solid green'];
$message = $this->t('Email ok.');
} else {
$css = ['border' => '1px solid red'];
$message = $this->t('Email not valid.');
}
// $response->addCommand(new CssCommand('#edit-email', $css));.
$response->addCommand(new OpenModalDialogCommand('Alert', 'hello', array('width' => '700')));
return $response;
}
示例3: ajaxSample
/**
* Ajax callback to render a sample of the input date format.
*
* @param array $form
* Form API array structure.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state information.
*
* @return AjaxResponse
* Ajax response with the rendered sample date using the given format. If
* the given format cannot be identified or was empty, the response will
* be empty as well.
*/
public static function ajaxSample(array $form, FormStateInterface $form_state)
{
$response = new AjaxResponse();
$format_value = NestedArray::getValue($form_state->getValues(), $form_state->getTriggeringElement()['#array_parents']);
if (!empty($format_value)) {
// Format the date with a custom date format with the given pattern.
// The object is not instantiated in an Ajax context, so $this->t()
// cannot be used here.
$format = t('Displayed as %date_format', array('%date_format' => \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', $format_value)));
// Return a command instead of a string, since the Ajax framework
// automatically prepends an additional empty DIV element for a string,
// which breaks the layout.
$response->addCommand(new ReplaceCommand('#edit-date-format-suffix', '<small id="edit-date-format-suffix">' . $format . '</small>'));
}
return $response;
}
示例4: checkboxCallback
/**
* Ajax callback triggered by checkbox.
*/
function checkboxCallback($form, FormStateInterface $form_state)
{
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state->getValue('checkbox')));
$response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
return $response;
}
示例5: checkboxCallback
/**
* Ajax callback triggered by checkbox.
*/
function checkboxCallback($form, $form_state)
{
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state['values']['checkbox']));
$response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']));
return $response;
}
示例6: renderResponse
/**
* {@inheritdoc}
*/
public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match)
{
$response = new AjaxResponse();
if (isset($main_content['#type']) && $main_content['#type'] == 'ajax') {
// Complex Ajax callbacks can return a result that contains an error
// message or a specific set of commands to send to the browser.
$main_content += $this->elementInfoManager->getInfo('ajax');
$error = $main_content['#error'];
if (!empty($error)) {
// Fall back to some default message otherwise use the specific one.
if (!is_string($error)) {
$error = 'An error occurred while handling the request: The server received invalid input.';
}
$response->addCommand(new AlertCommand($error));
}
}
$html = $this->drupalRenderRoot($main_content);
$response->setAttachments($main_content['#attached']);
// The selector for the insert command is NULL as the new content will
// replace the element making the Ajax call. The default 'replaceWith'
// behavior can be changed with #ajax['method'].
$response->addCommand(new InsertCommand(NULL, $html));
$status_messages = array('#type' => 'status_messages');
$output = $this->drupalRenderRoot($status_messages);
if (!empty($output)) {
$response->addCommand(new PrependCommand(NULL, $output));
}
return $response;
}
示例7: testCommands
/**
* Tests the add and getCommands method.
*
* @see \Drupal\Core\Ajax\AjaxResponse::addCommand()
* @see \Drupal\Core\Ajax\AjaxResponse::getCommands()
*/
public function testCommands()
{
$command_one = $this->getMock('Drupal\\Core\\Ajax\\CommandInterface');
$command_one->expects($this->once())->method('render')->will($this->returnValue(array('command' => 'one')));
$command_two = $this->getMock('Drupal\\Core\\Ajax\\CommandInterface');
$command_two->expects($this->once())->method('render')->will($this->returnValue(array('command' => 'two')));
$command_three = $this->getMock('Drupal\\Core\\Ajax\\CommandInterface');
$command_three->expects($this->once())->method('render')->will($this->returnValue(array('command' => 'three')));
$this->ajaxResponse->addCommand($command_one);
$this->ajaxResponse->addCommand($command_two);
$this->ajaxResponse->addCommand($command_three, TRUE);
// Ensure that the added commands are in the right order.
$commands =& $this->ajaxResponse->getCommands();
$this->assertSame($commands[1], array('command' => 'one'));
$this->assertSame($commands[2], array('command' => 'two'));
$this->assertSame($commands[0], array('command' => 'three'));
// Remove one and change one element from commands and ensure the reference
// worked as expected.
unset($commands[2]);
$commands[0]['class'] = 'test-class';
$commands = $this->ajaxResponse->getCommands();
$this->assertSame($commands[1], array('command' => 'one'));
$this->assertFalse(isset($commands[2]));
$this->assertSame($commands[0], array('command' => 'three', 'class' => 'test-class'));
}
示例8: ajaxSubmit
public function ajaxSubmit(array &$form, FormStateInterface $form_state)
{
//---------------------------------------------------------------
// get the own attributes values of the swap
//---------------------------------------------------------------
//get all the swaps plugins
$manager = \Drupal::service('plugin.manager.swaps');
$swaps = $manager->getDefinitions();
$swap = $swaps['column'];
$input = $form_state->getUserInput();
$settings = array();
$settings['size'] = $input['swaps_column_size'];
$settings['number'] = $input['swaps_column_number'];
//---------------------------------------------------------------
// get the default attributes values of the swap (required for visual help)
//---------------------------------------------------------------
$settings['swapId'] = $swap['id'];
$settings['swapName'] = $swap['name'];
$settings['container'] = $swap['container'];
SwapDefaultAttributes::getDefaultFormElementsValues($settings, $input);
//---------------------------------------------------------------
// create the ajax response
//---------------------------------------------------------------
$visualSettings = array('visualContentLayout' => array('attributes' => $settings));
$response = new AjaxResponse();
$response->addCommand(new CloseModalDialogCommand());
$response->addCommand(new SettingsCommand($visualSettings, FALSE));
return $response;
}
示例9: validateEmailAjax
public function validateEmailAjax(array &$form, FormStateInterface $form_state)
{
$httpClient = \Drupal::httpClient();
$configuration = $this->config('gestiondenuncias.configuration');
$pwd = $configuration->get('contrasena_verifyemail');
$usr = $configuration->get('nombre_ususario_verifyemail');
$email = $form_state->getValues('denunciante')['email'];
$serverResponse = json_decode($httpClient->request('POST', "http://api.verify-email.org/api.php?usr={$usr}&pwd={$pwd}&check={$email}")->getBody()->getContents());
$response = new AjaxResponse();
if ($serverResponse->authentication_status != 1) {
\Drupal::logger('gestiondenuncias.verify-email')->error('Los parametros de conexion a verify-email son incorrectos');
} else {
if ($serverResponse->limit_status) {
\Drupal::logger('gestiondenuncias.verify-email')->error('Se llego al limite de consultas de verify-email');
} else {
if ($serverResponse->verify_status) {
$css = ['border' => '1px solid green'];
$message = $this->t('Email válido');
} else {
$css = ['border' => '1px solid red'];
$message = $this->t('Email parece ser inválido');
}
$message = $message . $form_state->getValues()['denunciante']['email'];
$response->addCommand(new CssCommand('#edit-email', $css));
$response->addCommand(new HtmlCommand('.email-valid-message', $message));
}
}
return $response;
}
示例10: ajaxFormCallback
public function ajaxFormCallback(array &$form, FormStateInterface $form_state)
{
dd('callback');
//dd(array_keys($form['ajax_wrapper']));
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#ajax_wrapper', $form['ajax_wrapper']));
$status_messages = ['#type' => 'status_messages'];
$response->addCommand(new HtmlCommand('.highlighted aside .region', $status_messages));
return $response;
}
示例11: submitForm
/**
* {@inheritdoc}
*/
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;
}
示例12: upload
/**
* Processes AJAX file uploads and deletions.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An AjaxResponse object.
*/
public function upload(Request $request)
{
$form_parents = explode('/', $request->query->get('element_parents'));
$form_build_id = $request->query->get('form_build_id');
$request_form_build_id = $request->request->get('form_build_id');
if (empty($request_form_build_id) || $form_build_id !== $request_form_build_id) {
// Invalid request.
drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', array('@size' => format_size(file_upload_max_size()))), 'error');
$response = new AjaxResponse();
$status_messages = array('#theme' => 'status_messages');
return $response->addCommand(new ReplaceCommand(NULL, drupal_render($status_messages)));
}
try {
/** @var $ajaxForm \Drupal\system\FileAjaxForm */
$ajaxForm = $this->getForm($request);
$form = $ajaxForm->getForm();
$form_state = $ajaxForm->getFormState();
$commands = $ajaxForm->getCommands();
} catch (HttpExceptionInterface $e) {
// Invalid form_build_id.
drupal_set_message(t('An unrecoverable error occurred. Use of this form has expired. Try reloading the page and submitting again.'), 'error');
$response = new AjaxResponse();
$status_messages = array('#theme' => 'status_messages');
return $response->addCommand(new ReplaceCommand(NULL, drupal_render($status_messages)));
}
// Get the current element and count the number of files.
$current_element = NestedArray::getValue($form, $form_parents);
$current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;
// Process user input. $form and $form_state are modified in the process.
drupal_process_form($form['#form_id'], $form, $form_state);
// Retrieve the element to be rendered.
$form = NestedArray::getValue($form, $form_parents);
// Add the special Ajax class if a new file was added.
if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
$form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
} else {
$form['#suffix'] .= '<span class="ajax-new-content"></span>';
}
$status_messages = array('#theme' => 'status_messages');
$form['#prefix'] .= drupal_render($status_messages);
$output = drupal_render($form);
drupal_process_attached($form);
$js = _drupal_add_js();
$settings = drupal_merge_js_settings($js['settings']['data']);
$response = new AjaxResponse();
foreach ($commands as $command) {
$response->addCommand($command, TRUE);
}
return $response->addCommand(new ReplaceCommand(NULL, $output, $settings));
}
示例13: dialog
/**
* Util to render dialog in ajax callback.
*
* @param bool $is_modal
* (optional) TRUE if modal, FALSE if plain dialog. Defaults to FALSE.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response object.
*/
protected function dialog($is_modal = FALSE)
{
$content = ajax_test_dialog_contents();
$response = new AjaxResponse();
$title = $this->t('AJAX Dialog contents');
$html = drupal_render($content);
if ($is_modal) {
$response->addCommand(new OpenModalDialogCommand($title, $html));
} else {
$selector = '#ajax-test-dialog-wrapper-1';
$response->addCommand(new OpenDialogCommand($selector, $title, $html));
}
return $response;
}
示例14: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$response = new AjaxResponse();
if ($form_state->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$status_messages = array('#theme' => 'status_messages');
$output = drupal_render($form);
$output = '<div>' . drupal_render($status_messages) . $output . '</div>';
$response->addCommand(new HtmlCommand('#editor-link-dialog-form', $output));
} else {
$response->addCommand(new EditorDialogSave($form_state->getValues()));
$response->addCommand(new CloseModalDialogCommand());
}
return $response;
}
示例15: dialog
/**
* Util to render dialog in ajax callback.
*
* @param bool $is_modal
* (optional) TRUE if modal, FALSE if plain dialog. Defaults to FALSE.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response object.
*/
protected function dialog($is_modal = FALSE)
{
$content = AjaxTestController::dialogContents();
$response = new AjaxResponse();
$title = $this->t('AJAX Dialog contents');
// Attach the library necessary for using the Open(Modal)DialogCommand and
// set the attachments for this Ajax response.
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
if ($is_modal) {
$response->addCommand(new OpenModalDialogCommand($title, $content));
} else {
$selector = '#ajax-test-dialog-wrapper-1';
$response->addCommand(new OpenDialogCommand($selector, $title, $content));
}
return $response;
}