Allow easily creating dialog links

Created on 13 February 2018, about 7 years ago
Updated 23 April 2025, 1 day ago

Problem/Motivation

To make a dialog link in Drupal you have know to do this

'normal_modal' => [
        '#title' => 'Normal Modal!',
        '#type' => 'link',
        '#url' => Url::fromRoute('dialog_renderer_test.modal_content'),
        '#attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'modal',
        ],
        '#attached' => [
          'library' => [
            'core/drupal.ajax',
          ],
        ],
      ],

To use the off-canvas dialog:

'off_canvas_link_1' => [
        '#title' => 'Click Me 1!',
        '#type' => 'link',
        '#url' => Url::fromRoute('off_canvas_test.thing1'),
        '#attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ],

You have to know about data-dialog-type, use-ajax, data-dialog-renderer.

But now we have Link objects and \Drupal\Core\Link::toRenderable
So making regular links is easier but we can't make dialog links this way.

Proposed resolution

Make it really easy and obvious to make dialog links.

Add \Drupal\Core\Link::openInDailog()

Remaining tasks

  1. Update ::toString() to handle new attributes.
  2. Create tests
  3. Create change record

User interface changes

None

API changes

New method

Data model changes

None

โœจ Feature request
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component

render system

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States tedbow Ithaca, NY, USA

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    prashant.c โ†’ changed the visibility of the branch 2944554-allow-easily-creating to hidden.

  • @prashantc opened merge request.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Have just created an MR from the patch submitted in #18 โ†’ . Going to test these methods locally first.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala
    1. Type "modal" worked fine.
    2. Fixed issues for type "off_canvas"
    3. Handled for the "off_canvas_top"

    To quickly test, return the following code in a block or controller:

    Open link in a "modal" window:

        $url = Url::fromRoute('entity.node.canonical', ['node' => 1], ['absolute' => TRUE]);
        $link = Link::fromTextAndUrl($this->t('Read more'), $url);
        $link->openInRenderer('modal');
      return [
          'read_more' => $link->toRenderable(),
      ];
    

    Open link in an "off-canvas" window:

        $link->openInRenderer('off_canvas');
    

    Open link in an "off_canvas_top" window:

        $link->openInRenderer('off_canvas', ['position' => 'top']);
    
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    A review of the current code is needed before moving forward with this. After that, we can go ahead with writing tests for this.

Production build 0.71.5 2024