Skip to content
Snippets Groups Projects

OpenModalDialogWithUrl command

Open Srishti Bankar requested to merge issue/drupal-3408738:3408738-create-a-new into 11.x
Files
4
<?php
declare(strict_types=1);
namespace Drupal\Core\Ajax;
use Drupal\Component\Utility\UrlHelper;
/**
* Provides an AJAX command for opening a modal with URL.
*
* OpenDialogCommand is a similar class which opens modals but works
* differently as it needs all data to be passed through dialogOptions while
* OpenModalDialogWithUrl fetches the data from routing info of the URL.
*
* @see \Drupal\Core\Ajax\OpenDialogCommand
*/
class OpenModalDialogWithUrl implements CommandInterface {
    • :thinking: Why does this not extend \Drupal\Core\Ajax\OpenModalDialogCommand?

      If it doesn't, we should document the relationship and difference in the docblock :pray:

      • We are not reusing anything from \Drupal\Core\Ajax\OpenModalDialogCommand class so it's not required according to me.

        Edited by utkarsh_33
      • That's not an answer :sweat_smile:

        These are clearly conceptually related. Plus, this is clearly using the same JS. So the relationship must be documented.

        Otherwise it becomes even more confusing/difficult for a developer to choose between these.

      • This is done intentionally as we don't want the public methods and it's much more similar to the RedirectCommandthan the OpenModalDialogCommand , well does open the url in a modal.

      • but we could add a comment to link them as means to open modals still OpenModalDialogCommand and OpenModalDialogWithUrl

      • Yes, please link to them with an @see and describe the difference.

        :thinking: Which public methods do you not want? Please list them and describe why they're unwanted in this docblock.

      • I added a comment on OpenModalDialogWithUrl and reference to the AjaxCommandsTest which might be helpful.

      • Please register or sign in to reply
Please register or sign in to reply
/**
* Constructs a OpenModalDialogWithUrl object.
*
* @param string $url
* Only Internal URLs or URLs with the same domain and base path are
* allowed.
* @param array $settings
* The dialog settings.
*/
public function __construct(
protected string $url,
protected array $settings,
) {}
/**
* {@inheritdoc}
*/
public function render() {
// @see \Drupal\Core\Routing\LocalAwareRedirectResponseTrait::isLocal()
if (!UrlHelper::isExternal($this->url) || UrlHelper::externalIsLocal($this->url, $this->getBaseURL())) {
return [
'command' => 'openModalDialogWithUrl',
'url' => $this->url,
'dialogOptions' => $this->settings,
];
}
throw new \LogicException('External URLs are not allowed.');
}
/**
* Gets the complete base URL.
*/
private function getBaseUrl() {
$requestContext = \Drupal::service('router.request_context');
return $requestContext->getCompleteBaseUrl();
}
}
Loading