CloseDialogCommand example

    $state = $this->getMediaLibraryState($form_state);
    $selected_count = $this->getSelectedMediaItemCount($media_ids$form_state);

    $available_slots = $this->getMediaLibraryState($form_state)->getAvailableSlots();
    if ($available_slots > 0 && $selected_count > $available_slots) {
      // Return to library where we display a warning about the overage.       return $this->updateLibrary($form$form_state);
    }

    return $this->openerResolver->get($state)
      ->getSelectionResponse($state$media_ids)
      ->addCommand(new CloseDialogCommand());
  }

  /** * Get the number of selected media. * * @param array $media_ids * Array with the media IDs. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current form state. * * @return int * The number of media currently selected. */
$error = \Drupal::translation()->formatPlural($selected_count - $available_slots, 'There are currently @total items selected. The maximum number of items for the field is @max. Please remove @count item from the selection.', 'There are currently @total items selected. The maximum number of items for the field is @max. Please remove @count items from the selection.', [
        '@total' => $selected_count,
        '@max' => $available_slots,
      ]);
      $response->addCommand(new MessageCommand($error, '#media-library-messages', ['type' => 'error']));
      return $response;
    }

    return \Drupal::service('media_library.opener_resolver')
      ->get($state)
      ->getSelectionResponse($state$selected_ids)
      ->addCommand(new CloseDialogCommand());
  }

  /** * {@inheritdoc} */
  public function viewsFormValidate(array &$form, FormStateInterface $form_state) {
    $selected = array_filter($form_state->getValue($this->options['id']));
    if (empty($selected)) {
      $form_state->setErrorByName('', $this->t('No items selected.'));
    }
  }

  
if ($this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id()$bundle_id)) {
        $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()]);
    
return $build;
  }

  /** * Returns an AjaxResponse with command to close dialog. * * @return \Drupal\Core\Ajax\AjaxResponse * The JSON response object. */
  public function dialogClose() {
    $response = new AjaxResponse();
    $response->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
    return $response;
  }

  /** * Render types. * * @return array * Render types. */
  protected function getRenderTypes() {
    $render_single_root = [
      

  protected function rebuildAndClose(SectionStorageInterface $section_storage) {
    $response = $this->rebuildLayout($section_storage);
    $response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
    return $response;
  }

  /** * Rebuilds the layout. * * @param \Drupal\layout_builder\SectionStorageInterface $section_storage * The section storage. * * @return \Drupal\Core\Ajax\AjaxResponse * An AJAX response to either rebuild the layout and close the dialog, or * reload the page. */
'selector' => '#drupal-modal',
      'persist' => FALSE,
    ];

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

  /** * @covers \Drupal\Core\Ajax\CloseDialogCommand */
  public function testCloseDialogCommand() {
    $command = new CloseDialogCommand('#some-dialog', TRUE);
    $expected = [
      'command' => 'closeDialog',
      'selector' => '#some-dialog',
      'persist' => TRUE,
    ];

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

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