AlertCommand example

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->renderer->renderRoot($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 = ['#type' => 'status_messages'];
    


  /** * Tests the behavior of an error alert command. */
  public function testAJAXRenderError() {
    // Verify custom error message.     $edit = [
      'message' => 'Custom error message.',
    ];
    $commands = $this->drupalGetAjax('ajax-test/render-error', ['query' => $edit]);
    $expected = new AlertCommand($edit['message']);
    $this->assertCommand($commands$expected->render());
  }

  /** * Asserts the array of Ajax commands contains the searched command. * * An AjaxResponse object stores an array of Ajax commands. This array * sometimes includes commands automatically provided by the framework in * addition to commands returned by a particular controller. During testing, * we're usually interested that a particular command is present, and don't * care whether other commands precede or follow the one we're interested in. * Additionally, the command we're interested in may include additional data * that we're not interested in. Therefore, this function simply asserts that * one of the commands in $haystack contains all of the keys and values in * $needle. Furthermore, if $needle contains a 'settings' key with an array * value, we simply assert that all keys and values within that array are * present in the command we're checking, and do not consider it a failure if * the actual command contains additional settings that aren't part of * $needle. * * @param array $haystack * An array of rendered Ajax commands returned by the server. * @param array $needle * Array of info we're expecting in one of those commands. * * @internal */

  public function renderError(Request $request) {
    $message = '';
    $query = $request->query;
    if ($query->has('message')) {
      $message = $query->get('message');
    }
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand($message));
    return $response;
  }

  /** * Returns a render array of form elements and links for dialog. */
  public function dialog() {
    // Add two wrapper elements for testing non-modal dialogs. Modal dialogs use     // the global drupal-modal wrapper by default.     $build['dialog_wrappers'] = ['#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>'];

    

    ];
    $request = new Request();
    $form = [
      'test' => [
        '#type' => 'textfield',
      ],
    ];
    $form_state = new FormState();
    $form_state->setTriggeringElement($triggering_element);
    $commands = [
      new AlertCommand('alert!'),
    ];
    $commands_expected = [];
    $commands_expected[] = ['command' => 'alert', 'text' => 'alert!'];

    $this->renderer->expects($this->never())
      ->method('renderResponse');

    $result = $this->formAjaxResponseBuilder->buildResponse($request$form$form_state$commands);
    $this->assertInstanceOf('\Drupal\Core\Ajax\AjaxResponse', $result);
    $this->assertSame($commands_expected$result->getCommands());
  }

  
'data' => '<p>New Text!</p>',
      'settings' => ['my-setting' => 'setting'],
    ];

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

  /** * @covers \Drupal\Core\Ajax\AlertCommand */
  public function testAlertCommand() {
    $command = new AlertCommand('Set condition 1 throughout the ship!');
    $expected = [
      'command' => 'alert',
      'text' => 'Set condition 1 throughout the ship!',
    ];

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

  /** * @covers \Drupal\Core\Ajax\AnnounceCommand * * @dataProvider announceCommandProvider */
Home | Imprint | This part of the site doesn't use cookies.