Как в функции вызывающей диалоговое окно добавить класс этому окну?

Главные вкладки

Аватар пользователя VasyOK VasyOK 12 декабря 2020 в 20:14

Приветствую специалистов по высокоуровневому программированию!

Есть функция. Выводит диалоговое окно корзины с шириной 700px.
Как сделать, чтобы у этого окна класс добавился?

public function onResponse(FilterResponseEvent $event) {
    $response = $event->getResponse();

    // We only care if this happened after an entity was added to the cart.
    if (!$this->purchasedEntity) {
      return;
    }

    // We only care about AJAX responses.
    if (!$response instanceof AjaxResponse) {
      return;
    }

    // Render the status message and the entity.
    $view_builder = $this->entityTypeManager->getViewBuilder('commerce_product_variation');
    $product_variation = $view_builder->view($this->purchasedEntity, 'dc_ajax_add_to_cart_popup');
    $content = [
      '#theme' => 'dc_ajax_add_cart_popup',
      '#product_variation' => $product_variation,
      '#product_variation_entity' => $this->purchasedEntity,
      '#cart_url' => Url::fromRoute('commerce_cart.page')->toString(),
    ];
    $title = '';
    $options = ['width' => '700'];    
    $response->addCommand(new OpenModalDialogCommand($title, $content, $options));
    $event->setResponse($response);
  }

Лучший ответ

Аватар пользователя OldWarrior OldWarrior 13 декабря 2020 в 17:39
1

VasyOK wrote: А как ваш код:

Как-то так:

<?php
// В начале файла.
use Drupal\Core\Ajax\InvokeCommand;
...
// Да, здесь просто селектор вашего диалогового окна,
// к которому желаете добавить класс.
$selector='.myDialogClass';
// Метод jQuery (не менять).
$method='addClass';
// Аргумент может быть и один - именно имя класса,
// который добавляете.
$arguments=['class1']; 
...
$response->addCommand(new OpenModalDialogCommand($title$content$options));
$response->addCommand(new InvokeCommand($selector$method$arguments));
$event->setResponse($response);
?>

Не знаю, почему не работает. Должно.

Комментарии

Аватар пользователя OldWarrior OldWarrior 12 декабря 2020 в 20:42

Не сразу понял, что речь о добавлении нового класса, а не об его определении.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Ajax%21In...

<?php
$selector
='.myClass[name$="myNameSuffix"]'/* A jQuery selector. */
$method='addClass'/* The name of a jQuery method to invoke. */
$arguments=['class1','class2']; /* (Optional) An array of arguments to pass to the method. */
$response->addCommand(new InvokeCommand($selector,$method,$arguments));
?>
Аватар пользователя VasyOK VasyOK 13 декабря 2020 в 14:37

А как ваш код:

$selector='.myClass[name$="myNameSuffix"]';
/* Сюда селектор диалогового окна ?
myNameSuffix - обязателен?*/

$method='addClass';
$arguments=['class1','class2'];
$response->addCommand(new InvokeCommand($selector,$method,$arguments));

Скрестить с имеющимся (?):

$title = '';
$options = ['width' => '700'];    
$response->addCommand(new OpenModalDialogCommand($title, $content, $options));
$event->setResponse($response);

Пробовал:

$response->addCommand(new OpenModalDialogCommand($title, $content, $options) InvokeCommand($selector,$method,$arguments));

и

$response->addCommand(new OpenModalDialogCommand($title, $content, $options));
$response->addCommand(new InvokeCommand($selector,$method,$arguments));

видимо это так не работает Sad

Аватар пользователя OldWarrior OldWarrior 13 декабря 2020 в 17:39
1

VasyOK wrote: А как ваш код:

Как-то так:

<?php
// В начале файла.
use Drupal\Core\Ajax\InvokeCommand;
...
// Да, здесь просто селектор вашего диалогового окна,
// к которому желаете добавить класс.
$selector='.myDialogClass';
// Метод jQuery (не менять).
$method='addClass';
// Аргумент может быть и один - именно имя класса,
// который добавляете.
$arguments=['class1']; 
...
$response->addCommand(new OpenModalDialogCommand($title$content$options));
$response->addCommand(new InvokeCommand($selector$method$arguments));
$event->setResponse($response);
?>

Не знаю, почему не работает. Должно.