Приветствую специалистов по высокоуровневому программированию!
Есть функция. Выводит диалоговое окно корзины с шириной 700px.
Как сделать, чтобы у этого окна класс добавился?
$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);
}
Комментарии
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Ajax%21Ad...
Example:
<?php
$CssString = '<style>.myclass{color:red;}</style>'; /*A string that contains the styles to be added to the page, including the wrapping <style> tag.*/
$response->addCommand(new AddCssCommand($CssString));
?>
Не сразу понял, что речь о добавлении нового класса, а не об его определении.
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));
?>
А как ваш код:
/* Сюда селектор диалогового окна ?
myNameSuffix - обязателен?*/
$method='addClass';
$arguments=['class1','class2'];
$response->addCommand(new InvokeCommand($selector,$method,$arguments));
Скрестить с имеющимся (?):
$options = ['width' => '700'];
$response->addCommand(new OpenModalDialogCommand($title, $content, $options));
$event->setResponse($response);
Пробовал:
и
$response->addCommand(new InvokeCommand($selector,$method,$arguments));
видимо это так не работает
Как-то так:
<?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);
?>
Не знаю, почему не работает. Должно.
use Drupal\Core\Ajax\InvokeCommand; - точно!
Супер. Все работает. Спасибо, OldWarrior!